00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 __facility__ = "Online"
00011 __abstract__ = "GLAST LAT alert email tool"
00012 __author__ = "Jim Panetta <panetta@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__ = "2004/03/09 00:00:00"
00014 __updated__ = "$Date: 2005/09/22 00:29:31 $"
00015 __version__ = "$Revision: 1.5 $"
00016 __release__ = "$Name: HEAD $"
00017 __credits__ = "SLAC"
00018
00019 import LICOS.copyright_SLAC
00020
00021
00022 import logging
00023 import re
00024 import time
00025
00026
00027 import MailAlert
00028
00029
00030 logging.addLevelName(60, 'ALERT')
00031 logging.ALERT = 60
00032
00033
00034 YELLOWALERT = 10
00035 REDALERT = 30
00036 alertDict = { YELLOWALERT:"Minor", REDALERT:"Major"}
00037
00038 def alert(message, system, severity=YELLOWALERT, machine=0):
00039
00040 alertStr = gAlertStr()
00041
00042 logging.log(logging.ALERT, alertStr.encode(severity, time.time(), system, message, machine))
00043
00044
00045 class gAlertHandler(object):
00046 def __init__(self):
00047 pass
00048
00049 def handle(self, alertStr):
00050 """!\brief Method to handle an alert.
00051
00052 \param alertStr a gAlertStr object
00053 """
00054 raise NotImplementedError, "call to abstract base class gAlertHandler::handle()"
00055
00056 class gAlertMailer(gAlertHandler):
00057 def __init__(self, recipient):
00058 gAlertHandler.__init__(self)
00059 self.__recipient = recipient
00060 self.__sender = "glast@slac.stanford.edu"
00061 self.__smtp = MailAlert.SLACmail()
00062
00063 def handle(self, alertStr):
00064 (severity, timestamp, machine, system, message) = alertStr.decode()
00065
00066
00067 subject = "[LAT] %s %s %s HOST:%d" % \
00068 ( system, alertDict[severity], time.asctime(time.gmtime(timestamp)), machine )
00069 eml = gAlertMail(self.__recipient, subject, message)
00070
00071 self.__smtp.sendMail(eml)
00072
00073 class gAlertStr(object):
00074 """!\brief Parser/unparser/container for alert strings.
00075
00076 """
00077 def __init__(self, alertStr=None):
00078 self.__alertFmt = "%04d-%f-%d-%s-%s"
00079 self.__alertRe = re.compile("(\d\d\d\d)-([\d\.]+)-([\d]+)-([^-]+)-(.*)")
00080 self.__string = alertStr
00081
00082 def encode(self, severity, timestamp, system, message, machine=0):
00083 """!\brief Method to encode an alert string for dispatch to the message logger.
00084
00085 \param severity Severity level (YELLOWALERT or REDALERT)
00086 \param time Time alert was sent
00087 \param system System alert was sent from
00088 \param message Alert message
00089 \param machine Machine ID alert is sent from
00090 """
00091 self.__string = self.__alertFmt % (severity, timestamp, machine, system, message)
00092 return self.__string
00093
00094 def decode(self, string=None):
00095 """!\brief Method to decode an alert string dispatched to the message logger.
00096
00097 \param string String to be decoded
00098
00099 \return (severity, time, system, message)
00100 """
00101 if string==None:
00102 string = self.__string
00103 dec = self.__alertRe.split(string)
00104
00105 severity = int(dec[1])
00106 timestamp = float(dec[2])
00107 machine = int(dec[3])
00108 system = str(dec[4])
00109 message = str(dec[5])
00110 return (severity, timestamp, machine, system, message)
00111
00112
00113 class gAlertMail(MailAlert.Email):
00114 def __init__(self, recip, subject, message):
00115 MailAlert.Email.__init__(self)
00116 self.sender("glast@slac.stanford.edu")
00117 self.recipient(recip)
00118 self.body("Subject: %s " % subject + "\r\n" + message)
00119
00120
00121 class gAlertRegistry(object):
00122 """!\brief Alert registry class.
00123
00124 """
00125 def __init__(self):
00126 self.__handlers = []
00127 self.__dispatchMap = {"ALL":[]}
00128 pass
00129
00130 def register(self, system, handler):
00131 """!\brief register method.
00132
00133 \param handler
00134 \param system
00135 """
00136 if handler not in self.__handlers:
00137 self.__handlers.append(handler)
00138 system = system.upper()
00139 if system not in self.__dispatchMap.keys():
00140 self.__dispatchMap[system] = []
00141 self.__dispatchMap[system].append(handler)
00142
00143 def unregister(self, handler, categories):
00144 pass
00145
00146 def handle(self, alertStr):
00147 (severity, timestamp, machine, system, message) = alertStr.decode()
00148
00149 system = system.upper()
00150
00151
00152
00153
00154
00155 handleList = self.__dispatchMap["ALL"]
00156 if system in self.__dispatchMap.keys():
00157 for handler in self.__dispatchMap[system]:
00158 if handler not in handleList:
00159 handleList.append(handler)
00160
00161 for handler in handleList:
00162 handler.handle(alertStr)
00163
00164
00165 if __name__ == "__main__":
00166
00167
00168 h1 = gAlertMailer("panetta@slac.stanford.edu")
00169
00170 h2 = gAlertMailer("6508499455@myairmail.com")
00171
00172 h1set = ['CAL', 'LATTE', 'HSK']
00173 h1list = []
00174 for s in h1set:
00175 h1list.append("%8s"%s)
00176
00177 registry = gAlertRegistry()
00178
00179 registry.register(h1, h1list)
00180
00181 alertStr = gAlertStr()
00182 alertStr.encode(YELLOWALERT, time.time(), "LATTE", "Ah, I just figured out how to set a subject", 0)
00183
00184 registry.handle(alertStr)
00185
00186
00187