2018-03-30 06:56:53 +00:00
|
|
|
"""
|
|
|
|
mitop.py is a version of the UNIX top command that knows how to display process
|
|
|
|
lists from multiple machines in a single listing.
|
|
|
|
|
|
|
|
This is a basic, initial version showing overall program layout. A future
|
|
|
|
version will extend it to:
|
|
|
|
|
|
|
|
* Only notify the master of changed processes, rather than all processes.
|
|
|
|
* Runtime-reconfigurable filters and aggregations handled on the remote
|
|
|
|
machines rather than forcing a bottleneck in the master.
|
|
|
|
|
|
|
|
"""
|
2017-10-04 19:22:42 +00:00
|
|
|
|
|
|
|
import curses
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
import mitogen.core
|
|
|
|
import mitogen.master
|
2018-05-25 03:47:59 +00:00
|
|
|
import mitogen.select
|
2017-10-04 19:22:42 +00:00
|
|
|
import mitogen.utils
|
|
|
|
|
|
|
|
|
|
|
|
class Host(object):
|
2018-03-30 06:50:41 +00:00
|
|
|
"""
|
|
|
|
A target host from the perspective of the master process.
|
|
|
|
"""
|
|
|
|
#: String hostname.
|
2017-10-04 19:22:42 +00:00
|
|
|
name = None
|
2018-03-30 06:50:41 +00:00
|
|
|
|
|
|
|
#: mitogen.parent.Context used to call functions on the host.
|
2017-10-04 19:22:42 +00:00
|
|
|
context = None
|
2018-03-30 06:50:41 +00:00
|
|
|
|
|
|
|
#: mitogen.core.Receiver the target delivers state updates to.
|
2017-10-04 19:22:42 +00:00
|
|
|
recv = None
|
|
|
|
|
|
|
|
def __init__(self):
|
2018-03-30 06:50:41 +00:00
|
|
|
#: Mapping of pid -> Process() for each process described
|
|
|
|
#: in the host's previous status update.
|
|
|
|
self.procs = {}
|
2017-10-04 19:22:42 +00:00
|
|
|
|
2017-10-04 19:29:14 +00:00
|
|
|
|
2017-10-04 19:22:42 +00:00
|
|
|
class Process(object):
|
2018-03-30 06:50:41 +00:00
|
|
|
"""
|
|
|
|
A single process running on a target host.
|
|
|
|
"""
|
2017-10-04 19:22:42 +00:00
|
|
|
host = None
|
|
|
|
user = None
|
|
|
|
pid = None
|
|
|
|
ppid = None
|
|
|
|
pgid = None
|
|
|
|
command = None
|
|
|
|
rss = None
|
|
|
|
pcpu = None
|
|
|
|
rss = None
|
|
|
|
|
|
|
|
|
2018-03-30 06:50:41 +00:00
|
|
|
def child_main(sender, delay):
|
|
|
|
"""
|
2018-03-30 07:03:26 +00:00
|
|
|
Executed on the main thread of the Python interpreter running on each
|
|
|
|
target machine, Context.call() from the master. It simply sends the output
|
|
|
|
of the UNIX 'ps' command at regular intervals toward a Receiver on master.
|
2018-03-30 06:50:41 +00:00
|
|
|
|
|
|
|
:param mitogen.core.Sender sender:
|
|
|
|
The Sender to use for delivering our result. This could target
|
|
|
|
anywhere, but the sender supplied by the master simply causes results
|
|
|
|
to be delivered to the master's associated per-host Receiver.
|
|
|
|
"""
|
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:
|
2018-07-09 16:01:28 +00:00
|
|
|
sender.send(subprocess.check_output(args).decode())
|
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):
|
2018-03-30 07:03:26 +00:00
|
|
|
"""
|
|
|
|
This is ncurses (screen drawing) magic, you can ignore it. :)
|
|
|
|
"""
|
2017-10-04 19:22:42 +00:00
|
|
|
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:
|
2018-07-09 16:01:28 +00:00
|
|
|
all_procs.extend(host.procs.values())
|
2017-10-04 19:22:42 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
2018-03-30 06:50:41 +00:00
|
|
|
def master_main(painter, router, select, delay):
|
|
|
|
"""
|
|
|
|
Loop until CTRL+C is pressed, waiting for the next result delivered by the
|
|
|
|
Select. Use parse_output() to turn that result ('ps' command output) into
|
|
|
|
rich data, and finally repaint the screen if the repaint delay has passed.
|
|
|
|
"""
|
2017-10-04 19:22:42 +00:00
|
|
|
next_paint = 0
|
|
|
|
while True:
|
2018-02-14 20:42:31 +00:00
|
|
|
msg = select.get()
|
|
|
|
parse_output(msg.receiver.host, msg.unpickle())
|
2017-10-04 19:22:42 +00:00
|
|
|
if next_paint < time.time():
|
|
|
|
next_paint = time.time() + delay
|
|
|
|
painter.paint()
|
|
|
|
|
|
|
|
|
2018-03-30 06:50:41 +00:00
|
|
|
@mitogen.main()
|
|
|
|
def main(router):
|
|
|
|
"""
|
|
|
|
Main program entry point. @mitogen.main() is just a helper to handle
|
|
|
|
reliable setup/destruction of Broker, Router and the logging package.
|
|
|
|
"""
|
|
|
|
argv = sys.argv[1:]
|
2017-10-04 19:22:42 +00:00
|
|
|
if not len(argv):
|
2018-04-17 16:40:45 +00:00
|
|
|
print('mitop: Need a list of SSH hosts to connect to.')
|
2017-10-04 19:22:42 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2017-10-05 15:55:29 +00:00
|
|
|
delay = 2.0
|
2018-05-25 03:47:59 +00:00
|
|
|
select = mitogen.select.Select(oneshot=False)
|
2017-10-04 19:22:42 +00:00
|
|
|
hosts = []
|
|
|
|
|
2018-03-30 06:50:41 +00:00
|
|
|
# For each hostname on the command line, create a Host instance, a Mitogen
|
|
|
|
# connection, a Receiver to accept messages from the host, and finally
|
|
|
|
# start child_main() on the host to pump messages into the receiver.
|
2017-10-04 19:22:42 +00:00
|
|
|
for hostname in argv:
|
2018-04-17 16:40:45 +00:00
|
|
|
print('Starting on', hostname)
|
2017-10-04 19:22:42 +00:00
|
|
|
host = Host()
|
|
|
|
host.name = hostname
|
2018-03-30 06:50:41 +00:00
|
|
|
|
2017-10-04 19:22:42 +00:00
|
|
|
if host.name == 'localhost':
|
|
|
|
host.context = router.local()
|
|
|
|
else:
|
|
|
|
host.context = router.ssh(hostname=host.name)
|
|
|
|
|
2018-03-30 06:50:41 +00:00
|
|
|
# A receiver wires up a handle (via Router.add_handler()) to an
|
|
|
|
# internal thread-safe queue object, which can be drained through calls
|
|
|
|
# to recv.get().
|
2017-10-04 19:22:42 +00:00
|
|
|
host.recv = mitogen.core.Receiver(router)
|
|
|
|
host.recv.host = host
|
2018-03-30 06:50:41 +00:00
|
|
|
|
|
|
|
# But we don't want to receive data from just one receiver, we want to
|
|
|
|
# receive data from many. In this case we can use a Select(). It knows
|
|
|
|
# how to efficiently sleep while waiting for the first message sent to
|
|
|
|
# many receivers.
|
2017-10-04 19:22:42 +00:00
|
|
|
select.add(host.recv)
|
|
|
|
|
2018-03-30 06:50:41 +00:00
|
|
|
# The inverse of a Receiver is a Sender. Unlike receivers, senders are
|
|
|
|
# serializable, so we can call the .to_sender() helper method to create
|
|
|
|
# one equivalent to our host's receiver, and pass it directly to the
|
|
|
|
# host as a function parameter.
|
|
|
|
sender = host.recv.to_sender()
|
|
|
|
|
|
|
|
# Finally invoke the function in the remote target. Since child_main()
|
|
|
|
# is an infinite loop, using .call() would block the parent, since
|
|
|
|
# child_main() never returns. Instead use .call_async(), which returns
|
2018-03-30 07:03:26 +00:00
|
|
|
# another Receiver. We also want to wait for results from it --
|
|
|
|
# although child_main() never returns, if it crashes the exception will
|
|
|
|
# be delivered instead.
|
2018-03-30 06:50:41 +00:00
|
|
|
call_recv = host.context.call_async(child_main, sender, delay)
|
|
|
|
call_recv.host = host
|
2017-10-04 19:34:08 +00:00
|
|
|
|
2018-03-30 07:03:26 +00:00
|
|
|
# Adding call_recv to the select will cause mitogen.core.CallError to
|
|
|
|
# be thrown by .get() if startup of any context fails, causing halt of
|
|
|
|
# master_main(), and the exception to be printed.
|
2017-10-04 19:22:42 +00:00
|
|
|
select.add(call_recv)
|
|
|
|
hosts.append(host)
|
|
|
|
|
2018-03-30 06:50:41 +00:00
|
|
|
# Painter just wraps up all the prehistory ncurses code and keeps it out of
|
|
|
|
# master_main().
|
2017-10-04 19:22:42 +00:00
|
|
|
painter = Painter(hosts)
|
|
|
|
try:
|
|
|
|
try:
|
2018-03-30 06:50:41 +00:00
|
|
|
master_main(painter, router, select, delay)
|
2017-10-04 19:22:42 +00:00
|
|
|
except KeyboardInterrupt:
|
2018-03-30 06:50:41 +00:00
|
|
|
# Shut down gracefully when the user presses CTRL+C.
|
2017-10-04 19:22:42 +00:00
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
painter.close()
|