an.py 2.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
#!/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
10
11
# for time.asctime()
#!import time
12
13
14
15

# needed for notifyOSX()
import os
osx_command = "/Users/kpetersn/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier"
kpetersn's avatar
kpetersn committed
16
osx_url = "http://bcda.xray.aps.anl.gov/cgi-bin/ioc_alive.cgi?eng=Peterson"
17
18
19
20
21
22

### Stuff to customize
engineer = "Peterson"
# period should match cron job period
period = 120

23
24
def notifyConsole(ioc, state, time_str):
  print "[%s] %s is %s" % (time_str, ioc, state)
25

26
def notifyOSX(ioc, state, time_str):
kpetersn's avatar
kpetersn committed
27
  notify_command = "%s -title \"%s is %s\" -message \"%s\" -open \"%s\"" % (osx_command, ioc, state, time_str, osx_url)
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  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)

50
51
52
53
# Get the date and time
time_elem = root.find('time')
time_str = time_elem.text

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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:
118
  notify(ioc, ioc_list[ioc], time_str)