Solved

Linux: top results in JSON format

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add a 1000 ms delay after each sending email operation 12 70
CentOS create a user with predefined MD5 Hashed password 17 69
AD LDAP LDS 3 48
linux redhat 7.2 10 43
Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
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…

920 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

12 Experts available now in Live!

Get 1:1 Help Now