"""A mini 'gkrellmd'-alike for any Unix machine with 'uptime' and 'uname'. Usage: on a remote host: minikrelld.py :PORT locally: gkrellm2 -s HOSTNAME --port PORT For convenience, this can also set up an ssh tunnel for you: locally: minikrelld.py 127.0.0.1:PORT SSHHOSTNAME locally: gkrellm2 -s localhost --port PORT This only serves the current time, the load average and the number of users. """ import os, socket, time, select UNAME = os.uname() UPDATE_INTERVALS = 25 SHORTEN_INTERNALS_BY = 5 try: UPDATE_INTERVALS = _UPDATE_INTERVALS except NameError: pass def format_time(): t = time.localtime() return '%d %d %d %d %d %d %d %d %d' % ( t.tm_sec, t.tm_min, t.tm_hour, t.tm_mday, t.tm_mon, t.tm_year, t.tm_wday, t.tm_yday, t.tm_isdst) def serve_client(s): banner = '' while True: buf = s.recv(1) if not buf: return if buf == '\n': break banner += buf print '*** Client banner: %r' % (banner,) assert banner.startswith('gkrellm ') s.sendall(""" gkrellmd 2.2.9 . %(hostname)s %(sysname)s %(release)s """ % { 'sysname': UNAME[0], 'hostname': UNAME[1], 'release': UNAME[2], 'time': format_time(), 'io_timeout': int(2.5 * UPDATE_INTERVALS), }) s.sendall('\n') last_value = send_update(s) s.sendall('\n') interval = float(UPDATE_INTERVALS) / SHORTEN_INTERNALS_BY count = 0 while True: iwtd = select.select([s], [], [], interval) if s in iwtd: buf = s.recv(9999) if not buf: return else: count += 1 if count == SHORTEN_INTERNALS_BY: count = 0 last_value = None last_value = send_update(s, last_value) def send_update(s, last_value=None): g = os.popen('uptime', 'r') line = g.read() g.close() parts = line.replace(',', '').replace('users', 'user').split() n_users = parts[parts.index('user')-1] assert n_users.isdigit() fload = float(parts[-3]) if last_value is not None and abs(fload - last_value) <= 0.05: return last_value s.sendall('\n0 0 0 %s %s\n' % (fload, n_users) + '