?
Solved

Linux: Add additional info about memory and swap when parsing top

Posted on 2013-11-17
1
Medium Priority
?
280 Views
Last Modified: 2013-11-17
I would like a few changes made to this:

1.
Instead of "memory" include:
memoryTotal
memoryUsed

2.
Instead of "swap" include:
swapTotal
swapUsed
# Thanks, woolmilkporc
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", $4/$2}'

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
1 Comment
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 39654773
Here you go:
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/   {gsub("k "," ",$0); print "\"memoryTotal\":", $2 ","
                                                        print "\"memoryUsed\":", $4 ","}
                            /Swap/  {gsub("k "," ",$0); print "\"swapTotal\":", $2 ","
                                                        print "\"swapUsed\":",$4}'

Open in new window

To get all output into one line try
echo $(
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/   {gsub("k "," ",$0); print "\"memoryTotal\":", $2 ","
                                                        print "\"memoryUsed\":", $4 ","}
                            /Swap/  {gsub("k "," ",$0); print "\"swapTotal\":", $2 ","
                                                        print "\"swapUsed\":",$4}'

      )

Open in new window

or
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/   {gsub("k "," ",$0); print "\"memoryTotal\":", $2 ","
                                                        print "\"memoryUsed\":", $4 ","}
                            /Swap/  {gsub("k "," ",$0); print "\"swapTotal\":", $2 ","
                                                        print "\"swapUsed\":",$4}' | tr "\n" " "

Open in new window

0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

765 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