Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
alived-notify 2.53 KiB
#!/APSshare/epd/rh6-x86_64/bin/python


import os
import sys
import urllib
import xml.etree.ElementTree as ET


#
email_dict = { 
'Peterson':'kmpeters@anl.gov', 
'Arms':'dohnarms@anl.gov',
'Kirchman':'jkirchman@aps.anl.gov',
'klang':'klang@aps.anl.gov',
'Goetze':'goetze@aps.anl.gov',
'Sluiter':'sluiter@aps.anl.gov',
'Sullivan':'sullivan@aps.anl.gov',
'WALSH':'twalsh@anl.gov'
}


def sendEmail(ioc, state, time_str, engineer):
  global email_dict
  
  # Alive web page
  alive_url = "http://bcda.xray.aps.anl.gov/cgi-bin/ioc_alive.cgi"
  
  # Kevin prefers different terminology
  ioc_state = {'BOOT':'UP', 'FAIL':'DOWN'}[state]

  # Build email
  recipient = email_dict[engineer]
  subject = "%s is %s" % (ioc, ioc_state)
  message = "Snapshot time: %s\n\n" % time_str
  message += "%s info:\n%s?ioc=%s\n\n" % (ioc, alive_url, ioc)
  message += "%s IOCs:\n%s?eng=%s\n" % (engineer, alive_url, engineer)
  # Build command
  email_command = "echo \"%s\" | mailx -s \"%s\" %s" % (message, subject, recipient)
  # Send email
  os.system(email_command)


def main(args):
  # Alive XML page
  alive_xml_url = "http://bcda.xray.aps.anl.gov/cgi-bin/alivexml.cgi"

  ### Parse args
  # The following line only works if the number of args is exactly four
  #!name, ip, state, mess = args
  # The following lines allow Dohn to add arguments without breaking the script
  name = args[0]
  ip = args[1]
  state = args[2]
  mess = args[3]
  
  # Get the XLM file
  try:
    f = urllib.urlopen(alive_xml_url)
  except IOError:
    print "Couldn't open URL"
    # Failure
    return False

  # Read XML from URL"
  xml_string = f.read()

  # Close URL
  f.close()
  
  #!print xml_string
  
  # Parse the XML string
  try:
    root = ET.fromstring(xml_string)
  except ET.ParseError:
    print "Invalid XML file"
    # Failure
    return False

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

  engineer = ""
  for ioc in root.findall('IOC'):
    # Find ioc whose state has changed
    if name == ioc.get('name'):
      # Find ENGINEER env var
      for env in ioc.findall('env'):
        if env.get('name') == 'ENGINEER':
	  engineer = env.text
	  # No break is needed here, since ENGINEER only appears once for each IOC element
      # Break the loop; the desired IOC has been found
      break

  # Send the email
  sendEmail(name, state, time_str, engineer)
  
  # Success
  return True


if __name__ == '__main__':
  if len(sys.argv) < 5:
    print "Usage: an_hook.py <iocname> <ip> <status> <user_args>"
    #!print sys.argv
  else:
    main(sys.argv[1:])