• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

Linux: Add additional info about memory and swap when parsing top

I would like a few changes made to this:

1.
Instead of "memory" include:
memoryTotal
memoryUsed

2.
Instead of "swap" include:
swapTotal
swapUsed
# Thanks, woolmilkporc
top -b -1 | head -6 | awk '/load/   {print "\"load\":", $12}
                            /Tasks/ {print "\"tasksRunning\":", $4 ",";
                                     print "\"tasksSleeping\":", $6 ","
                                     print "\"tasksStopped\":", $8 ","
                                     print "\"tasksZombie\":", $10 ","}
                            /Cpu/   {sub("%us,"," ",$0);sub("%sy,"," ",$0);sub("%wa,"," ",$0);
                                        print "\"cpu\":",$2+$3+$6 ","}
                            /Mem/   {sub("k "," ",$0); printf "\"memory\": %.9f,\n", $4/$2}
                            /Swap/  {sub("k "," ",$0); printf "\"swap\": %.5f", $4/$2}'

Open in new window

0
hankknight
Asked:
hankknight
1 Solution
 
woolmilkporcCommented:
Here you go:
top -b -1 | head -6 | awk '/load/   {print "\"load\":", $12}
                            /Tasks/ {print "\"tasksRunning\":", $4 ",";
                                     print "\"tasksSleeping\":", $6 ","
                                     print "\"tasksStopped\":", $8 ","
                                     print "\"tasksZombie\":", $10 ","}
                            /Cpu/   {sub("%us,"," ",$0);sub("%sy,"," ",$0);sub("%wa,"," ",$0);
                                     print "\"cpu\":", $2+$3+$6 ","}
                            /Mem/   {gsub("k "," ",$0); print "\"memoryTotal\":", $2 ","
                                                        print "\"memoryUsed\":", $4 ","}
                            /Swap/  {gsub("k "," ",$0); print "\"swapTotal\":", $2 ","
                                                        print "\"swapUsed\":",$4}'

Open in new window

To get all output into one line try
echo $(
top -b -1 | head -6 | awk '/load/   {print "\"load\":", $12}
                            /Tasks/ {print "\"tasksRunning\":", $4 ",";
                                     print "\"tasksSleeping\":", $6 ","
                                     print "\"tasksStopped\":", $8 ","
                                     print "\"tasksZombie\":", $10 ","}
                            /Cpu/   {sub("%us,"," ",$0);sub("%sy,"," ",$0);sub("%wa,"," ",$0);
                                     print "\"cpu\":", $2+$3+$6 ","}
                            /Mem/   {gsub("k "," ",$0); print "\"memoryTotal\":", $2 ","
                                                        print "\"memoryUsed\":", $4 ","}
                            /Swap/  {gsub("k "," ",$0); print "\"swapTotal\":", $2 ","
                                                        print "\"swapUsed\":",$4}'

      )

Open in new window

or
top -b -1 | head -6 | awk '/load/   {print "\"load\":", $12}
                            /Tasks/ {print "\"tasksRunning\":", $4 ",";
                                     print "\"tasksSleeping\":", $6 ","
                                     print "\"tasksStopped\":", $8 ","
                                     print "\"tasksZombie\":", $10 ","}
                            /Cpu/   {sub("%us,"," ",$0);sub("%sy,"," ",$0);sub("%wa,"," ",$0);
                                     print "\"cpu\":", $2+$3+$6 ","}
                            /Mem/   {gsub("k "," ",$0); print "\"memoryTotal\":", $2 ","
                                                        print "\"memoryUsed\":", $4 ","}
                            /Swap/  {gsub("k "," ",$0); print "\"swapTotal\":", $2 ","
                                                        print "\"swapUsed\":",$4}' | tr "\n" " "

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now