Skip to content
Permalink
Browse files

Minor refactoring and clean-up of termination process.

  • Loading branch information...
cyrusdaboo committed Apr 6, 2007
1 parent e999a4d commit 2aa85d8f5fb5350fed7052963695fddb6899921e
Showing with 116 additions and 79 deletions.
  1. +109 −78 monitor.py
  2. +2 −1 scripts/monitoring/monitorinfo.dtd
  3. +1 −0 scripts/monitoring/monitorinfo.xml
  4. +3 −0 src/monitorinfo.py
  5. +1 −0 src/xmlDefs.py
@@ -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

if logname:
self.log = open(logname, "a")
else:
self.log = None

self.running = True

def readXML():
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):
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,),
)
def doEnd(self):
if self.minfo.endscript:
self.logtxt("Runnning end script %s" % (self.minfo.endscript,))
self.doScript(self.minfo.endscript)

signal.signal(signal.SIGINT, doEnd)
self.logtxt("Stopped Monitor")
self.running = False

if monitorlogname:
log = open(monitorlogname, "a")
else:
log = None
def doError(self, msg):
self.logtxt("Run exception: %s" % (msg,))

def logtxt(txt):
if log:
log.write("%s\n" % (txt,))
else:
print txt

doStart()
def doNotification(self, msg):
sendemail(
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,),
)

if minfo.logging:
logtxt("Start:")
try:
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(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)
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:
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)
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 minfo.logging:
logtxt("Done")
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:
logname = None

user = raw_input("User: ")
pswd = getpass("Password: ")

m = monitor(infoname, logname, user, pswd)
m.readXML()

def signalEnd(sig, frame):
m.doEnd()
sys.exit()

signal.signal(signal.SIGINT, signalEnd)

m.doStart()

try:
m.runLoop()
except SystemExit:
pass
except Exception, e:
log.write("Run exception: %s" % (str(e),))
m.doError(str(e))
@@ -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)>
@@ -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,
@@ -39,6 +39,7 @@ class monitorinfo( object ):
'endscript',
'warningtime',
'notify',
'notify_from',
'notify_time_exceeded',
'notify_request_failed',
'notify_interval',
@@ -55,6 +56,7 @@ def __init__( self ):
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 @@ def parseXML( self, node ):
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)
@@ -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"

0 comments on commit 2aa85d8

Please sign in to comment.
You can’t perform that action at this time.