Revision: 1456 http://trac.macosforge.org/projects/calendarserver/changeset/1456 Author: cdaboo@apple.com Date: 2007-04-06 08:29:20 -0700 (Fri, 06 Apr 2007) Log Message: ----------- Minor refactoring and clean-up of termination process. Modified Paths: -------------- CalDAVTester/trunk/monitor.py CalDAVTester/trunk/scripts/monitoring/monitorinfo.dtd CalDAVTester/trunk/scripts/monitoring/monitorinfo.xml CalDAVTester/trunk/src/monitorinfo.py CalDAVTester/trunk/src/xmlDefs.py Modified: CalDAVTester/trunk/monitor.py =================================================================== --- CalDAVTester/trunk/monitor.py 2007-04-05 21:21:47 UTC (rev 1455) +++ CalDAVTester/trunk/monitor.py 2007-04-06 15:29:20 UTC (rev 1456) @@ -35,108 +35,139 @@ EX_INVALID_CONFIG_FILE = "Invalid Config File" -if __name__ == "__main__": +class monitor(object): - monitorinfoname = "scripts/monitoring/monitorinfo.xml" - if len(sys.argv) > 1: - monitorinfoname = sys.argv[1] - - if len(sys.argv) > 2: - monitorlogname = sys.argv[2] - else: - monitorlogname = None + def __init__(self, infoname, logname, user, pswd): + self.infoname = infoname + self.user = user + self.pswd = pswd + self.minfo = None - def readXML(): + if logname: + self.log = open(logname, "a") + else: + self.log = None + + self.running = True + def readXML(self): + # Open and parse the server config file - fd = open(monitorinfoname, "r") + fd = open(self.infoname, "r") doc = xml.dom.minidom.parse( fd ) fd.close() # Verify that top-level element is correct - monitorinfoname_node = doc._get_documentElement() - if monitorinfoname_node._get_localName() != src.xmlDefs.ELEMENT_MONITORINFO: + node = doc._get_documentElement() + if node._get_localName() != src.xmlDefs.ELEMENT_MONITORINFO: raise EX_INVALID_CONFIG_FILE - if not monitorinfoname_node.hasChildNodes(): + if not node.hasChildNodes(): raise EX_INVALID_CONFIG_FILE - minfo = monitorinfo() - minfo.parseXML(monitorinfoname_node) - return minfo + self.minfo = monitorinfo() + self.minfo.parseXML(node) - user = raw_input("User: ") - pswd = getpass("Password: ") - - minfo = readXML() - - def doScript(script): + def doScript(self, script): mgr = manager(level=manager.LOG_NONE) - return mgr.runWithOptions(minfo.serverinfo, "", [script,], {"$userid1:":user, "$pswd1:":pswd, "$principal:":"/principals/users/%s/"%(user,)}) + return mgr.runWithOptions( + self.minfo.serverinfo, + "", + [script,], + { + "$userid1:" : self.user, + "$pswd1:" : self.pswd, + "$principal:": "/principals/users/%s/" % (self.user,) + } + ) - def doStart(): - if minfo.startscript: - print "Runnning start script %s" % (minfo.startscript,) - doScript(minfo.startscript) + def doStart(self): + self.logtxt("Starting Monitor") - def doEnd(sig, frame): - if minfo.endscript: - print "Runnning end script %s" % (minfo.endscript,) - doScript(minfo.endscript) - sys.exit() + if self.minfo.startscript: + self.logtxt("Runnning start script %s" % (self.minfo.startscript,)) + self.doScript(self.minfo.startscript) - def doNotification(msg): + def doEnd(self): + if self.minfo.endscript: + self.logtxt("Runnning end script %s" % (self.minfo.endscript,)) + self.doScript(self.minfo.endscript) + + self.logtxt("Stopped Monitor") + self.running = False + + def doError(self, msg): + self.logtxt("Run exception: %s" % (msg,)) + + def doNotification(self, msg): sendemail( - fromaddr = ("Do Not Reply", "icalbridge-alert@apple.com"), - toaddrs = [("", a) for a in minfo.notify], - subject = minfo.notify_subject, - body = minfo.notify_body % (msg,), + fromaddr = ("Do Not Reply", self.minfo.notify_from), + toaddrs = [("", a) for a in self.minfo.notify], + subject = self.minfo.notify_subject, + body = self.minfo.notify_body % (msg,), ) - signal.signal(signal.SIGINT, doEnd) + def logtxt(self, txt): + dt = str(datetime.datetime.now()) + dt = dt[0:dt.rfind(".")] + if self.log: + self.log.write("[%s] %s\n" % (dt, txt,)) + self.log.flush() + else: + print "[%s] %s" % (dt, txt,) + + def runLoop(self): + last_notify = 0 + while(self.running): + time.sleep(self.minfo.period) + if not self.running: + break + result, timing = m.doScript(self.minfo.testinfo) + if not self.running: + break + if self.minfo.logging: + self.logtxt("Result: %d, Timing: %.3f" % (result, timing,)) + if timing >= self.minfo.warningtime: + msg = "WARNING: request time (%.3f) exceeds limit (%.3f)" % (timing, self.minfo.warningtime,) + self.logtxt(msg) + if self.minfo.notify_time_exceeded and (time.time() - last_notify > self.minfo.notify_interval * 60): + self.logtxt("Sending notification to %s" % (self.minfo.notify,)) + self.doNotification(msg) + last_notify = time.time() + if result != 0: + msg = "WARNING: request failed" + self.logtxt(msg) + if self.minfo.notify_request_failed and (time.time() - last_notify > self.minfo.notify_interval * 60): + self.logtxt("Sending notification to %s" % (self.minfo.notify,)) + self.doNotification(msg) + last_notify = time.time() - if monitorlogname: - log = open(monitorlogname, "a") +if __name__ == "__main__": + + infoname = "scripts/monitoring/monitorinfo.xml" + if len(sys.argv) > 1: + infoname = sys.argv[1] + + if len(sys.argv) > 2: + logname = sys.argv[2] else: - log = None + logname = None - def logtxt(txt): - if log: - log.write("%s\n" % (txt,)) - else: - print txt + user = raw_input("User: ") + pswd = getpass("Password: ") + + m = monitor(infoname, logname, user, pswd) + m.readXML() - doStart() + def signalEnd(sig, frame): + m.doEnd() + sys.exit() - if minfo.logging: - logtxt("Start:") + signal.signal(signal.SIGINT, signalEnd) + + m.doStart() + try: - last_notify = 0 - while(True): - time.sleep(minfo.period) - result, timing = doScript(minfo.testinfo) - if minfo.logging: - logtxt("Result: %d, Timing: %.3f" % (result, timing,)) - if timing >= minfo.warningtime: - dt = str(datetime.datetime.now()) - dt = dt[0:dt.rfind(".")] - msg = "[%s] WARNING: request time (%.3f) exceeds limit (%.3f)" % (dt, timing, minfo.warningtime,) - logtxt(msg) - if minfo.notify_time_exceeded and (time.time() - last_notify > minfo.notify_interval * 60): - logtxt("Sending notification to %s" % (minfo.notify,)) - doNotification(msg) - last_notify = time.time() - if result != 0: - dt = str(datetime.datetime.now()) - dt = dt[0:dt.rfind(".")] - msg = "[%s] WARNING: request failed" % (dt,) - logtxt(msg) - if minfo.notify_request_failed and (time.time() - last_notify > minfo.notify_interval * 60): - logtxt("Sending notification to %s" % (minfo.notify,)) - doNotification(msg) - last_notify = time.time() - - if minfo.logging: - logtxt("Done") + m.runLoop() except SystemExit: pass except Exception, e: - log.write("Run exception: %s" % (str(e),)) + m.doError(str(e)) Modified: CalDAVTester/trunk/scripts/monitoring/monitorinfo.dtd =================================================================== --- CalDAVTester/trunk/scripts/monitoring/monitorinfo.dtd 2007-04-05 21:21:47 UTC (rev 1455) +++ CalDAVTester/trunk/scripts/monitoring/monitorinfo.dtd 2007-04-06 15:29:20 UTC (rev 1456) @@ -33,11 +33,12 @@ <!ELEMENT warningtime (#PCDATA)> - <!ELEMENT notify (mailto+, subject, body)> + <!ELEMENT notify (mailfrom, mailto+, subject, body)> <!ATTLIST notify time-exceeded (yes|no) "no" request-failed (yes|no) "yes" interval CDATA "15"> + <!ELEMENT mailfrom (#PCDATA)> <!ELEMENT mailto (#PCDATA)> <!ELEMENT subject (#PCDATA)> <!ELEMENT body (#PCDATA)> Modified: CalDAVTester/trunk/scripts/monitoring/monitorinfo.xml =================================================================== --- CalDAVTester/trunk/scripts/monitoring/monitorinfo.xml 2007-04-05 21:21:47 UTC (rev 1455) +++ CalDAVTester/trunk/scripts/monitoring/monitorinfo.xml 2007-04-06 15:29:20 UTC (rev 1456) @@ -29,6 +29,7 @@ <end>monitor/get/get-end.xml</end> <warningtime>1.0</warningtime> <notify time-exceeded="no" request-failed="yes"> + <mailfrom>test@example.com</mailfrom> <mailto>test@example.com</mailto> <subject>Calendar Server error</subject> <body><![CDATA[Hi, Modified: CalDAVTester/trunk/src/monitorinfo.py =================================================================== --- CalDAVTester/trunk/src/monitorinfo.py 2007-04-05 21:21:47 UTC (rev 1455) +++ CalDAVTester/trunk/src/monitorinfo.py 2007-04-06 15:29:20 UTC (rev 1456) @@ -39,6 +39,7 @@ 'endscript', 'warningtime', 'notify', + 'notify_from', 'notify_time_exceeded', 'notify_request_failed', 'notify_interval', @@ -55,6 +56,7 @@ self.endscript = "" self.warningtime = 1.0 self.notify = None + self.notify_from = None self.notify_time_exceeded = False self.notify_request_failed = False self.notify_interval = 15 @@ -84,5 +86,6 @@ self.notify_request_failed = getYesNoAttributeValue(child, src.xmlDefs.ATTR_REQUEST_FAILED) self.notify_interval = int(getDefaultAttributeValue(child, src.xmlDefs.ATTR_INTERVAL, "15")) self.notify = readStringElementList(child, src.xmlDefs.ELEMENT_MAILTO) + self.notify_from = readOneStringElement(child, src.xmlDefs.ELEMENT_MAILFROM) self.notify_subject = readOneStringElement(child, src.xmlDefs.ELEMENT_SUBJECT) self.notify_body = readOneStringElement(child, src.xmlDefs.ELEMENT_BODY) Modified: CalDAVTester/trunk/src/xmlDefs.py =================================================================== --- CalDAVTester/trunk/src/xmlDefs.py 2007-04-05 21:21:47 UTC (rev 1455) +++ CalDAVTester/trunk/src/xmlDefs.py 2007-04-06 15:29:20 UTC (rev 1456) @@ -37,6 +37,7 @@ ELEMENT_HOST = "host" ELEMENT_KEY = "key" ELEMENT_LOGGING = "logging" +ELEMENT_MAILFROM = "mailfrom" ELEMENT_MAILTO = "mailto" ELEMENT_METHOD = "method" ELEMENT_MONITORINFO = "monitorinfo"
participants (1)
-
source_changes@macosforge.org