Solved

Linux: total wcpu used by top 5 resource hogging processes

Posted on 2013-11-18
3
334 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
  • 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
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…
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…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now