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

x
Solved

Posted on 2013-11-17
Medium Priority
283 Views
I would like a few changes made to this:

1.
memoryTotal
memoryUsed

2.
swapTotal
swapUsed
``````# Thanks, woolmilkporc
/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", \$4/\$2}'
``````
0
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
1 Comment

LVL 68

Accepted Solution

woolmilkporc earned 2000 total points
ID: 39654773
Here you go:
``````top -b -1 | head -6 | awk '/load/   {print "\"load\":", \$12}
/Cpu/   {sub("%us,"," ",\$0);sub("%sy,"," ",\$0);sub("%wa,"," ",\$0);
print "\"cpu\":", \$2+\$3+\$6 ","}
/Mem/   {gsub("k "," ",\$0); print "\"memoryTotal\":", \$2 ","
print "\"memoryUsed\":", \$4 ","}
/Swap/  {gsub("k "," ",\$0); print "\"swapTotal\":", \$2 ","
print "\"swapUsed\":",\$4}'
``````
To get all output into one line try
``````echo \$(
/Cpu/   {sub("%us,"," ",\$0);sub("%sy,"," ",\$0);sub("%wa,"," ",\$0);
print "\"cpu\":", \$2+\$3+\$6 ","}
/Mem/   {gsub("k "," ",\$0); print "\"memoryTotal\":", \$2 ","
print "\"memoryUsed\":", \$4 ","}
/Swap/  {gsub("k "," ",\$0); print "\"swapTotal\":", \$2 ","
print "\"swapUsed\":",\$4}'

)
``````
or
``````top -b -1 | head -6 | awk '/load/   {print "\"load\":", \$12}
/Cpu/   {sub("%us,"," ",\$0);sub("%sy,"," ",\$0);sub("%wa,"," ",\$0);
print "\"cpu\":", \$2+\$3+\$6 ","}
/Mem/   {gsub("k "," ",\$0); print "\"memoryTotal\":", \$2 ","
print "\"memoryUsed\":", \$4 ","}
/Swap/  {gsub("k "," ",\$0); print "\"swapTotal\":", \$2 ","
print "\"swapUsed\":",\$4}' | tr "\n" " "
``````
0

## Featured Post

Question has a verified solution.

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

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…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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
Course of the Month9 days, 22 hours left to enroll