import os import stat import time MESSAGE_NAMES = [ 'ADT', 'ORM' ] INACTIVITY_TIME = 30 WARNING_FREQUENCY = 60 # create global list variables for message timestamps: # Timestamp contains the last time a message of this type was seen # WarningTime contains the last time a warning was sent for # messages of this type try: Timestamp except NameError: # the following statements are needed to define the dictionaries Timestamp = {'ADT':0} MessageCount = {'ADT':0} WarningTime = {'ADT':0} # now that the dictionaries are defined, they can be initialized for MessageName in MESSAGE_NAMES: Timestamp[MessageName] = time.time() MessageCount[MessageName] = 0 WarningTime[MessageName] = 0 # update the timestamp for this message and increment message counter def touchMessage(MessageName): Timestamp[MessageName] = time.time() MessageCount[MessageName] += 1 def checkForInactivity(): CurrentTime = time.time() for MessageName in MESSAGE_NAMES: # check whether messages of this type have arrived recently LastTime = Timestamp[MessageName] Delta = int(CurrentTime - LastTime) if Delta > INACTIVITY_TIME: # check whether a warning message has been sent recently LastWarningTime = WarningTime[MessageName] WarningDelta = int(CurrentTime - LastWarningTime) if WarningDelta > WARNING_FREQUENCY: # send the warning message print "CHANNEL INACTIVITY WARNING:" if MessageCount[MessageName] > 0: LastString = time.strftime( '%Y-%m-%d %H:%M', time.localtime(LastTime)) print "No %s messages since %s" % (MessageName, LastString) else: print "No %s messages ever" % MessageName # update the timestamp for warning messages WarningTime[MessageName] = CurrentTime