View difference between Paste ID: wpziu1RD and NgwmB0DB
SHOW: | | - or go back to the newest paste.
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):
16+
def parser(parm, parm2):
17
    #pdb.set_trace()
18
    global alarm_falhas
19
    global alarm_desempenho
20
    #separators
21
    svalida          = str(parm2)[:5]
22
    sbloco_inicio    = "<alarm-responses>"
23
    sbloco_fim       = "</alarm-responses>"
24
    sbloco_alarm     = "</alarm>"
25
    smodel_name      = "<attribute id=0x1006e>"
26
    screation_t      = "<attribute id=0x11f4e>"
27
    sseverity        = "<attribute id=0x11f56>"
28
    salarm_title     = "<attribute id=0x12b4c>"
29
    sip              = "<attribute id=0x12d7f>"
30
    ssecurity        = "<attribute id=0x10009>"
31
    sfim             = "</attribute>"
32
    sseparador       = ";"
33
    #preparing the parser
34
    string_temp1     = str(parm.split(sbloco_inicio)[1])
35
    string_temp2     = str(string_temp1.split(sbloco_fim)[0])
36
    string_temp3     = str(string_temp2).split(sbloco_alarm)
37
    string_temp4     = ""
38
    list_temp1       = []
39
    list_temp2       = []
40
    loop1_           = 0
41
    #building the list
42
    while loop1_ < len(string_temp3):
43
        list_temp1.append(str(string_temp3[loop1_]))
44
        loop1_ = loop1_ + 1   
45
    loop2_ = 0
46
    #cleaning the list
47
    while loop2_ < len(list_temp1):
48
        temp_  = str(list_temp1[loop2_])
49
        if len(temp_) > 10:
50
            temp_1 = int(temp_.find(smodel_name)) + len(smodel_name)
51
            temp_2 = int(temp_.find(screation_t)) + len(screation_t)
52
            temp_3 = int(temp_.find(sseverity)) + len(sseverity)
53
            temp_4 = int(temp_.find(salarm_title)) + len(salarm_title)
54
            temp_5 = int(temp_.find(sip)) + len(sip)
55
            temp_6 = int(temp_.find(ssecurity)) + len(ssecurity)
56
            temp_1 = str(str(temp_[temp_1:]).split(sfim)[0]) + sseparador
57-
            temp_5 = str(str(temp_[temp_5:]).split(sfim)[0])
57+
58
            temp_3 = str(str(temp_[temp_3:]).split(sfim)[0]).replace("1", "Minor").replace("2", "Major").replace("3", "Critical") + sseparador
59
            temp_4 = str(str(temp_[temp_4:]).split(sfim)[0]) + sseparador
60-
		alarm_desempenho.append(temp_f)
60+
            temp_5 = str(str(temp_[temp_5:]).split(sfim)[0]) + sseparador
61
            temp_6 = str(str(temp_[temp_6:]).split(sfim)[0])
62-
		alarm_falhas.append(temp_f)
62+
63
	    if temp_3.replace(sseparador,"") != "Critical" and temp_4[:4] == "EH -":
64
                if svalida == str(temp_6).split("|")[0]:
65
        		alarm_desempenho.append(temp_f)
66
	    elif temp_3.replace(sseparador,"") == "Critical" and temp_4.replace(sseparador,"") == "DEVICE HAS STOPPED RESPONDING TO POLLS":
67
                if svalida == str(temp_6).split("|")[0]:
68
        		alarm_falhas.append(temp_f)
69
        loop2_ = loop2_ + 1
70
    #return list_temp2
71
#Model Name | Creation date | Severity | Alarm Title | IP 
72
xml_post = """\
73
<?xml version="1.0" encoding="UTF-8"?>
74
<rs:alarm-request throttlesize="100"
75
xmlns:rs="http://www.ca.com/spectrum/restful/schema/request"
76
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
77
xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd ">
78
<rs:attribute-filter><search-criteria xmlns="http://www.ca.com/spectrum/restful/schema/filter">
79
<filtered-models><equals><attribute id="0x111f54"><value>"3"</value></attribute></equals>
80
</filtered-models></search-criteria></rs:attribute-filter>
81
<rs:requested-attribute id="0x1006e" />
82
<rs:requested-attribute id="0x11f4e" />
83
<rs:requested-attribute id="0x11f56" />
84
<rs:requested-attribute id="0x12b4c" />
85
<rs:requested-attribute id="0x12d7f" />
86
<rs:requested-attribute id="0x10009" />
87
</rs:alarm-request>
88
"""
89
def x(a,b):
90
    global usuario_
91
    global pw_
92
    usuario_ = a
93
    pw_ = b
94
    pwm_   = urllib2.HTTPPasswordMgrWithDefaultRealm()
95-
    parser(str(xml_result).replace(chr(34),""))
95+
96
    auth_  = urllib2.HTTPBasicAuthHandler(pwm_)
97
    authf_ = urllib2.build_opener(auth_)
98
    urllib2.install_opener(authf_)
99
    req_   = urllib2.Request(restful_)
100
    req_.add_header('Content-Type', 'application/xml')
101
    xml_result = urllib2.urlopen(req_, xml_post).read()
102
    parser(str(xml_result).replace(chr(34),""),a)
103
    #print alarm_desempenho, alarm_falhas
104
    return alarm_desempenho, alarm_falhas
105
    if os.path.exists("teste.txt"):
106
        os.remove("teste.txt")
107
    y = open("teste.txt", "w")
108
    y.write(str(alarm_desempenho))
109
    y.close()
110
    print "fim"
111
#x("tremgope","tremgope")
112
113
#print len() #Litsa com os alarmes de desempenho
114
#print len(alarm_falhas) #Lista com os alarmes de falha
115
116
#(Obviamente soh estou printando a quantidade de elementos das listas nesse exemplo. Precisamos printar o conteudo alarm_desempenho[x])