Commit ca28fe23 authored by kpetersn's avatar kpetersn
Browse files

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

parents
.DS_Store
#!/usr/bin/env python
# TODO:
# 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/terminal-notifier.app/Contents/MacOS/terminal-notifier"
### 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)
os.system(notify_command)
# set notification function
notify = notifyOSX
#!notify = notifyConsole
###
alive_url = "http://bcda.xray.aps.anl.gov/cgi-bin/alivexml.cgi"
ioc_list = {}
# Get the xml file
#!print "Opening URL"
f = urllib.urlopen(alive_url)
#!print "Reading XML from URL"
xml_string = f.read()
#!print "Closing URL"
f.close()
# 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
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
# Indent for making ET.tostring() output readable
#!_indent(root)
# 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
#!print
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'
else:
# ioc has been down for a while
pass
iocs = ioc_list.keys()
iocs.sort()
for ioc in iocs:
notify(ioc, ioc_list[ioc])
Markdown is supported
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