#!/usr/bin/python import time, os, sys, socket, threading, ConfigParser monitored_file = '/home/storm/test.py' logs = ['/home/storm/xml'] monitor_log = '/home/storm/monitor.log' pidfile = '/home/storm/monitor.pid' statfile = '/home/storm/stats.mon' timer = time.time() class myThread(threading.Thread): def run(self): mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) mySocket.bind ( ( '', 2727 ) ) mySocket.listen ( 1 ) while True: channel, details = mySocket.accept() command = channel.recv (100).lower().strip() if command == 'kill': mySocket.close() os.system('kill -9 ' + open(pidfile, 'r').read().strip()) elif command == 'uptime': channel.send ('uptime is :' + str(round(time.time() - timer, 2)) + ' seconds\n') elif command == 'stats': c = ConfigParser.ConfigParser() c.readfp(open(statfile)) #channel.send ('uptime is :' + str(round(time.time() - timer, 2)) + ' seconds\n') channel.send ('Current status:\n - Crashed:' + str(c.getint('general','crashes')) + '\n - Resets: ' + str(c.getint('general','resets')) + '\n - Uptime: ' + str(round(time.time() - timer, 2)) + ' seconds\n') channel.close() def doubleFork(): # do the UNIX double-fork magic, see Stevens' "Advanced # Programming in the UNIX Environment" for details (ISBN 0201563177) try: pid = os.fork() if pid > 0: # exit first parent sys.exit(0) except OSError, e: print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror) sys.exit(1) # decouple from parent environment os.chdir("/") os.setsid() os.umask(0) # do second fork try: pid = os.fork() if pid > 0: # exit from second parent, print eventual PID before pfile = open(pidfile, 'w') pfile.write(str(pid)) pfile.close() sys.exit(0) except OSError, e: print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror) sys.exit(1) if os.path.exists(monitored_file): logfile_time = os.path.getmtime(monitored_file) else: logfile_time = -1 doubleFork() myThread().start() config = ConfigParser.ConfigParser() config.add_section('general') config.set('general','resets',0) config.set('general','crashes',0) config.write(open(statfile, 'w')) while (1): if os.path.exists(monitored_file): if logfile_time < os.path.getmtime(monitored_file) and os.path.getsize(monitored_file) < 5000: print 'crashed' logfile_time = os.path.getmtime(monitored_file) fmonitor = open(monitor_log, 'a') fmonitor.write('') fmonitor.write(time.ctime()) for f in logs: fmonitor.write(f + ':\n' + os.popen('tail -20 ' + f).read()) c = ConfigParser.ConfigParser() c.readfp(open(statfile)) config.set('general','resets', c.getint('general','resets') + 1) config.set('general','crashes',c.getint('general','crashes') + 1) config.write(open(statfile, 'w')) elif logfile_time < os.path.getmtime(monitored_file): logfile_time = os.path.getmtime(monitored_file) print 'restarted' c = ConfigParser.ConfigParser() c.readfp(open(statfile)) config.set('general','resets', c.getint('general','resets') + 1) config.write(open(statfile, 'w')) else: print 'File ' + monitored_file + ' missing!' time.sleep (10)