Solved

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

Posted on 2013-11-17
1
270 Views
Last Modified: 2013-11-17
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
Comment
Question by:hankknight
1 Comment
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39654773
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now