import os import stat import time # create global 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 def startMessageChecking(paramMessageNames): global MessageNames global Timestamp global MessageCount global WarningTime MessageNames = paramMessageNames try: Timestamp except NameError: # the following statements are needed to define the dictionaries; # otherwise, Python will flag this as an error Timestamp = {} MessageCount = {} WarningTime = {} # now that the dictionaries are defined, they can be initialized for MessageName in MessageNames: Timestamp[MessageName] = time.time() MessageCount[MessageName] = 0 WarningTime[MessageName] = 0 # update the timestamp for this message and increment message counter def touchMessage(MessageName): global Timestamp global MessageCount Timestamp[MessageName] = time.time() MessageCount[MessageName] += 1 # check each message type to see whether there is message activity def checkForInactivity(InactivityTime, WarningFrequency): global MessageNames global Timestamp global MessageCount global WarningTime CurrentTime = time.time() for MessageName in MessageNames: # check whether messages of this type have arrived recently LastTime = Timestamp[MessageName] Delta = int(CurrentTime - LastTime) if Delta > InactivityTime: # no message of this type has arrived recently; channel # inactivity has been detected # # check whether a warning message has been sent recently LastWarningTime = WarningTime[MessageName] WarningDelta = int(CurrentTime - LastWarningTime) if WarningDelta > WarningFrequency: # no warning message has been sent recently, so send one 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