AlertGuiImpl.py

Go to the documentation of this file.
00001 #!/usr/local/bin/python
00002 #
00003 #                               Copyright 2005
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__     = "2005/07/23 00:08:27"
00014 __updated__  = "$Date: 2005/10/13 03:12:35 $"
00015 __version__  = "$Revision: 1.6 $"
00016 __release__  = "$Name: HEAD $"
00017 __credits__  = "SLAC"
00018 
00019 import LICOS.copyright_SLAC
00020 
00021 from LICOS.tools.logger.MSGLogGUIImpl import *
00022 from LICOS.util.gOptions              import Options
00023 from LICOS.util.gAlert                import *
00024 from LICOS.tools.logger.socketLogging import *
00025 
00026 from ConfigParser                     import *
00027 from string                           import *
00028 
00029 
00030 class gAlertPopup(gAlertHandler):
00031   """!\brief Popup class for alerts.
00032 
00033   """
00034   def __init__(self, gui):
00035     """!\brief gAlertPopup constructor.
00036 
00037     \param gui Alert GUI
00038     """
00039     gAlertHandler.__init__(self)
00040     self.__gui = gui
00041 
00042   def handle(self, alertStr):
00043     """!\brief Handle the alert.
00044 
00045     \param alertStr Alert string
00046     """
00047     (severity, timestamp, machine, system, message) = alertStr.decode()
00048     alertHeader = "%s: %s System Alert, HOST:%d" % (alertDict[severity], system, machine)
00049 
00050     mb = QMessageBox( alertHeader, message, QMessageBox.Critical,
00051                       QMessageBox.Ok, QMessageBox.NoButton, QMessageBox.NoButton,
00052                       self.__gui, None, False )
00053 
00054     mb.show()
00055 
00056 class AlertGuiImpl(MSGLogGUIImpl):
00057   """!\brief Alert GUI class
00058 
00059   """
00060   def __init__(self, cfg):
00061     """!\brief AlertGuiImpl constructor
00062 
00063     \param cfg Configuration file
00064     """
00065     MSGLogGUIImpl.__init__(self)
00066     self.setCaption("LICOS alerting system")
00067     self.__registry = gAlertRegistry()
00068 
00069     self.config(cfg)
00070 
00071   def handleAlert(self, alertList):
00072     """!\brief Handle the alert.
00073 
00074     \param alertList List of alerts
00075     """
00076     tempString = join(alertList[5:],' ')
00077     alertString = gAlertStr(tempString)
00078     self.__registry.handle(alertString)
00079 
00080   def startListenerThread(self):
00081     """!\brief Start the listener thread.
00082 
00083     """
00084     self.Logger.clear()
00085     self.LogType.setText("Active Run Control Log")
00086     self.LogType.setAlignment(QLineEdit.AlignLeft)
00087 
00088     self.tcpserver = MSGLogSocketReceiver(self, port=ALARM_TCP_LOGGING_PORT)
00089     self.tcpserver.logname = self._MSGLogGUIImpl__logname
00090     self.runTCPThread = threading.Thread(target=self.runTCP, name=self.tcpserver.logname, args=(self.tcpserver,))
00091     self.runTCPThread.start()
00092 
00093 
00094   def config(self, configFile):
00095     """!\brief Parse the configuration file \a configFile
00096 
00097     \param configFile Configuration filename
00098     """
00099     parser = ConfigParser()
00100     parser.read(configFile)
00101 
00102     handlers = {}
00103     alarms   = {}
00104 
00105     # test the config for required parameters
00106     cSect    = parser.sections()
00107     REQUIRED = ('core', 'handlers', 'alarms')
00108     for sect in REQUIRED:
00109       if sect not in cSect:
00110         msg = 'Config file %s is missing required section: %s' % ( configFile, sect )
00111         raise KeyError, msg
00112 
00113     for section in cSect:
00114       options = parser.options(section)
00115       if section == "core":
00116         # core doesn't do anything ... yet
00117         pass
00118       elif section == "handlers":
00119         for option in options:
00120           handlers[option] = parser.get(section, option)
00121 
00122       elif section == "alarms":
00123         for option in options:
00124           alarms[option] = parser.get(section, option)
00125 
00126     # parse the handlers out
00127     for handler in handlers:
00128       (name, email) = handlers[handler].split(",")
00129       address = '\"%s\" <%s>' % (strip(name), strip(email))
00130       handlers[handler] = gAlertMailer(address)     # replace the instance
00131 
00132     for alarm in alarms:
00133       for handlerString in alarms[alarm].split(","):
00134         # print "adding handler %s to alarm %s" % (strip(handlerString), alarm)
00135         self.__registry.register(alarm, handlers[strip(handlerString)])
00136 
00137     popupHandler = gAlertPopup(self)
00138     self.__registry.register("ALL", popupHandler)
00139 
00140 def usage():
00141   """!\brief Return usage
00142 
00143   \return Usage string
00144   """
00145   return """
00146 
00147 AlertGuiImpl Usage:
00148 
00149 Mandatory options:
00150 --config    specify the configuration file to read
00151 
00152 Example:
00153 $ AlertGuiImpl --config alert.cfg
00154 
00155 """
00156 
00157 if __name__ == "__main__":
00158 
00159   # Options, first list mandatory, second list optional
00160   options = Options(['config'])
00161   try:
00162     options.parse()
00163   except Exception, msg:
00164     options.usage(usage())
00165     raise Exception, msg
00166 
00167   a = QApplication(sys.argv)
00168   QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()"))
00169   w = AlertGuiImpl(options.config)
00170   w.startListenerThread()
00171   a.setMainWidget(w)
00172   w.show()
00173   a.exec_loop()

Generated on Thu Apr 27 20:52:41 2006 for LICOS L02-01-00 by doxygen 1.4.6-NO