Skip to content

Instantly share code, notes, and snippets.

@guyromm
Last active August 29, 2015 14:07
Show Gist options
  • Save guyromm/2bcc88b8984b0dfb9322 to your computer and use it in GitHub Desktop.
Save guyromm/2bcc88b8984b0dfb9322 to your computer and use it in GitHub Desktop.
parseprocs - jenkins process type parse
#!/usr/bin/env python
# usage
# 1. run on a jenkins node to get a grouping of processes by type with memory consumption:
# ps aux | /root/parseprocs.py
# 2. get a detail of which processes were included under a particular set of tags:
# ps aux | /root/parseprocs.py celery,python,user_attrs_actions
import sys
import re
av_signs = {
'python':re.compile('python'),
'runserver':re.compile('runserver'),
'celery':re.compile('celery'),
'bash':re.compile('bash'),
'php-fpm':re.compile('fpm'),
'tmux':re.compile('tmux'),
'mysqld':re.compile('mysqld'),
'jenkins':re.compile('java(.*)jenkins'),
'slave':re.compile('slave.jar'),
'nginx':re.compile('^nginx'),
'xvfb':re.compile('xvfb',re.I),
'chromedriver':re.compile('chromedriver'),
'chrome':re.compile('chrome'),
'redis':re.compile('redis-server'),
'collectd':re.compile('collectd'),
'user':re.compile('(emacs|ps aux)'),
'sys':re.compile('(/sbin/(init|udevd)|upstart-(udev|socket)-bridge|dhclient3|/usr/sbin/sshd|rsyslogd|dbus-daemon|/sbin/getty|acpid|cron|atd|irqbalance|console-kit-daemon|polkitd|whoopsie|rinetd|sshd|dbus-launch)'),
'kern':re.compile('^\[')
}
ct = re.compile('-n ([^ ]+)')
bysign={} ; bysign_amt={} ; mlen = 0 ; tots={'mem':0,'qty':0}
for ln in sys.stdin:
if 'VSZ' in ln: continue
row = ln.split()
mem = int(row[5])
cmd = ' '.join(row[10:])
#tag acquisition
signs=[]
for tag,expr in av_signs.items():
if expr.search(cmd) and tag not in signs:
signs.append(tag)
#custom tag
if ct.search(cmd): signs.append(ct.search(cmd).group(1))
signs.sort()
key = ','.join(signs)
if len(key)>mlen: mlen=len(key)
if key not in bysign:
bysign[key]=0
bysign_amt[key]=0
bysign[key]+=mem
bysign_amt[key]+=1
tots['mem']+=mem
tots['qty']+=1
if not len(signs):
print mem,cmd,'<-- UNFOUND'
if len(sys.argv)>1 and key in sys.argv[1:]:
print mem,cmd
bysign = bysign.items()
bysign.sort(lambda x,y: cmp(x[1],y[1]))
print 'tags'.ljust(mlen),'mem'.rjust(15),'qty'.rjust(15)
print ('='*(mlen+15+15+3))
for k,v in bysign:
print k.ljust(mlen),str('{:,.0f}'.format(v)).rjust(15),str(bysign_amt[k]).rjust(15)
print ('-'*(mlen+15+15+3))
print 'TOT'.ljust(mlen),str('{:,.0f}'.format(tots['mem'])).rjust(15),str('{:,.0f}'.format(tots['qty'])).rjust(15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment