Solved

Linux: top results in JSON format

Posted on 2013-11-15
2
610 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
Comment Utility
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
Comment Utility
Great, even better than what I asked for.  Thanks!
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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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 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…

771 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

9 Experts available now in Live!

Get 1:1 Help Now