2017-10-04 19:22:42 +00:00
|
|
|
|
|
|
|
import curses
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
import mitogen.core
|
|
|
|
import mitogen.master
|
|
|
|
import mitogen.utils
|
|
|
|
|
|
|
|
|
|
|
|
class Host(object):
|
|
|
|
name = None
|
|
|
|
context = None
|
|
|
|
recv = None
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.procs = {} #: pid -> Process()
|
|
|
|
|
2017-10-04 19:29:14 +00:00
|
|
|
|
2017-10-04 19:22:42 +00:00
|
|
|
class Process(object):
|
|
|
|
host = None
|
|
|
|
user = None
|
|
|
|
pid = None
|
|
|
|
ppid = None
|
|
|
|
pgid = None
|
|
|
|
command = None
|
|
|
|
rss = None
|
|
|
|
pcpu = None
|
|
|
|
rss = None
|
|
|
|
|
|
|
|
|
|
|
|
@mitogen.core.takes_router
|
|
|
|
def remote_main(context_id, handle, delay, router):
|
|
|
|
context = mitogen.core.Context(router, context_id)
|
|
|
|
sender = mitogen.core.Sender(context, handle)
|
|
|
|
|
2017-10-05 15:55:29 +00:00
|
|
|
args = ['ps', '-axwwo', 'user,pid,ppid,pgid,%cpu,rss,command']
|
2017-10-04 19:22:42 +00:00
|
|
|
while True:
|
2017-10-05 15:55:29 +00:00
|
|
|
sender.put(subprocess.check_output(args))
|
2017-10-04 19:22:42 +00:00
|
|
|
time.sleep(delay)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_output(host, s):
|
|
|
|
prev_pids = set(host.procs)
|
|
|
|
|
|
|
|
for line in s.splitlines()[1:]:
|
|
|
|
bits = line.split(None, 6)
|
|
|
|
pid = int(bits[1])
|
2017-10-05 15:55:29 +00:00
|
|
|
new = pid not in prev_pids
|
2017-10-04 19:22:42 +00:00
|
|
|
prev_pids.discard(pid)
|
|
|
|
|
|
|
|
try:
|
|
|
|
proc = host.procs[pid]
|
|
|
|
except KeyError:
|
|
|
|
host.procs[pid] = proc = Process()
|
|
|
|
proc.hostname = host.name
|
|
|
|
|
2017-10-05 15:55:29 +00:00
|
|
|
proc.new = new
|
2017-10-04 19:22:42 +00:00
|
|
|
proc.user = bits[0]
|
|
|
|
proc.pid = pid
|
|
|
|
proc.ppid = int(bits[2])
|
|
|
|
proc.pgid = int(bits[3])
|
|
|
|
proc.pcpu = float(bits[4])
|
|
|
|
proc.rss = int(bits[5]) / 1024
|
|
|
|
proc.command = bits[6]
|
|
|
|
|
|
|
|
# These PIDs had no update, so probably they are dead now.
|
|
|
|
for pid in prev_pids:
|
|
|
|
del host.procs[pid]
|
|
|
|
|
|
|
|
|
|
|
|
class Painter(object):
|
|
|
|
def __init__(self, hosts):
|
|
|
|
self.stdscr = curses.initscr()
|
2017-10-05 15:55:29 +00:00
|
|
|
curses.start_color()
|
2017-10-04 19:22:42 +00:00
|
|
|
self.height, self.width = self.stdscr.getmaxyx()
|
|
|
|
curses.cbreak()
|
|
|
|
curses.noecho()
|
|
|
|
self.stdscr.keypad(1)
|
|
|
|
self.hosts = hosts
|
|
|
|
self.format = (
|
|
|
|
'%(hostname)10.10s '
|
|
|
|
'%(pid)7.7s '
|
|
|
|
'%(ppid)7.7s '
|
|
|
|
'%(pcpu)6.6s '
|
|
|
|
'%(rss)5.5s '
|
|
|
|
'%(command)20s'
|
|
|
|
)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
curses.endwin()
|
|
|
|
|
|
|
|
def paint(self):
|
2017-10-05 17:11:51 +00:00
|
|
|
self.stdscr.erase()
|
2017-10-04 19:22:42 +00:00
|
|
|
self.stdscr.addstr(0, 0, time.ctime())
|
|
|
|
|
|
|
|
all_procs = []
|
|
|
|
for host in self.hosts:
|
|
|
|
all_procs.extend(host.procs.itervalues())
|
|
|
|
|
|
|
|
all_procs.sort(key=(lambda proc: -proc.pcpu))
|
|
|
|
|
|
|
|
self.stdscr.addstr(1, 0, self.format % {
|
|
|
|
'hostname': 'HOST',
|
|
|
|
'pid': 'PID',
|
|
|
|
'ppid': 'PPID',
|
|
|
|
'pcpu': '%CPU',
|
|
|
|
'rss': 'RSS',
|
|
|
|
'command': 'COMMAND',
|
|
|
|
})
|
|
|
|
for i, proc in enumerate(all_procs):
|
|
|
|
if (i+3) >= self.height:
|
|
|
|
break
|
2017-10-05 15:55:29 +00:00
|
|
|
if proc.new:
|
|
|
|
self.stdscr.attron(curses.A_BOLD)
|
|
|
|
else:
|
|
|
|
self.stdscr.attroff(curses.A_BOLD)
|
2017-10-05 17:11:51 +00:00
|
|
|
self.stdscr.addstr(2+i, 0, self.format % dict(
|
|
|
|
vars(proc),
|
|
|
|
command=proc.command[:self.width-36]
|
|
|
|
))
|
2017-10-04 19:22:42 +00:00
|
|
|
|
|
|
|
self.stdscr.refresh()
|
|
|
|
|
|
|
|
|
|
|
|
def local_main(painter, router, select, delay):
|
|
|
|
next_paint = 0
|
|
|
|
while True:
|
|
|
|
recv, (msg, data) = select.get()
|
|
|
|
parse_output(recv.host, data)
|
|
|
|
if next_paint < time.time():
|
|
|
|
next_paint = time.time() + delay
|
|
|
|
painter.paint()
|
|
|
|
|
|
|
|
|
|
|
|
def main(router, argv):
|
|
|
|
mitogen.utils.log_to_file()
|
|
|
|
if not len(argv):
|
|
|
|
print 'mitop: Need a list of SSH hosts to connect to.'
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-10-05 15:55:29 +00:00
|
|
|
delay = 2.0
|
2017-10-04 19:22:42 +00:00
|
|
|
select = mitogen.master.Select(oneshot=False)
|
|
|
|
hosts = []
|
|
|
|
|
|
|
|
for hostname in argv:
|
|
|
|
print 'Starting on', hostname
|
|
|
|
host = Host()
|
|
|
|
host.name = hostname
|
|
|
|
if host.name == 'localhost':
|
|
|
|
host.context = router.local()
|
|
|
|
else:
|
|
|
|
host.context = router.ssh(hostname=host.name)
|
|
|
|
|
|
|
|
host.recv = mitogen.core.Receiver(router)
|
|
|
|
host.recv.host = host
|
|
|
|
select.add(host.recv)
|
|
|
|
|
|
|
|
call_recv = host.context.call_async(remote_main,
|
2017-10-04 19:34:08 +00:00
|
|
|
mitogen.context_id, host.recv.handle, delay)
|
|
|
|
|
|
|
|
# Adding call_recv to the select will cause CallError to be thrown by
|
|
|
|
# .get() if startup in the context fails, halt local_main() and cause
|
|
|
|
# the exception to be printed.
|
2017-10-04 19:22:42 +00:00
|
|
|
select.add(call_recv)
|
|
|
|
hosts.append(host)
|
|
|
|
|
|
|
|
painter = Painter(hosts)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
local_main(painter, router, select, delay)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
painter.close()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
mitogen.utils.run_with_router(main, sys.argv[1:])
|