maurobaraldi

Spectrum Alarms

Apr 3rd, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. import urllib2
  2. import time
  3. import os
  4. import datetime
  5. import sys
  6. #import pdb
  7.  
  8. # %sope % operador.cliente
  9. usuario_         = ''
  10. pw_              = ''
  11. alarm_falhas     = []
  12. alarm_desempenho = []
  13. endereco_        = '172.16.24.242'
  14. restful_         = 'http://172.16.24.242/spectrum/restful/alarms'
  15.  
  16. def parser(parm):
  17.     #pdb.set_trace()
  18.     global alarm_falhas
  19.     global alarm_desempenho
  20.     #separators
  21.     sbloco_inicio    = "<alarm-responses>"
  22.     sbloco_fim       = "</alarm-responses>"
  23.     sbloco_alarm     = "</alarm>"
  24.     smodel_name      = "<attribute id=0x1006e>"
  25.     screation_t      = "<attribute id=0x11f4e>"
  26.     sseverity        = "<attribute id=0x11f56>"
  27.     salarm_title     = "<attribute id=0x12b4c>"
  28.     sip              = "<attribute id=0x12d7f>"
  29.     sfim             = "</attribute>"
  30.     sseparador       = ";"
  31.     #preparing the parser
  32.     string_temp1     = str(parm.split(sbloco_inicio)[1])
  33.     string_temp2     = str(string_temp1.split(sbloco_fim)[0])
  34.     string_temp3     = str(string_temp2).split(sbloco_alarm)
  35.     string_temp4     = ""
  36.     list_temp1       = []
  37.     list_temp2       = []
  38.     loop1_           = 0
  39.     #building the list
  40.     while loop1_ < len(string_temp3):
  41.         list_temp1.append(str(string_temp3[loop1_]))
  42.         loop1_ = loop1_ + 1  
  43.     loop2_ = 0
  44.     #cleaning the list
  45.     while loop2_ < len(list_temp1):
  46.         temp_  = str(list_temp1[loop2_])
  47.         if len(temp_) > 10:
  48.             temp_1 = int(temp_.find(smodel_name)) + len(smodel_name)
  49.             temp_2 = int(temp_.find(screation_t)) + len(screation_t)
  50.             temp_3 = int(temp_.find(sseverity)) + len(sseverity)
  51.             temp_4 = int(temp_.find(salarm_title)) + len(salarm_title)
  52.             temp_5 = int(temp_.find(sip)) + len(sip)
  53.             temp_1 = str(str(temp_[temp_1:]).split(sfim)[0]) + sseparador
  54.             temp_2 = str(datetime.datetime.fromtimestamp(int(str(str(temp_[temp_2:]).split(sfim)[0]))).strftime('%Y-%m-%d %H:%M:%S')) + sseparador        
  55.             temp_3 = str(str(temp_[temp_3:]).split(sfim)[0]).replace("1", "Minor").replace("2", "Major").replace("3", "Critical") + sseparador
  56.             temp_4 = str(str(temp_[temp_4:]).split(sfim)[0]) + sseparador
  57.             temp_5 = str(str(temp_[temp_5:]).split(sfim)[0])
  58.             temp_f = temp_1 + temp_2 + temp_3 + temp_4 + temp_5
  59.         if temp_3.replace(sseparador,"") != "Critical" and temp_4[:4] == "EH -":
  60.         alarm_desempenho.append(temp_f)
  61.         elif temp_3.replace(sseparador,"") == "Critical" and temp_4.replace(sseparador,"") == "DEVICE HAS STOPPED RESPONDING TO POLLS":
  62.         alarm_falhas.append(temp_f)
  63.         loop2_ = loop2_ + 1
  64.     #return list_temp2
  65. #Model Name | Creation date | Severity | Alarm Title | IP
  66. xml_post = """\
  67. <?xml version="1.0" encoding="UTF-8"?>
  68. <rs:alarm-request throttlesize="100"
  69. xmlns:rs="http://www.ca.com/spectrum/restful/schema/request"
  70. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  71. xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd ">
  72. <rs:attribute-filter><search-criteria xmlns="http://www.ca.com/spectrum/restful/schema/filter">
  73. <filtered-models><equals><attribute id="0x111f54"><value>"3"</value></attribute></equals>
  74. </filtered-models></search-criteria></rs:attribute-filter>
  75. <rs:requested-attribute id="0x1006e" />
  76. <rs:requested-attribute id="0x11f4e" />
  77. <rs:requested-attribute id="0x11f56" />
  78. <rs:requested-attribute id="0x12b4c" />
  79. <rs:requested-attribute id="0x12d7f" />
  80. </rs:alarm-request>
  81. """
  82. def x(a,b):
  83.     global usuario_
  84.     global pw_
  85.     usuario_ = a
  86.     pw_ = b
  87.     pwm_   = urllib2.HTTPPasswordMgrWithDefaultRealm()
  88.     pwm_.add_password(None, endereco_, usuario_, pw_)
  89.     auth_  = urllib2.HTTPBasicAuthHandler(pwm_)
  90.     authf_ = urllib2.build_opener(auth_)
  91.     urllib2.install_opener(authf_)
  92.     req_   = urllib2.Request(restful_)
  93.     req_.add_header('Content-Type', 'application/xml')
  94.     xml_result = urllib2.urlopen(req_, xml_post).read()
  95.     parser(str(xml_result).replace(chr(34),""))
  96.     #print alarm_desempenho, alarm_falhas
  97.     return alarm_desempenho, alarm_falhas
  98.  
  99. #print len() #Litsa com os alarmes de desempenho
  100. #print len(alarm_falhas) #Lista com os alarmes de falha
  101.  
  102. #(Obviamente soh estou printando a quantidade de elementos das listas nesse exemplo. Precisamos printar o conteudo alarm_desempenho[x])
Advertisement
Add Comment
Please, Sign In to add comment