?
Solved

Linux: total wcpu used by top 5 resource hogging processes

Posted on 2013-11-18
3
Medium Priority
?
345 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 2000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

770 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