-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_xml.py
155 lines (118 loc) · 4.02 KB
/
get_xml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from pysnmp.entity.rfc3413.oneliner import cmdgen
import time
import sys
import operator
# import shutil
from copy import deepcopy
import onevalue
import xml_body
DATE = time.strftime('%Y-%m-%d %H-%M')
IP = input('IP: ')
community = input('Community (default: 3.1415926535): ') or '3.1415926535'
Group = input('Group: ')
sysname = onevalue.get_value(ip=IP, comm=community, oid='1.3.6.1.2.1.1.5.0')
oids_value = {}
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData(community),
cmdgen.UdpTransportTarget((str(IP), 161)),
'1.3.6.1.2.1.2.2.1.2'
)
if errorIndication:
print(errorIndication)
sys.exit()
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
a = name.prettyPrint().replace("SNMPv2-SMI::mib-2.2.2.1.2.", '')
oids_value[a] = val.prettyPrint()
print('There are next interfaces:')
sorted_oids_value = sorted(oids_value.items(), key=operator.itemgetter(1))
for k, v in sorted_oids_value:
print(k, ' = ', v)
def grep(oidlist, keyword):
if 'disabled' in keyword:
return oidlist
else:
res = {}
for k, v in oidlist.items():
for i in keyword:
if i in str(v):
res[k] = oidlist[k]
oidlist = res
return oidlist
def grepv(oidlist, keyword):
res = deepcopy(oidlist)
for k, v in res.items():
for i in keyword:
if i in str(v):
del oidlist[k]
return oidlist
def set_for_name(string):
res = ''
if 'FastEthernet' in string:
res = string.replace('FastEthernet', 'Fa')
elif 'GigabitEthernet' in string:
res = string.replace('GigabitEthernet', 'Gi')
return res
def set_for_keys(string):
res = ''
if 'FastEthernet' in string:
res = string.replace('FastEthernet', 'Fa')
res = res.replace('/', '-')
elif 'GigabitEthernet' in string:
res = string.replace('GigabitEthernet', 'Gi')
res = res.replace('/', '-')
return res
while True:
filter_choosing = input('grep[1] or grep -v [2] [default: without]: ') or 'without'
if filter_choosing == 'without':
print('No filters applied')
break
if filter_choosing == 'end':
print('Applying filter')
break
elif filter_choosing == '1':
Filter = input('Filter: ') or 'disabled'
inputs = Filter.split(' ')
oids_value = grep(oids_value, inputs)
for i in oids_value:
print(oids_value[i])
elif filter_choosing == '2':
Filter = input('Filter: ') or 'disabled'
inputs = Filter.split(' ')
oids_value = grepv(oids_value, inputs)
for i in oids_value:
print(oids_value[i])
else:
print('Wrong choice')
break
srt = list(oids_value.keys())
srt.sort()
file_name = IP + ' ' + DATE + '.xml'
print('Next interfaces added to' + file_name)
sorted_oids_value = sorted(oids_value.items(), key=operator.itemgetter(1))
for k, v in sorted_oids_value:
print(k, ' = ', v)
f = open(file_name, 'w')
f.write(xml_body.header % (Group, sysname, sysname, Group, IP))
for i in srt:
data = {'hostname': sysname, 'interface_key': set_for_keys(oids_value[i]),
'interface_name': (set_for_name(oids_value[i])), 'interface_oid': i, 'comm': community}
f.write(xml_body.items % data)
f.write(xml_body.items_triggers)
for i in srt:
data = {'hostname': sysname, 'interface_key': set_for_keys(oids_value[i]),
'interface_name': (set_for_name(oids_value[i]))}
if 'Fast' in oids_value[i]:
f.write(xml_body.triggers_fa % data)
elif 'Giga' in oids_value[i]:
f.write(xml_body.triggers_gi % data)
f.write(xml_body.triggers_graphs)
for i in srt:
data = {'hostname': sysname, 'interface_key': set_for_keys(oids_value[i]),
'interface_name': (set_for_name(oids_value[i]))}
f.write(xml_body.graph % data)
f.write(xml_body.end_of_xml)
f.close()
# shutil.move(file_name, '/home/sgalay/')