/
switch.py
360 lines (316 loc) · 10.4 KB
/
switch.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import time
import pexpect
import configparser
import easysnmp
from multiprocess import Pool, Manager
# from py2neo import Graph, Node, authenticate
from funcy import lmap, compose, partial, re_find, select, re_test
from device.switch import S85, S93, T64, S89, S8905E
config = configparser.ConfigParser()
config.read(os.path.expanduser('~/.weihu/config.ini'))
community = config.get('switch', 'community')
neo4j_username = config.get('neo4j', 'username')
neo4j_password = config.get('neo4j', 'password')
# authenticate('61.155.48.36:7474', neo4j_username, neo4j_password)
# graph = Graph("http://61.155.48.36:7474/db/data")
processor = 128
sw_file, log_file, result_file = ('sw.txt', 'result/sw_log.txt',
'result/sw_info.txt')
def clear_log():
for f in [log_file, result_file]:
if os.path.exists(f):
os.remove(f)
os.mknod(f)
def create_sw_node(r):
area, ip, hostname, model = r.split(',')
node = Node('Switch', area=area, ip=ip, hostname=hostname, model=model)
return node
def import_sw(file):
switchs = (x.strip() for x in open(file))
lmap(lambda x: graph.create(create_sw_node(x)), switchs)
def update_model():
# clear_log()
nodes = graph.cypher.execute("match (s:Switch) return s.ip as ip")
switchs = [x['ip'] for x in nodes]
for x in switchs:
mark = 'success'
try:
session = easysnmp.Session(
hostname=x, community=community, version=1)
rslt = session.get('1.3.6.1.2.1.1.1.0').value
model = re_find(r'((?<=Quidway )S\d+|(?<=ZXR10 )\w+(?= Software))',
rslt)
# model = re_find(r'(?:Quidway (S\d+)|ZXR10 (\w+) Software)', rslt)
# model = select(bool, model)[0]
if model.startswith('8905'):
model = 'S' + model
hostname = session.get('1.3.6.1.2.1.1.5.0').value
except (easysnmp.EasySNMPTimeoutError) as e:
mark = 'fail'
graph.cypher.execute(
"match (s:Switch) where s.ip={ip} set s.snmpState='abnormal'",
ip=x)
if mark == 'success':
graph.cypher.execute(
"match (s:Switch) where s.ip={ip} set s.model={model},s.hostname={hostname},s.snmpState='normal'",
ip=x,
model=model,
hostname=hostname)
# with open(log_file, 'a') as flog:
# flog.write('{ip}:{mark}\n'.format(ip=x, mark=mark))
def del_old_data():
# cmd1 = """
# match (:Switch)-->(i:Inf)
# where timestamp()-i.updated>=24*60*60*1000
# detach delete i
# """
# cmd2 = """
# match (:Switch)-->(g:Group)
# where timestamp()-g.updated>=24*60*60*1000
# detach delete g
# """
# cmd3 = """
# match (:Switch)-->(:Group)-[r]->(:Inf)
# where timestamp()-r.updated>=24*60*60*1000
# detach delete r
# """
cmd1 = """
match (:Switch)-->(i:Inf)
detach delete i
"""
cmd2 = """
match (:Switch)-->(g:Group)
detach delete g
"""
cmd3 = """
match (:Switch)-->(:Group)-[r]->(:Inf)
detach delete r
"""
graph.cypher.execute(cmd1)
graph.cypher.execute(cmd2)
graph.cypher.execute(cmd3)
def _model(funcs, device):
def no_model(**kw):
return ('fail', None, kw['ip'])
model = device.pop('model')
return funcs.get(model, no_model)(**device)
def _add_groups(lock, record):
mark, groups, ip = record
statement = """match (s:Switch {ip:{ip}})
merge (s)-[r:HAS]->(g:Group {name:{name}})
on create set g.mode={mode},g.desc={desc},g.updated=timestamp()
on match set g.mode={mode},g.desc={desc},g.updated=timestamp()
with s,g
match (s)-->(g)-[r:OWNED]->(i:Inf)
delete r"""
with lock:
if mark == 'success':
tx = graph.cypher.begin()
lmap(lambda x: tx.append(statement, ip=ip, **x), groups)
tx.process()
tx.commit()
# with open(log_file, 'a') as flog:
# flog.write('{ip}:{mark}\n'.format(ip=ip, mark=mark))
def add_groups():
funcs = {
'S8508': S85.get_groups,
'S8505': S85.get_groups,
'T64G': T64.get_groups,
'S8905': S89.get_groups,
'S8905E': S8905E.get_groups,
'S9306': S93.get_groups,
'S9303': S93.get_groups
}
get_groups = partial(_model, funcs)
# clear_log()
nodes = graph.cypher.execute(
"match(s:Switch) where s.snmpState='normal' return s.ip as ip,s.model as model"
)
switchs = [dict(ip=x['ip'], model=x['model']) for x in nodes]
pool = Pool(processor)
lock = Manager().Lock()
_ff = partial(_add_groups, lock)
list(pool.map(compose(_ff, get_groups), switchs))
pool.close()
pool.join()
def _add_infs(lock, record):
mark, infs, ip = record
statement = """
match (s:Switch {ip:{ip}})
merge (s)-[:has]->(i:Inf {name:{name}})
set i.desc={desc},i.updated=timestamp()
with s,i
match (s)-->(g:Group {name:{group}})
merge (g)-[r:OWNED]->(i)
set r.updated=timestamp()
"""
with lock:
if mark == 'success':
tx = graph.cypher.begin()
lmap(lambda x: tx.append(statement, ip=ip, name=x[
'name'], desc=x['desc'], group=x['group']), infs)
tx.process()
tx.commit()
# with open(log_file, 'a') as flog:
# flog.write('{ip}:{mark}\n'.format(ip=ip, mark=mark))
def add_infs():
funcs = {
'S8508': S85.get_infs,
'S8505': S85.get_infs,
'T64G': T64.get_infs,
'S8905': S89.get_infs,
'S8905E': S8905E.get_infs,
'S9306': S93.get_infs,
'S9303': S93.get_infs
}
get_infs = partial(_model, funcs)
# clear_log()
nodes = graph.cypher.execute(
"match(s:Switch) where s.snmpState='normal' return s.ip as ip,s.model as model"
)
switchs = [dict(ip=x['ip'], model=x['model']) for x in nodes]
pool = Pool(processor)
lock = Manager().Lock()
_ff = partial(_add_infs, lock)
list(pool.map(compose(_ff, get_infs), switchs))
pool.close()
pool.join()
def _add_traffics(lock, record):
mark, rslt, ip = record
cmd = """
match (s:Switch {ip:{ip}})-->(i:Inf{name:{name}})
set i.state={state},i.bw={bw},i.inTraffic={inTraffic},i.outTraffic={outTraffic},i.updated=timestamp()
"""
with lock:
if mark == 'success':
tx = graph.cypher.begin()
lmap(lambda x: tx.append(cmd, ip=ip, **x), rslt)
tx.process()
tx.commit()
# with open(log_file, 'a') as flog:
# flog.write('{ip}:{mark}\n'.format(ip=ip, mark=mark))
def add_traffics():
funcs = {
'S8508': S85.get_traffics,
'S8505': S85.get_traffics,
'T64G': T64.get_traffics,
'S8905': S89.get_traffics,
'S8905E': S8905E.get_traffics,
'S9306': S93.get_traffics,
'S9303': S93.get_traffics
}
get_traffics = partial(_model, funcs)
# clear_log()
nodes = graph.cypher.execute(
"match (s:Switch)--(i:Inf) where s.snmpState='normal' return s.ip as ip,collect(i.name) as infs,s.model as model"
)
switchs = [
dict(ip=x['ip'], infs=x['infs'], model=x['model']) for x in nodes
]
pool = Pool(processor)
lock = Manager().Lock()
_ff = partial(_add_traffics, lock)
list(pool.map(compose(_ff, get_traffics), switchs))
pool.close()
pool.join()
def _add_main_card(lock, record):
mark, rslt, ip = record
cmd = """
match (s:Switch {ip:{ip}})
set s.mainCard={rslt}
"""
with lock:
if mark == 'success':
tx = graph.cypher.begin()
tx.append(cmd, ip=ip, rslt=rslt)
tx.process()
tx.commit()
# with open(log_file, 'a') as flog:
# flog.write('{ip}:{mark}\n'.format(ip=ip, mark=mark))
def add_main_card():
funcs = {
'S8508': S85.get_main_card,
'S8505': S85.get_main_card,
'T64G': T64.get_main_card,
'S8905': S89.get_main_card,
'S8905E': S8905E.get_main_card,
'S9306': S93.get_main_card,
'S9303': S93.get_main_card
}
get_main_card = partial(_model, funcs)
# clear_log()
nodes = graph.cypher.execute(
"match (s:Switch) where s.snmpState='normal' return s.ip as ip,s.model as model"
)
switches = [dict(ip=x['ip'], model=x['model']) for x in nodes]
pool = Pool(processor)
lock = Manager().Lock()
_ff = partial(_add_main_card, lock)
list(pool.map(compose(_ff, get_main_card), switches))
pool.close()
pool.join()
def _add_power_info(lock, record):
mark, rslt, ip = record
cmd = """
match (s:Switch {ip:{ip}})
set s.powerInfo={rslt}
"""
with lock:
if mark == 'success':
tx = graph.cypher.begin()
tx.append(cmd, ip=ip, rslt=rslt)
tx.process()
tx.commit()
# with open(log_file, 'a') as flog:
# flog.write('{ip}:{mark}\n'.format(ip=ip, mark=mark))
def add_power_info():
funcs = {
'S8508': S85.get_power_info,
'S8505': S85.get_power_info,
'T64G': T64.get_power_info,
'S8905': S89.get_power_info,
'S8905E': S8905E.get_power_info,
'S9306': S93.get_power_info,
'S9303': S93.get_power_info
}
get_power_info = partial(_model, funcs)
# clear_log()
nodes = graph.cypher.execute(
"match (s:Switch) where s.snmpState='normal' return s.ip as ip,s.model as model"
)
switches = [dict(ip=x['ip'], model=x['model']) for x in nodes]
pool = Pool(processor)
lock = Manager().Lock()
_ff = partial(_add_power_info, lock)
list(pool.map(compose(_ff, get_power_info), switches))
pool.close()
pool.join()
def get_vlans_of_port():
switch = dict(9306=S93, 9303=S93)
bras = dict(me60=ME60)
action = 'get_vlans_of_port'
reader = csv.reader(open('links.csv'))
next(reader)
for link in reader:
sw_vlans = getattr(switch.get(link[2]), action)(link[0], link[1])
bras_vlans = getattr(bras.get(link[5]), action)(link[3], link[4])
print(sw_vlans)
print(bras_vlans)
def main():
# pass
starttime = time.time()
update_model()
del_old_data()
add_groups()
add_infs()
add_traffics()
# add_main_card()
# add_power_info()
endtime = time.time()
print(endtime - starttime)
if __name__ == '__main__':
main()