Solved

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

Posted on 2013-11-17
1
272 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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). …
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

785 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