00001 #!/usr/local/bin/python 00002 # 00003 # Copyright 2002 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 System GUI" 00012 __author__ = "J. Panetta" 00013 __date__ = "28/01/2005" 00014 __version__ = "$Revision: 1.5 $" 00015 __credits__ = "SLAC" 00016 00017 import LATTE.copyright_SLAC 00018 00019 from LATTE.logger.MSGLogGUIImpl import * 00020 from LATTE.client.gOptions import Options 00021 from LATTE.tools.gAlert import * 00022 from LATTE.logger.socketLogging import * 00023 00024 from ConfigParser import * 00025 from string import * 00026 00027 00028 class gAlertPopup(gAlertHandler): 00029 def __init__(self, gui): 00030 gAlertHandler.__init__(self) 00031 self.__gui = gui 00032 00033 def handle(self, alertStr): 00034 (severity, timestamp, machine, system, message) = alertStr.decode() 00035 alertHeader = "%s: %s System Alert, HOST:%d" % (alertDict[severity], system, machine) 00036 00037 mb = QMessageBox( alertHeader, message, QMessageBox.Critical, 00038 QMessageBox.Ok, QMessageBox.NoButton, QMessageBox.NoButton, 00039 self.__gui, None, False ) 00040 00041 mb.show() 00042 00043 class AlertGuiImpl(MSGLogGUIImpl): 00044 def __init__(self, cfg): 00045 MSGLogGUIImpl.__init__(self) 00046 self.setCaption("LATTE alerting system") 00047 self.__registry = gAlertRegistry() 00048 00049 self.config(cfg) 00050 00051 def handleAlert(self, alertList): 00052 tempString = join(alertList[5:],' ') 00053 alertString = gAlertStr(tempString) 00054 self.__registry.handle(alertString) 00055 00056 def startListenerThread(self): 00057 self.Logger.clear() 00058 self.LogType.setText("Active Run Control Log") 00059 self.LogType.setAlignment(QLineEdit.AlignLeft) 00060 00061 self.tcpserver = MSGLogSocketReceiver(self, port=ALARM_TCP_LOGGING_PORT) 00062 self.tcpserver.logname = self._MSGLogGUIImpl__logname 00063 self.runTCPThread = threading.Thread(target=self.runTCP, name=self.tcpserver.logname, args=(self.tcpserver,)) 00064 self.runTCPThread.start() 00065 00066 00067 def config(self, configFile): 00068 parser = ConfigParser() 00069 parser.read(configFile) 00070 00071 handlers = {} 00072 alarms = {} 00073 00074 # test the config for required parameters 00075 cSect = parser.sections() 00076 REQUIRED = ('core', 'handlers', 'alarms') 00077 for sect in REQUIRED: 00078 if sect not in cSect: 00079 msg = 'Config file %s is missing required section: %s' % ( configFile, sect ) 00080 raise KeyError, msg 00081 00082 for section in cSect: 00083 options = parser.options(section) 00084 if section == "core": 00085 # core doesn't do anything ... yet 00086 pass 00087 elif section == "handlers": 00088 for option in options: 00089 handlers[option] = parser.get(section, option) 00090 00091 elif section == "alarms": 00092 for option in options: 00093 alarms[option] = parser.get(section, option) 00094 00095 # parse the handlers out 00096 for handler in handlers: 00097 (name, email) = handlers[handler].split(",") 00098 address = '\"%s\" <%s>' % (strip(name), strip(email)) 00099 handlers[handler] = gAlertMailer(address) # replace the instance 00100 00101 for alarm in alarms: 00102 for handlerString in alarms[alarm].split(","): 00103 # print "adding handler %s to alarm %s" % (strip(handlerString), alarm) 00104 self.__registry.register(alarm, handlers[strip(handlerString)]) 00105 00106 popupHandler = gAlertPopup(self) 00107 self.__registry.register("ALL", popupHandler) 00108 00109 def usage(): 00110 return """ 00111 00112 AlertGuiImpl Usage: 00113 00114 Mandatory options: 00115 --config specify the configuration file to read 00116 00117 Example: 00118 $ AlertGuiImpl --config alert.cfg 00119 00120 """ 00121 00122 if __name__ == "__main__": 00123 00124 # Options, first list mandatory, second list optional 00125 options = Options(['config']) 00126 try: 00127 options.parse() 00128 except Exception, msg: 00129 options.usage(usage()) 00130 raise Exception, msg 00131 00132 a = QApplication(sys.argv) 00133 QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()")) 00134 w = AlertGuiImpl(options.config) 00135 w.startListenerThread() 00136 a.setMainWidget(w) 00137 w.show() 00138 a.exec_loop()