#!/usr/bin/env python import sys, os, commands, string, getopt, glob, time def ts(): rt = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime( time.time() )) rt = rt + " = " return rt # period the period of time in second which you want to check whether the file is out of date # file the file you want to check # cmd when the file is out of date, execute this cmd # timeout the time which you think the file is out of date # lines the lines in the end of the file you want to print try: optlist, args = getopt.getopt(sys.argv[1:], 'p:f:c:t:l:g:', ['period=', 'file=', 'cmd=', 'timeout=', 'lines=', 'globalTimeout=' ]) except getopt.GetoptError, e: print 'WatchDog: argument error' sys.stdout.flush() sys.exit(1) period = 120 file = '' cmd = '' timeout = 0 globalTimeout = 1000000 # sec allowed to run in total lines = 30 for key, val in optlist: if key == '--file': file = val elif key == '-f': file = val elif key == '--period': period = int(val) elif key == '-p': period = int(val) elif key == '--timeout': timeout = int(val) elif key == '-t': timeout = int(val) elif key == '--globalTimeout': globalTimeout = int(val) elif key == '-g': globalTimeout = int(val) elif key == '--cmd': cmd = val elif key == '-c': cmd = val elif key == '--lines': lines = int(val) elif key == '-l': lines = int(val) if file == '' or os.path.exists( file ) == 0: print ts() + 'WatchDog: no file assigned or file is not existent.' sys.stdout.flush() sys.exit(1) if timeout == 0: print ts() + 'WatchDog: no timeout assigned.' sys.stdout.flush() sys.exit(1) if cmd == '': print ts() + 'WatchDog: no cmd assigned.' sys.stdout.flush() sys.exit(1) pid = os.fork() # parent if pid != 0: print ts() + 'WatchDog: fork a child process %d to monitor %s' % (pid, file) print ts() + 'WatchDog parameters:' print ' period = ',period print ' cmd = ',cmd print ' timeout = ',timeout print ' globalTimeout = ',globalTimeout print ' lines = ',lines #os.system( 'echo %d > WatchDog.pid' % pid ) sys.stdout.flush() sys.exit(0) # child pid = os.getpid() print ts() + 'WatchDog: Child process %d is monitoring %s' % (pid, file) globalT0 = int( time.time() ) while 1: # file is gone if os.path.exists( file ) == 0: break stat = os.stat( file ) LastUpdateTime = stat[8] now = int( time.time() ) dt0 = now - LastUpdateTime dt1 = now - globalT0 if ( dt0 > timeout or dt1 > globalTimeout ) : c = 'tail -n %d %s' % (lines, file) s, r = commands.getstatusoutput( c ) print ts() + '> %s' % c print ts() + '%s' % r print '' if ( dt0 > timeout ) : LastUpdateTime = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime( LastUpdateTime )) print ts() + 'WatchDog: File %s is not updated more than %d since %s' % (file, timeout, LastUpdateTime) else: nowStr = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime( now ) ) sttStr = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime( globalT0 ) ) print ts() + 'WatchDog: Exceeded global timeout of %d s, start=%s, now=%s' % (globalTimeout, sttStr ,nowStr) s, r = commands.getstatusoutput( cmd ) print ts() + '> %s' % cmd print ts() + '%s' % r print '' sys.stdout.flush() break os.system( 'sleep %d' % period ) sys.stdout.flush() sys.exit(0)