<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[1456] CalDAVTester/trunk</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.macosforge.org/projects/calendarserver/changeset/1456">1456</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2007-04-06 08:29:20 -0700 (Fri, 06 Apr 2007)</dd>
</dl>

<h3>Log Message</h3>
<pre>Minor refactoring and clean-up of termination process.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalDAVTestertrunkmonitorpy">CalDAVTester/trunk/monitor.py</a></li>
<li><a href="#CalDAVTestertrunkscriptsmonitoringmonitorinfodtd">CalDAVTester/trunk/scripts/monitoring/monitorinfo.dtd</a></li>
<li><a href="#CalDAVTestertrunkscriptsmonitoringmonitorinfoxml">CalDAVTester/trunk/scripts/monitoring/monitorinfo.xml</a></li>
<li><a href="#CalDAVTestertrunksrcmonitorinfopy">CalDAVTester/trunk/src/monitorinfo.py</a></li>
<li><a href="#CalDAVTestertrunksrcxmlDefspy">CalDAVTester/trunk/src/xmlDefs.py</a></li>
</ul>

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

</body>
</html>