-
Notifications
You must be signed in to change notification settings - Fork 1
/
olt.py
359 lines (297 loc) · 12.5 KB
/
olt.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import configparser
import os
import multiprocessing
from multiprocess import Pool, Manager
from py2neo import Graph, Node
from py2neo import authenticate
from toolz import compose, map, partial
from device.olt import Zte, Huawei
config = configparser.ConfigParser()
config.read('config.ini')
neo4j_username = config.get('neo4j', 'username')
neo4j_password = config.get('neo4j', 'password')
zte_olt_username = config.get('olt', 'zte_username')
zte_olt_password = config.get('olt', 'zte_password')
hw_olt_username = config.get('olt', 'hw_username')
hw_olt_password = config.get('olt', 'hw_password')
olts_file, log_file, result_file = ('olts.txt', 'result/olt_log.txt',
'result/olt_info.txt')
authenticate('61.155.48.36:7474', neo4j_username, neo4j_password)
graph = Graph("http://61.155.48.36:7474/db/data")
def clear_log():
for f in [log_file, result_file]:
if os.path.exists(f):
os.remove(f)
os.mknod(f)
######################card check################################
zte_card_check = partial(Zte.card_check,
username=zte_olt_username,
password=zte_olt_password)
hw_card_check = partial(Huawei.card_check,
username=hw_olt_username,
password=hw_olt_password)
def get_card(olt):
functions = dict(zte=zte_card_check, hw=hw_card_check)
no_company = lambda x: ['fail', None]
ip, company = olt[:2]
return functions.get(company, no_company)(ip) + [','.join(olt)]
def card_entry(info):
create_card_node = lambda x: graph.create(
Node('Card', slot=x[0], name=x[1]))[0]
mark, result, olt = info
with open(log_file, 'a') as logging:
logging.write("{0}:{1}\n".format(olt, mark))
if result and mark == 'success':
ip = olt.split(',')[0]
node = graph.find_one(
'Olt', property_key='ip', property_value=ip)
card_nodes = map(create_card_node, result)
list(map(lambda x: graph.create((node, 'HAS', x)), card_nodes))
def card_entry_m(lock, info):
create_card_node = lambda x: graph.create(
Node('Card', slot=x[0], name=x[1]))[0]
mark, result, olt = info
with lock:
with open(log_file, 'a') as logging:
logging.write("{0}:{1}\n".format(olt, mark))
if result and mark == 'success':
ip = olt.split(',')[0]
with lock:
node = graph.find_one(
'Olt', property_key='ip', property_value=ip)
card_nodes = map(create_card_node, result)
list(map(lambda x: graph.create((node, 'HAS', x)), card_nodes))
def card_check():
clear_log()
# nodes = graph.find('Olt', property_key='ip', property_value='218.92.130.130')
nodes = graph.find('Olt')
# nodes = graph.find('Olt', property_key='company', property_value='zte')
olts = [(x['ip'], x['company'], x['area']) for x in nodes]
# list(map(compose(card_entry, get_card), olts))
pool = multiprocessing.Pool(8)
lock = multiprocessing.Manager().Lock()
func = partial(card_entry_m, lock)
list(pool.map(compose(func, get_card), olts))
pool.close()
pool.join()
#############power check###################################
hw_power_check = partial(Huawei.power_check,
username=hw_olt_username,
password=hw_olt_password)
def get_power(olt):
functions = dict(hw=hw_power_check)
no_company = lambda x: ['fail', None]
ip, company = olt[:2]
return functions.get(company, no_company)(ip) + [','.join(olt)]
def output_power_info(info):
mark, result, olt = info
with open(log_file, 'a') as logging:
logging.write("{0}:{1}\n".format(olt, mark))
if result and result[0] == 'Alarm' and mark == 'success':
with open(result_file, 'a') as frslt:
frslt.write("{0}:单电源或单路供电.\n".format(olt, ))
def power_check():
clear_log()
nodes = graph.find('Olt', property_key='company',
property_value='hw')
olts = [(x['ip'], x['company'], x['area']) for x in nodes]
funcy.lmap(funcy.compose(output_power_info, get_power), olts)
###################svlan check#############################
zte_epon_svlan = partial(Zte.epon_svlan,
username=zte_olt_username,
password=zte_olt_password)
zte_gpon_svlan = partial(Zte.gpon_svlan,
username=zte_olt_username,
password=zte_olt_password)
hw_svlan = partial(Huawei.svlan,
username=hw_olt_username,
password=hw_olt_password)
def get_svlan(olt):
functions = dict(hw=hw_svlan, zte=zte_epon_svlan)
no_company = lambda x: ['fail', None]
ip, company = olt[:2]
return functions.get(company, no_company)(ip) + [','.join(olt)]
def svlan_entry(lock, info):
cmd = "match (n:Olt{ip:{ip}}) create unique (n)-[:USE{port:{port}}]-(:Svlan{value:{value}})"
mark, result, olt = info
ip = olt.split(',')[0]
with lock:
with open(log_file, 'a') as logging:
logging.write("{0}:{1}\n".format(olt, mark))
if result and mark == 'success':
with lock:
list(map(lambda x: graph.cypher.execute(
cmd, {"ip": ip, "port": x[0], "value": x[1]}), result))
# with open(result_file, 'a') as frslt:
# list(map(
# lambda x: frslt.write("{0},{1},{2}\n".format(ip, x[0], x[1])),
# result))
def svlan_check():
clear_log()
# nodes = graph.find('Olt', property_key='ip', property_value='9.192.96.246')
nodes = graph.find('Olt')
# nodes = graph.find('Olt', property_key='company', property_value='zte')
olts = [(x['ip'], x['company'], x['area']) for x in nodes]
# list(map(compose(card_entry, get_card), olts))
pool = Pool(16)
lock = Manager().Lock()
func = partial(svlan_entry, lock)
list(pool.map(compose(func, get_svlan), olts))
pool.close()
pool.join()
def zte_gpon_svlan_check():
clear_log()
nodes = graph.cypher.execute(
"match(n:Olt)--(c:Card) where c.name='GTGO' return n.ip,collect(c.slot)")
olts = ((x[0], x[1]) for x in nodes)
lzte_gpon_svlan = lambda x: zte_gpon_svlan(ip=x[0], slots=x[1])
pool = Pool(8)
lock = Manager().Lock()
func = partial(svlan_entry, lock)
list(pool.map(compose(func, lzte_gpon_svlan), olts))
pool.close()
pool.join()
###########################hostname#############################
zte_hostname = partial(Zte.hostname,
username=zte_olt_username,
password=zte_olt_password)
hw_hostname = partial(Huawei.hostname,
username=hw_olt_username,
password=hw_olt_password)
def get_hostname(olt):
functions = dict(hw=hw_hostname, zte=zte_hostname)
no_company = lambda x: ['fail', None, x]
ip, company = olt[:2]
return functions.get(company, no_company)(ip)
def hostname_entry(lock, info):
cmd = "match (n:Olt{ip:{ip}}) create unique (n)-[:USE{port:{port}}]-(:Svlan{value:{value}})"
mark, result, ip = info
with lock:
with open(log_file, 'a') as logging:
logging.write("{0}:{1}\n".format(ip, mark))
if result and mark == 'success':
with lock:
# list(map(lambda x: graph.cypher.execute(cmd, {"ip": ip, "port": x[0], "value": x[1]}), result))
with open(result_file, 'a') as frslt:
frslt.write("{0},{1}\n".format(ip, result))
def hostname_check():
clear_log()
nodes = graph.find('Olt')
# nodes = graph.find('Olt', property_key='ip', property_value='172.18.0.46')
olts = [(x['ip'], x['company']) for x in nodes]
pool = Pool(16)
lock = Manager().Lock()
func = partial(hostname_entry, lock)
list(pool.map(compose(func, get_hostname), olts))
pool.close()
pool.join()
ip_hostname = (x.split(',') for x in open(result_file))
cmd = "match (n:Olt) where n.ip={ip} set n.hostname={hostname}"
list(map(lambda x: graph.cypher.execute(
cmd, ip=x[0], hostname=x[1]), ip_hostname))
################################zhongji check###########################
zte_zhongji = partial(Zte.zhongji,
username=zte_olt_username,
password=zte_olt_password)
hw_zhongji = partial(Huawei.zhongji,
username=hw_olt_username,
password=hw_olt_password)
def get_zhongji(olt):
functions = dict(hw=hw_zhongji, zte=zte_zhongji)
no_company = lambda x: ['fail', None, x]
ip, company = olt[:2]
return functions.get(company, no_company)(ip)
def zhongji_entry(lock, info):
mark, result, ip = info
with lock:
with open(log_file, 'a') as logging:
logging.write("{ip}:{mark}\n".format(ip=ip, mark=mark))
if result and mark == 'success':
with lock:
# list(map(lambda x: graph.cypher.execute(cmd, {"ip": ip, "port": x[0], "value": x[1]}), result))
with open(result_file, 'a') as frslt:
for (k, v) in result.items():
for i in v:
frslt.write(
"{ip},{sm},{interface}\n".format(ip=ip, sm=k, interface=i))
def zhongji_check():
clear_log()
nodes = graph.find('Olt')
# nodes = graph.find('Olt', property_key='ip', property_value='172.18.0.46')
olts = [(x['ip'], x['company']) for x in nodes]
pool = Pool(16)
lock = Manager().Lock()
func = partial(zhongji_entry, lock)
list(pool.map(compose(func, get_zhongji), olts))
pool.close()
pool.join()
ports = (x.split(',') for x in open(result_file))
cmd = """match(n: Olt) where n.ip = {ip}
merge(n) - [:HAS]->(m: Etrunk{name: {sm}})
merge(m) - [:Include]->(p: Port{name: {interface}})"""
list(map(lambda x: graph.cypher.execute(
cmd, ip=x[0], sm=x[1], interface=x[2]), ports))
#############################################traffic####################
zte_traffic = partial(Zte.get_traffics,
username=zte_olt_username,
password=zte_olt_password)
hw_traffic = partial(Huawei.get_traffics,
username=hw_olt_username,
password=hw_olt_password)
def get_traffic(olt):
functions = dict(hw=hw_traffic, zte=zte_traffic)
no_company = lambda x, y: ['fail', None, x]
ip, company, ports = olt
return functions.get(company, no_company)(ip=ip, ports=ports)
def traffic_output(lock, info):
mark, result, ip = info
with lock:
with open(log_file, 'a') as logging:
logging.write("{ip}:{mark}\n".format(ip=ip, mark=mark))
if result and mark == 'success':
with lock:
for r in result:
with open(result_file, 'a') as frslt:
frslt.write("{ip},{port},{down},{up}\n".format(
ip=ip, port=r['name'].strip(), down=r['in_traffic'], up=r['out_traffic']))
def traffic_check():
# clear_log()
# cmd = 'match(n:Olt)-[*]-(p:Port) where n.company="hw" return n.ip,n.company,collect(p.name)'
# nodes = graph.cypher.execute(cmd)
# olts = [(x[0], x[1], x[2]) for x in nodes]
# pool = Pool(16)
# lock = Manager().Lock()
# func = partial(traffic_output, lock)
# list(pool.map(compose(func, get_traffic), olts))
# pool.close()
# pool.join()
records = (x.split(',') for x in open(result_file))
cmd = "match (n:Olt)-[*]-(p:Port) where n.ip={ip} and p.name={name} set p.in_traffic={i},p.out_traffic={o}"
list(map(lambda x: graph.cypher.execute(
cmd, ip=x[0], name=x[1], i=x[2], o=x[3]), records))
def hw_gpon():
clear_log()
cmd = 'match(n:Olt{company:\'hw\'})--(c:Card) where c.name contains \'GPBD\' return distinct n.ip'
nodes = graph.cypher.execute(cmd)
olts = [x[0] for x in nodes]
# nodes = graph.find('Olt')
# nodes = graph.find('Olt', property_key='ip', property_value='172.18.0.46')
command = 'xpon ont-interoperability-mode gpon tcont-pq-priority-reverse enable'
doSomething = partial(Huawei.doSomething,
username=hw_olt_username,
password=hw_olt_password,
command=command)
def output(info):
mark, ip = info
with open(log_file, 'a') as logging:
logging.write("{ip}:{mark}\n".format(ip=ip, mark=mark))
list(map(compose(output, doSomething), olts))
def main():
# zhongji_check()
# hw_gpon()
# pass
traffic_check()
if __name__ == '__main__':
main()