Solved

Linux: top results in JSON format

Posted on 2013-11-15
2
734 Views
Last Modified: 2013-11-15
top -b -1 | head -6
top - 07:28:22 up 626 days, 19:01,  2 users,  load average: 0.15, 0.03, 0.08
Tasks: 137 total,   2 running, 135 sleeping,   0 stopped,   0 zombie
Cpu(s):  2.4%us,  0.5%sy,  0.0%ni, 96.8%id,  0.2%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4149608k total,  3997996k used,   151612k free,    28500k buffers
Swap:  4104556k total,       84k used,  4104472k free,  3472140k cached

Open in new window

How can I get results in this format?
"load": 0.15,
"tasksRunning": 135,
"tasksSleeping": 0,
"tasksStopped": 0,
"tasksZombie": 0,
"cpu": 3.26,
"memory": 0.963463537,
"swap":  0.00002,

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
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39651157
Nice task!

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,\n", $4/$2}'

Open in new window

I calculated "cpu" by adding the "%us", "%sy" and "%wa" values, and I calculated "memory" and "swap" by dividing the "used" value by the "total" value.
Just guessing, though ...

I just saw your new question: To omit the last comma just change line 9 to

/Swap/  {sub("k "," ",$0); printf "\"swap\": %.5f\n", $4/$2}'
0
 
LVL 16

Author Comment

by:hankknight
ID: 39651196
Great, even better than what I asked for.  Thanks!
0

Featured Post

Industry Leaders: 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

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). …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

737 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