Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linux: top results in JSON format

Posted on 2013-11-15
2
Medium Priority
?
816 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

618 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