Commit ca28fe23 authored by kpetersn's avatar kpetersn
Browse files

Initial commit of, which works on the console and OS X.

#!/usr/bin/env python
# Allow engineer to be passed as an argument
# Add other notification functions
# Replace explicit period a time since a dot file was last touched
import urllib
import xml.etree.ElementTree as ET
import time
# needed for notifyOSX()
import os
osx_command = "/Users/kpetersn/Applications/"
### Stuff to customize
engineer = "Peterson"
# period should match cron job period
period = 120
def notifyConsole(ioc, state):
notify_time = time.asctime()
print "[%s] %s is %s" % (notify_time, ioc, state)
def notifyOSX(ioc, state):
notify_time = time.asctime()
notify_command = "%s -title \"%s is %s\" -message \"%s\"" % (osx_command, ioc, state, notify_time)
# set notification function
notify = notifyOSX
#!notify = notifyConsole
alive_url = ""
ioc_list = {}
# Get the xml file
#!print "Opening URL"
f = urllib.urlopen(alive_url)
#!print "Reading XML from URL"
xml_string =
#!print "Closing URL"
# Parse the xml string
#!print "Parsing XML"
root = ET.fromstring(xml_string)
# Indent for making ET.tostring() output readable
def _indent(elem, level=0):
Internal method to make the xml file easier to read by humans.
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
_indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
# Indent for making ET.tostring() output readable
# Extract info from the xml file
for ioc in root.iter('IOC'):
ping_timer = -1
boot_timer = -1
calc_flag = False
name = ioc.attrib['name']
#print ET.tostring(ioc)
for elem in ioc:
if elem.tag == 'ping_timer':
ping_timer = int(elem.text)
if elem.tag == 'boot_timer':
boot_timer = int(elem.text)
if elem.tag == 'env':
if elem.attrib['name'] == 'ENGINEER':
if elem.text == engineer:
# Set calc flag
calc_flag = True
if calc_flag == True:
#!print "name =", name
#!print "ping_timer =", ping_timer
#!print "boot_timer =", boot_timer
#!print "engineer =", engineer
if boot_timer <= period:
# ioc just booted
ioc_list[name] = 'UP'
elif ping_timer > period:
if ping_timer <= (2 * period):
# ioc went down since last poll
ioc_list[name] = 'DOWN'
# ioc has been down for a while
iocs = ioc_list.keys()
for ioc in iocs:
notify(ioc, ioc_list[ioc])
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment