Solved

Linux: top results in JSON format

Posted on 2013-11-15
2
674 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
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove a folder in Linux 9 104
nagios monitor 3 54
fedora linux on laptop - setup sendmail - or some kind of email 5 45
CENTOS DHCP Server / PXE/TFTP 14 77
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…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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…
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.:

777 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