Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib2
- import time
- import os
- import datetime
- import sys
- #import pdb
- # %sope % operador.cliente
- usuario_ = ''
- pw_ = ''
- alarm_falhas = []
- alarm_desempenho = []
- endereco_ = '172.16.24.242'
- restful_ = 'http://172.16.24.242/spectrum/restful/alarms'
- def parser(parm, parm2):
- #pdb.set_trace()
- global alarm_falhas
- global alarm_desempenho
- #separators
- svalida = str(parm2)[:5]
- sbloco_inicio = "<alarm-responses>"
- sbloco_fim = "</alarm-responses>"
- sbloco_alarm = "</alarm>"
- smodel_name = "<attribute id=0x1006e>"
- screation_t = "<attribute id=0x11f4e>"
- sseverity = "<attribute id=0x11f56>"
- salarm_title = "<attribute id=0x12b4c>"
- sip = "<attribute id=0x12d7f>"
- ssecurity = "<attribute id=0x10009>"
- sfim = "</attribute>"
- sseparador = ";"
- #preparing the parser
- string_temp1 = str(parm.split(sbloco_inicio)[1])
- string_temp2 = str(string_temp1.split(sbloco_fim)[0])
- string_temp3 = str(string_temp2).split(sbloco_alarm)
- string_temp4 = ""
- list_temp1 = []
- list_temp2 = []
- loop1_ = 0
- #building the list
- while loop1_ < len(string_temp3):
- list_temp1.append(str(string_temp3[loop1_]))
- loop1_ = loop1_ + 1
- loop2_ = 0
- #cleaning the list
- while loop2_ < len(list_temp1):
- temp_ = str(list_temp1[loop2_])
- if len(temp_) > 10:
- temp_1 = int(temp_.find(smodel_name)) + len(smodel_name)
- temp_2 = int(temp_.find(screation_t)) + len(screation_t)
- temp_3 = int(temp_.find(sseverity)) + len(sseverity)
- temp_4 = int(temp_.find(salarm_title)) + len(salarm_title)
- temp_5 = int(temp_.find(sip)) + len(sip)
- temp_6 = int(temp_.find(ssecurity)) + len(ssecurity)
- temp_1 = str(str(temp_[temp_1:]).split(sfim)[0]) + sseparador
- temp_2 = str(datetime.datetime.fromtimestamp(int(str(str(temp_[temp_2:]).split(sfim)[0]))).strftime('%Y-%m-%d %H:%M:%S')) + sseparador
- temp_3 = str(str(temp_[temp_3:]).split(sfim)[0]).replace("1", "Minor").replace("2", "Major").replace("3", "Critical") + sseparador
- temp_4 = str(str(temp_[temp_4:]).split(sfim)[0]) + sseparador
- temp_5 = str(str(temp_[temp_5:]).split(sfim)[0]) + sseparador
- temp_6 = str(str(temp_[temp_6:]).split(sfim)[0])
- temp_f = temp_1 + temp_2 + temp_3 + temp_4 + temp_5
- if temp_3.replace(sseparador,"") != "Critical" and temp_4[:4] == "EH -":
- if svalida == str(temp_6).split("|")[0]:
- alarm_desempenho.append(temp_f)
- elif temp_3.replace(sseparador,"") == "Critical" and temp_4.replace(sseparador,"") == "DEVICE HAS STOPPED RESPONDING TO POLLS":
- if svalida == str(temp_6).split("|")[0]:
- alarm_falhas.append(temp_f)
- loop2_ = loop2_ + 1
- #return list_temp2
- #Model Name | Creation date | Severity | Alarm Title | IP
- xml_post = """\
- <?xml version="1.0" encoding="UTF-8"?>
- <rs:alarm-request throttlesize="100"
- xmlns:rs="http://www.ca.com/spectrum/restful/schema/request"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd ">
- <rs:attribute-filter><search-criteria xmlns="http://www.ca.com/spectrum/restful/schema/filter">
- <filtered-models><equals><attribute id="0x111f54"><value>"3"</value></attribute></equals>
- </filtered-models></search-criteria></rs:attribute-filter>
- <rs:requested-attribute id="0x1006e" />
- <rs:requested-attribute id="0x11f4e" />
- <rs:requested-attribute id="0x11f56" />
- <rs:requested-attribute id="0x12b4c" />
- <rs:requested-attribute id="0x12d7f" />
- <rs:requested-attribute id="0x10009" />
- </rs:alarm-request>
- """
- def x(a,b):
- global usuario_
- global pw_
- usuario_ = a
- pw_ = b
- pwm_ = urllib2.HTTPPasswordMgrWithDefaultRealm()
- pwm_.add_password(None, endereco_, usuario_, pw_)
- auth_ = urllib2.HTTPBasicAuthHandler(pwm_)
- authf_ = urllib2.build_opener(auth_)
- urllib2.install_opener(authf_)
- req_ = urllib2.Request(restful_)
- req_.add_header('Content-Type', 'application/xml')
- xml_result = urllib2.urlopen(req_, xml_post).read()
- parser(str(xml_result).replace(chr(34),""),a)
- #print alarm_desempenho, alarm_falhas
- return alarm_desempenho, alarm_falhas
- if os.path.exists("teste.txt"):
- os.remove("teste.txt")
- y = open("teste.txt", "w")
- y.write(str(alarm_desempenho))
- y.close()
- print "fim"
- #x("tremgope","tremgope")
- #print len() #Litsa com os alarmes de desempenho
- #print len(alarm_falhas) #Lista com os alarmes de falha
- #(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
Advertisement