Skip to content

Instantly share code, notes, and snippets.

@tomviner
Last active November 29, 2021 12:42
Show Gist options
  • Save tomviner/3435564 to your computer and use it in GitHub Desktop.
Save tomviner/3435564 to your computer and use it in GitHub Desktop.
Parse ps output to show top CPU, memory and process count, summed over identically named processes

download and install

git clone git://gist.github.com/3435564.git sum_top_gist
chmod +x sum_top_gist/sum_top.sh 
ln -s `readlink -f sum_top_gist/sum_top.sh`  ~/bin/sum_top
sum_top
import string
def pretty_size(size):
"size in bytes"
size = int(size)
units = ' KMGTPE'+string.lowercase[::-1]
bytes = 'B'
i = 0
while size>1024:
i += 1
size /= 1024.0
return '% 2.1f %s' %(size, units[i]+bytes)
def pretty_time(t):
"time t in secs"
t = float(t)
unit = 'secs'
weeks_in_year = 365.25/7
d = ((60,'mins'),(60,'hours'),(24,'days'),(7,'weeks'),(weeks_in_year,'year'),
(10,'decade'),(10,'century'),(10,'millenia'))
for mx, name in d:
if t>mx:
t /= mx
unit = name
else:
break
return '%2.1f %s' %(t, unit)
def pretty_percent(n, tot=None):
if tot:
n = 100.0*n/tot
return '%2.1f %%' % n
'''
Basic usage:
ps -eo args,%cpu,rss --sort %mem | tail -n+2 | python sum_top.py
cpu
106.0 % /usr/libexec/mysqld --based
95.8 % /usr/sbin/httpd
8.7 % python manage.py garbage_co
0.2 % [kipmi0]
0.1 % memcached -d -l 127.0.0.1 -
mem
991.7 MB /usr/sbin/httpd
358.8 MB /usr/libexec/mysqld --based
42.0 MB memcached -d -l 127.0.0.1 -
23.1 MB /opt/dell/srvadmin/sbin/dsm
18.7 MB python manage.py garbage_co
count
25 x /usr/sbin/httpd
7 x smtp -t unix -u
7 x /opt/dell/srvadmin/sbin/dsm
5 x /usr/sbin/saslauthd -m /var
4 x [kjournald]
You want to record some server stats while you hammer it with ab?
Try this:
nohup python -c 'import time,os,sys;[time.sleep(1) or os.system("sum_top.sh") for _ in range(60*60)]' &
'''
import sys
import pprint
from collections import defaultdict
import pretty
ls = sys.stdin.readlines()
c = defaultdict(lambda: 0)
m = defaultdict(lambda: 0)
n = defaultdict(lambda: 0)
for l in ls:
cmd, cpu, mem = l.rsplit(None, 2)
c[cmd] += float(cpu)
m[cmd] += float(mem)
n[cmd] += 1
for title, dd, fmter in (
('cpu', c, pretty.pretty_percent),
('mem', m, lambda i: pretty.pretty_size(i*1024)),
('count', n, lambda i: '%d x' % i),
):
print title
print '\n'.join('%s %s' % (fmter(v).rjust(9), k) for (k,v) in sorted(dd.items(), key=lambda kv: kv[1], reverse=1)[:5])
# eg
# ln -s ~/sum_top_gist/sum_top.sh ~/bin/sum_top
DIR="$(dirname "$(readlink -f "$0")")"
echo -e "\n\n===============================\n"
echo -e "$(uptime)\n"
echo -e "$(free -m)\n"
echo -e "$(ps -eo args,%cpu,rss --sort %mem | tail -n+2 | python $DIR/sum_top.py)\n"
echo -e "\n===============================\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment