Skip to content

Instantly share code, notes, and snippets.

@fuzzy
Created October 31, 2011 18:12
Show Gist options
  • Save fuzzy/1328281 to your computer and use it in GitHub Desktop.
Save fuzzy/1328281 to your computer and use it in GitHub Desktop.
This is a quick recipe for a simple 'top' for watching DistCC. I know there's distccmon-text, but I prefer a 'top'-like display.
#!/usr/bin/env python
import os, sys, time
import curses,struct
states = ['STARTUP', 'BLOCKED', 'CONNECT', 'PREPROCESS', 'SEND', 'COMPILE', 'RECEIVE', 'DONE']
DISTCC_DIR = os.getenv('DISTCC_DIR') or '%s/.distcc' % os.getenv('HOME')
def getStats(scr):
curses.init_pair(10, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(11, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(12, curses.COLOR_GREEN, curses.COLOR_BLACK)
(maxY, maxX) = scr.getmaxyx()
while 1:
try:
scr.addstr(0, 0, time.ctime())
scr.addstr(1, 0, 'System Load: %s' % (str(os.getloadavg())[1:][:-1]))
scr.addstr(3, 0, '%-4s %-20s %-15s %-40s%s' % ('Slot', 'Remote Host', 'State', 'Filename', ' '*(maxX-83)), curses.A_BOLD|curses.A_REVERSE)
cnt = 5
try:
for i in os.listdir(DISTCC_DIR+'/state'):
data = struct.unpack('@iLL128s128siiP', open(DISTCC_DIR+'/state/'+i).readline().strip())
file = data[3].split('\x00')[0] or 'None'
host = data[4].split('\x00')[0] or 'None'
slot = int(data[5])
stte = states[int(data[6])]
if 'None' not in (file, host):
scr.addstr(cnt, 0, '%4d %-20s ' % (slot, host))
if int(data[6]) in (2,3):
scr.addstr(cnt, 26, '%-15s ' % (stte), curses.color_pair(10))
elif int(data[6]) in (0,1):
scr.addstr(cnt, 26, '%-15s ' % (stte), curses.color_pair(11))
elif int(data[6]) in (4,5):
scr.addstr(cnt, 26, '%-15s ' % (stte), curses.color_pair(12))
elif int(data[6]) in (6,7):
scr.addstr(cnt, 26, '%-15s ' % (stte), curses.color_pair(12)|curses.A_BOLD)
else: scr.addstr(cnt, 26, '%-15s ' % (stte))
scr.addstr(cnt, 42, '%-40s%s' % (file, ' '*(maxX-83)))
cnt += 1
except struct.error: pass
except IOError: pass
scr.refresh()
time.sleep(0.75)
scr.erase()
scr.move(0,0)
except KeyboardInterrupt:
sys.exit(-1)
if __name__ == '__main__':
curses.wrapper(getStats)
@fuzzy
Copy link
Author

fuzzy commented Nov 1, 2011

Now with color, respect for terminal sizes (init not resizes), and 10% less sodium!!! SWEET JESUS!@!#!TY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment