• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Linux: total wcpu used by top 5 resource hogging processes

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
hankknight
Asked:
hankknight
  • 2
1 Solution
 
simon3270Commented:
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
 
hankknightAuthor Commented:
Thanks for the excellent solution!
0
 
hankknightAuthor Commented:
I have asked a related question here:
http://www.experts-exchange.com/OS/Linux/Q_28299717.html
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now