Solved

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

Posted on 2013-11-17
1
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Fine Tune your automatic Updates for Ubuntu / Debian
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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

734 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