00001 #!/usr/local/bin/python
00002 #
00003 # Copyright 2004
00004 # by
00005 # The Board of Trustees of the
00006 # Leland Stanford Junior University.
00007 # All rights reserved.
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__ = "09/03/2004"
00014 __version__ = "$Revision: 1.5 $"
00015 __credits__ = "SLAC"
00016
00017 import LATTE.copyright_SLAC
00018
00019 # Python imports
00020 import logging
00021 import re
00022 import time
00023
00024 # LATTE imports
00025 import LATTE.tools.MailAlert as MailAlert
00026
00027 # set up logging
00028 logging.addLevelName(60, 'ALERT')
00029 logging.ALERT = 60
00030
00031 # add alert severity levels
00032 YELLOWALERT = 10
00033 REDALERT = 30
00034 alertDict = { YELLOWALERT:"Minor", REDALERT:"Major"}
00035
00036 def alert(message, system, severity=YELLOWALERT, machine=0):
00037
00038 alertStr = gAlertStr()
00039
00040 logging.log(logging.ALERT, alertStr.encode(severity, time.time(), system, message, machine))
00041
00042
00043 class gAlertHandler(object):
00044 def __init__(self):
00045 pass
00046
00047 def handle(self, alertStr):
00048 """ Method to handle an alert
00049 \param alertStr a gAlertStr object
00050 """
00051 raise NotImplementedError, "call to abstract base class gAlertHandler::handle()"
00052
00053 class gAlertMailer(gAlertHandler):
00054 def __init__(self, recipient):
00055 gAlertHandler.__init__(self)
00056 self.__recipient = recipient
00057 self.__sender = "glast@slac.stanford.edu" # add text for glast alarm acct <...>
00058 self.__smtp = MailAlert.SLACmail()
00059
00060 def handle(self, alertStr):
00061 (severity, timestamp, machine, system, message) = alertStr.decode()
00062
00063 # construct email object
00064 subject = "[LAT] %s %s %s HOST:%d" % \
00065 ( system, alertDict[severity], time.asctime(time.gmtime(timestamp)), machine )
00066 eml = gAlertMail(self.__recipient, subject, message)
00067 # print "sending email to %s, subj=%s, msg=%s" % (self.__recipient, subject, message)
00068 self.__smtp.sendMail(eml)
00069
00070 class gAlertStr(object):
00071 """Parser/unparser/container for alert strings
00072 """
00073 def __init__(self, alertStr=None):
00074 self.__alertFmt = "%04d-%f-%d-%s-%s"
00075 self.__alertRe = re.compile("(\d\d\d\d)-([\d\.]+)-([\d]+)-([^-]+)-(.*)")
00076 self.__string = alertStr
00077
00078 def encode(self, severity, timestamp, system, message, machine=0):
00079 """Method to encode an alert string for dispatch to the message logger
00080 \param severity Severity level (YELLOWALERT or REDALERT)
00081 \param time Time alert was sent
00082 \param system System alert was sent from
00083 \param message Alert message
00084 \param machine Machine ID alert is sent from
00085 """
00086 self.__string = self.__alertFmt % (severity, timestamp, machine, system, message)
00087 return self.__string
00088
00089 def decode(self, string=None):
00090 """Method to decode an alert string dispatched to the message logger
00091 \return (severity, time, system, message)
00092 """
00093 if string==None:
00094 string = self.__string
00095 dec = self.__alertRe.split(string)
00096 # print string, dec
00097 severity = int(dec[1])
00098 timestamp = float(dec[2])
00099 machine = int(dec[3])
00100 system = str(dec[4])
00101 message = str(dec[5])
00102 return (severity, timestamp, machine, system, message)
00103
00104
00105 class gAlertMail(MailAlert.Email):
00106 def __init__(self, recip, subject, message):
00107 MailAlert.Email.__init__(self)
00108 self.sender("glast@slac.stanford.edu") # add text for glast alarm acct <...>
00109 self.recipient(recip)
00110 self.body("Subject: %s " % subject + "\r\n" + message)
00111
00112
00113 class gAlertRegistry(object):
00114 """Alert registry class
00115 """
00116 def __init__(self):
00117 self.__handlers = []
00118 self.__dispatchMap = {"ALL":[]}
00119 pass
00120
00121 def register(self, system, handler):
00122 """register method
00123 \param handler
00124 \param system
00125 """
00126 if handler not in self.__handlers:
00127 self.__handlers.append(handler)
00128 system = system.upper()
00129 if system not in self.__dispatchMap.keys():
00130 self.__dispatchMap[system] = []
00131 self.__dispatchMap[system].append(handler)
00132
00133 def unregister(self, handler, categories):
00134 pass
00135
00136 def handle(self, alertStr):
00137 (severity, timestamp, machine, system, message) = alertStr.decode() # just to get system
00138
00139 system = system.upper()
00140 # if system not in self.__dispatchMap.keys():
00141 # print "system \'%s\' not in keys" % system, self.__dispatchMap.keys()
00142 # raise KeyError
00143
00144 # deal with "ALL" and appending handlers
00145 handleList = self.__dispatchMap["ALL"]
00146 if system in self.__dispatchMap.keys():
00147 for handler in self.__dispatchMap[system]:
00148 if handler not in handleList:
00149 handleList.append(handler)
00150
00151 for handler in handleList:
00152 handler.handle(alertStr)
00153
00154
00155 if __name__ == "__main__":
00156
00157 # create handlers
00158 h1 = gAlertMailer("panetta@slac.stanford.edu")
00159 # h1 = gAlertMailer("claus@slac.stanford.edu")
00160 h2 = gAlertMailer("6508499455@myairmail.com")
00161
00162 h1set = ['CAL', 'LATTE', 'HSK']
00163 h1list = []
00164 for s in h1set:
00165 h1list.append("%8s"%s)
00166
00167 registry = gAlertRegistry()
00168 # registry.register(h1, h1list)
00169 registry.register(h1, h1list)
00170
00171 alertStr = gAlertStr()
00172 alertStr.encode(YELLOWALERT, time.time(), "LATTE", "Ah, I just figured out how to set a subject", 0)
00173
00174 registry.handle(alertStr)
00175
00176
00177 #EOF