Solved

Linux: total wcpu used by top 5 resource hogging processes

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
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.

740 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