Advertisement
darrengoulden

Cisco Video Communications Server alarm notifications

Apr 12th, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. ''' update the above line to suit your env
  3.  
  4. VCS Configuration
  5. Create a read-only account on your VCS with API access
  6. NOTE: VCS must be running at least x7.2.1
  7. Format:
  8. vcs = (
  9.     ['username', 'password', 'authentication_realm', 'base_uri', 'full uri to status.xml'],
  10. }
  11.  
  12. To get the authentication_realm simply open the status.xml in your browser
  13. i.e. https://vcs.hostname.com/status.xml
  14. the authentication popup will include a string like
  15. "TANDBERG Video Communication Server xxxxxx" where x is the serial number
  16. '''
  17. vcs = (
  18.     ['username', 'password', 'TANDBERG Video Communication Server XXXXXXXX', 'http://vcs1.hostname.com', 'http://vcs1.hostname.com/status.xml'],
  19.     ['username', 'password', 'TANDBERG Video Communication Server XXXXXXXX', 'https://vcs2.hostname.com', 'https://vcs2.hostname.com/status.xml'],
  20. )
  21. # SMTP server configuration
  22. smtpsrv = "my.smtphost.com"                     # smtp host
  23. destadd = "destination.email@address.com".split()       # email destination - don't remove the .split()
  24. fromadd = "source.email@address.com"                # email source
  25.  
  26. import urllib.request, urllib.error, re, sys, smtplib, string
  27. from xml.dom.minidom import parseString
  28. def stream (username, password, vcsrealm, vcs_buri, vcs_furi):
  29.     'connect to cisco video communication server'
  30.     auth_handler = urllib.request.HTTPBasicAuthHandler()
  31.     auth_handler.add_password(realm=vcsrealm,
  32.                        uri=vcs_buri,
  33.                        user=username,
  34.                        passwd=password)
  35.     opener =  urllib.request.build_opener(auth_handler)
  36.     urllib.request.install_opener(opener)
  37.     res = opener.open(vcs_furi)
  38.     nodes = res.read()
  39.     return nodes
  40. def stripxml (xml):
  41.     'strip XML tagging'
  42.     regex = '<[^<]+>'
  43.     r1 = (re.sub(regex, ':::', xml))
  44.     r2 = (r1.split(':::'))
  45.     r3 = [x for x in r2 if x != '']
  46.     da = 3
  47.     ad = ([])
  48.     while da < len(r3):
  49.         for i in range(0, len(r3), 3):
  50.             ad.append (r3[i:i + 3])
  51.         po = len(r3) / 2
  52.         da *= int(po)
  53.     return ad
  54. def getalarmdetail (xml):
  55.     'parse stripped xml'
  56.     parseme = stripxml(xml)
  57.     x = ''
  58.     c = 0
  59.     for i in enumerate(parseme):
  60.         if i[1][2] == 'Acknowledged':
  61.             continue
  62.         if i[1][2] == 'Unacknowledged':
  63.             c += 1
  64.         elif i[1][2] == 'Raised':
  65.             c += 1
  66.         u = 'ID: ' + str(i[1][0])
  67.         u = u + '\nDescription: ' + str(i[1][1])
  68.         u = u + '\nState: ' + str(i[1][2])
  69.         u = u + '\n'
  70.         x = ("%s%s\n" % (x, u))
  71.     if x == '':
  72.         return str(x)
  73.     x = x + '\nPlease login to ' + vcs_buri + ' to investigate'
  74.     return (c, str(x))
  75. for o in vcs:
  76.     username = o[0]
  77.     password = o[1]
  78.     vcsrealm = o[2]
  79.     vcs_buri = o[3]
  80.     vcs_furi = o[4]
  81.     try:
  82.         dom = parseString(stream(username, password, vcsrealm, vcs_buri, vcs_furi))
  83.         xmlTag = dom.getElementsByTagName('Warnings')[0].toxml()
  84.         # send email
  85.         body = getalarmdetail(xmlTag)
  86.         if body == '':
  87.              break
  88.         body_d = body[1]
  89.         alarm_c = body[0]
  90.         if str(alarm_c) > "1":
  91.             subject = '[WARNING] ' + str(alarm_c) + ' unacknowledged alarms detected on ' + o[3]
  92.         else:
  93.             subject = '[WARNING] ' + str(alarm_c) + ' unacknowledged alarm detected on ' + o[3]
  94.         msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
  95.                % (fromadd, ", ".join(destadd), subject))
  96.         msg = msg + body_d
  97.         server = smtplib.SMTP(smtpsrv)
  98.         server.sendmail(fromadd, [destadd], msg)
  99.         server.quit()
  100.     except urllib.error.HTTPError as e:
  101.         if e.code == 401:
  102.             print ( "Error: Basic authentication failed %s, please check your username and password" % e.code )
  103.         elif e.code == 404:
  104.             print ( "Error: Page not found %s, please check your configuration" % e.code )
  105.         elif e.code == 408:
  106.             print ( "Error: Request timed out %s" % e.code )
  107.         else:
  108.             print ( "Error: %s" % e.code )
  109.     except urllib.error.URLError as e:
  110.         print ( "Error opening URL: %s, please check your configuration" % e )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement