Solved

Linux: total wcpu used by top 5 resource hogging processes

Posted on 2013-11-18
3
343 Views
Last Modified: 2013-11-20
I would like the combined wcpu value for the five processes that use the most wcpu.

Results should be combined.  For example, if there are 25 processes with the CMD "httpd" and each uses 0.1 wcpu then the value for "httpd" should be 2.5
 
I would like the results in this format:
{
 "hogs": {
  "mysql" 38.13
  "httpd" 2.45,
  "sendmail" 0.45,
  "sshd" 0.02,
  "bash" 0.01
 }
}

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
3 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 39659851
Here she is.

This assumes the following format of "top" output, with the processes starting on the 8th line of output, the %CPU as column 9, and the process name as column 12.  If your "top" output is different, change the 8, 9 and 12 values below.

It also assumes the Linux variant of "top", where "top -bn 1" produces a single pass of top to stdout, with as many processes as there are running, then exits.  Other UNIX-like systems have different "top" parameters to get the same effect.
top - 16:34:15 up  6:09,  1 user,  load average: 1.35, 1.08, 1.03
Tasks: 159 total,   1 running, 158 sleeping,   0 stopped,   0 zombie
Cpu(s): 47.4%us, 22.6%sy,  0.1%ni, 20.5%id,  1.1%wa,  0.4%hi,  7.9%si,  0.0%st
Mem:   1278584k total,  1089316k used,   189268k free,    76912k buffers
Swap:  2097144k total,     2148k used,  2094996k free,   576312k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                         
24735 root      20   0 11560 1684 1216 S 15.0  0.1   0:00.12 service                                         
 1790 root      20   0  332m 226m 3668 S 11.2 18.1 103:18.45 puppetd                                         
16881 root      20   0 11140 3332 2104 S  1.2  0.3   3:19.55 IPAgent                                         
16882 root      20   0  9976 3236 2072 S  1.2  0.3   3:19.35 HostMonitor                                     
24718 root      20   0 15020 1212  876 R  1.2  0.1   0:00.11 top                                             
    1 root      20   0 19228 1392 1192 S  0.0  0.1   0:02.04 init 

Open in new window


The script uses several awk processes, each with a "sort" between.  There's probbaly a cleaner way, but this does the job reasonably quickly.  The first awk selects just the CPU and Processname columns, and removes the decimal point from the CPU.  The second one adds up the %CPU values for processes with the same name, then prints out the process name and the summed CPU percentage.  The third awk adds the header and footer, formats the individual lines, and limits it to the top 5 process names.
top -bn 1 | awk 'NR >= 8{split($9,a,".");print $12 " " a[1] a[2]}' | sort | awk '{if (s == $1){n=n+$2}else {if (s != ""){print s " " n};s=$1;n=$2}}END{print s " " n}' | sort -rn -k +2 | awk 'BEGIN{print "{\n \"hogs\": {"}NR <= 5{printf "   \"%s\" %.1f,\n",$1,$2/10}END{print " }\n}\n"}'

Open in new window

0
 
LVL 16

Author Closing Comment

by:hankknight
ID: 39662657
Thanks for the excellent solution!
0
 
LVL 16

Author Comment

by:hankknight
ID: 39663845
I have asked a related question here:
http://www.experts-exchange.com/OS/Linux/Q_28299717.html
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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…
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.

737 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