forked from Aspirin1897/VersaTEL_G1_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HAAP.py
689 lines (574 loc) · 21.7 KB
/
HAAP.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# coding:utf-8
from __future__ import print_function
import os
import sys
import time
import re
import Conn as conn
import Sundry as s
import GetConfig as gc
from collections import OrderedDict as odd
# <<<Get Config Field>>>
objHAAPConfig = gc.EngineConfig()
haapcfg = gc.EngineConfig()
list_engines_IP = haapcfg.list_engines_IP()
list_engines_alias = haapcfg.list_engines_alias()
telnet_port = haapcfg.telnet_port()
FTP_port = haapcfg.FTP_port()
passwd = haapcfg.password()
trace_level_cfg = haapcfg.trace_level()
setting = gc.Setting()
strCFGFolder = setting.folder_cfgbackup()
strTraceFolder = setting.folder_trace()
strPCFolder = setting.folder_PeriodicCheck()
oddHAAPErrorDict = setting.oddRegularTrace()
lstPCCommand = setting.PCEngineCommand()
# <<<Get Config Field>>>
def backup_config_all():
folder = '%s/%s' % (strCFGFolder, s.time_now_folder())
for ip in list_engines_IP:
Action(ip, telnet_port, passwd, FTP_port).backup(folder)
def backup_config(ip):
folder = '%s/%s' % (strCFGFolder, s.time_now_folder())
Action(ip, telnet_port, passwd, FTP_port).backup(folder)
def change_firmware(ip, fw_file):
Action(ip, telnet_port, passwd, FTP_port).change_FW(fw_file)
def get_trace_all(trace_level):
folder = '%s/%s' % (strTraceFolder, s.time_now_folder())
if trace_level:
for ip in list_engines_IP:
Action(ip, telnet_port, passwd, FTP_port).get_trace(
folder, trace_level)
else:
for ip in list_engines_IP:
Action(ip, telnet_port, passwd, FTP_port).get_trace(
folder, trace_level_cfg)
def get_trace(ip, trace_level):
folder = '%s/%s' % (strTraceFolder, s.time_now_folder())
try:
if trace_level:
Action(ip, telnet_port, passwd, FTP_port).get_trace(
folder, trace_level)
else:
Action(ip, telnet_port, passwd, FTP_port).get_trace(
folder, trace_level_cfg)
finally:
return folder
def analyse_trace_all(trace_level):
s.TraceAnalyse(oddHAAPErrorDict, get_trace_all(trace_level))
def analyse_trace(ip, trace_level):
s.TraceAnalyse(oddHAAPErrorDict, get_trace(ip, trace_level))
def execute_multi_commands(ip, command_file):
Action(ip, telnet_port, passwd, FTP_port).auto_commands(command_file)
tupDesc = ('Engine', 'Status', 'Uptime', 'Master', 'Cluster', 'Mirror')
tupWidth = (18, 15, 15, 8, 9, 8)
def _print_description():
for i in range(len(tupDesc)):
print(tupDesc[i].ljust(tupWidth[i]), end='')
print()
def _print_status_in_line(lstStatus):
for i in range(len(lstStatus)):
if lstStatus[i]:
print(lstStatus[i].ljust(tupWidth[i]), end='')
else:
print(''.ljust(tupWidth[i]), end='')
print()
def show_stauts_all():
_print_description()
for ip in list_engines_IP:
lstStatus = Status(ip, telnet_port, passwd, FTP_port).status_to_show()
_print_status_in_line(lstStatus)
def show_stauts(ip):
_print_description()
lstStatus = Status(ip, telnet_port, passwd, FTP_port).status_to_show()
_print_status_in_line(lstStatus)
def set_time_all():
for ip in list_engines_IP:
Action(ip, telnet_port, passwd, FTP_port).set_time()
def set_time(ip):
Action(ip, telnet_port, passwd, FTP_port).set_time()
def show_time_all():
for ip in list_engines_IP:
Action(ip, telnet_port, passwd, FTP_port).show_time()
def show_time(ip):
Action(ip, telnet_port, passwd, FTP_port).show_time()
def periodically_check_all():
for ip in list_engines_IP:
PCFile_name = 'PC_%s_Engine_%s.log' % (
s.time_now_folder(), ip)
Action(ip, telnet_port, passwd, FTP_port).periodic_check(
lstPCCommand, strPCFolder, PCFile_name)
def periodically_check(ip):
PCFile_name = 'PC_%s_Engine_%s.log' % (
s.time_now_folder(), ip)
Action(ip, telnet_port, passwd, FTP_port).periodic_check(
lstPCCommand, strPCFolder, PCFile_name)
def status_for_judging_realtime(ip):
objEngine = Status(ip, telnet_port, passwd, FTP_port)
lstStatus = objEngine.over_all_and_warning()
intUpTimeSec = objEngine.uptime_second()
return lstStatus, intUpTimeSec
def list_status_for_realtime_show():
'''
[['1.1.1.1',0,'2d','M',0,0,0],['1.1.1.1',0,'2d','M',0,1,2]]
'''
lstStatus = []
for i in list_engines_alias:
objEngine = Status(list_engines_IP[i], telnet_port, passwd, FTP_port)
lstStatusWarning = list(objEngine.over_all_and_warning())
return lstStatus
def origin(haap_alias, objEngine):
dicOrigin = {haap_alias: {'ip': objEngine._host}}
if objEngine.dictInfo:
dicOrigin[haap_alias].update(objEngine.dictInfo)
else:
pass
return dicOrigin
def info(haap_alias, objEngine):
lstStatus = objEngine.status_to_show_and_warning()
intUpTimeSec = objEngine.uptime_second()
dicInfo = {haap_alias: {'status': lstStatus[:-1],
'up_sec': intUpTimeSec,
'level': lstStatus[-1]}}
return dicInfo
def data_for_db():
dicInfo = {}
dicOrigin = {}
for i in range(len(list_engines_alias)):
objEngine = Status(list_engines_IP[i], telnet_port, passwd, FTP_port)
dicInfo.update(info(list_engines_alias[i], objEngine))
dicOrigin.update(origin(list_engines_alias[i], objEngine))
return dicOrigin, dicInfo
class Action():
'''
get_trace
pc
backup
change_FW
emc
stt
st
'''
def __init__(self, strIP, intTNPort, strPassword,
intFTPPort, intTimeout=1.5):
self._host = strIP
self._TNport = intTNPort
self._FTPport = intFTPPort
self._password = strPassword
self._timeout = intTimeout
self._TN_Conn = None
self._FTP_Conn = None
self._TN_Connect_Status = None
self._telnet_connect()
self.AHStatus = self._TN_Conn.is_AH()
def _telnet_connect(self):
self._TN_Conn = conn.HAAPConn(self._host,
self._TNport,
self._password,
self._timeout)
self._TN_Connect_Status = self._TN_Conn.get_connection_status()
@s.deco_Exception
def _executeCMD(self, cmd):
if self._TN_Connect_Status:
return self._TN_Conn.exctCMD(cmd)
def _FTP_connect(self):
self._FTP_Conn = conn.FTPConn(self._host,
self._FTPport,
'adminftp',
self._password,
self._timeout)
def _ftp(self):
if self._FTP_Conn:
connFTP = self._FTP_Conn
else:
self._FTP_connect()
connFTP = self._FTP_Conn
return connFTP
@s.deco_OutFromFolder
def backup(self, strBaseFolder):
connFTP = self._ftp()
s.GotoFolder(strBaseFolder)
lstCFGFile = ['automap.cfg', 'cm.cfg', 'san.cfg']
for strCFGFile in lstCFGFile:
if connFTP.GetFile('bin_conf', '.', strCFGFile,
'backup_{}_{}'.format(self._host, strCFGFile)):
print('{} backup completely for {}'.format(
strCFGFile.ljust(12), self._host))
continue
else:
print('{} backup failed for {}'.format(
strCFGFile.ljust(12), self._host))
break
time.sleep(0.25)
@s.deco_Exception
def change_FW(self, strFWFile):
connFTP = self._ftp()
time.sleep(0.25)
connFTP.PutFile('/mbflash', './', 'fwimage', strFWFile)
print('FW upgrade completed for {}, waiting for reboot...'.format(
self._host))
@s.deco_Exception
def auto_commands(self, strCMDFile):
tn = self._TN_Conn
if self.AHStatus:
print("Engine '%s' is at AH status(AH Code %d)"
% (self.host, self.AHStatus))
return
with open(strCMDFile, 'r') as f:
lstCMD = f.readlines()
for strCMD in lstCMD:
strResult = tn.exctCMD(strCMD)
if strResult:
print(strResult)
else:
print('\rExecute command "{}" failed ...'.format(
strCMD))
break
time.sleep(0.5)
@s.deco_OutFromFolder
def get_trace(self, strBaseFolder, intTraceLevel):
tn = self._TN_Conn
connFTP = self._ftp()
def _get_oddCommand(intTraceLevel):
oddCMD = odd()
if intTraceLevel == 1 or intTraceLevel == 2 or intTraceLevel == 3:
oddCMD['Trace'] = 'ftpprep trace'
if intTraceLevel == 2 or intTraceLevel == 3:
oddCMD['Primary'] = 'ftpprep coredump primary all'
if intTraceLevel == 3:
oddCMD['Secondary'] = 'ftpprep coredump secondary all'
return oddCMD
else:
print('Trace level must be: 1 or 2 or 3, please refer "Config.ini" ')
def _get_trace_file(command, strTraceDes):
# TraceDes = Trace Description
def _get_trace_name():
result = tn.exctCMD(command)
reTraceName = re.compile(r'(ftp_data_\d{8}_\d{6}.txt)')
strTraceName = reTraceName.search(result)
if strTraceName:
return strTraceName.group()
else:
print('Generate trace "{}" file failed for "{}"'.format(
strTraceDes, self._host))
trace_name = _get_trace_name()
if trace_name:
time.sleep(0.1)
local_name = 'Trace_{}_{}.log'.format(self._host, strTraceDes)
if connFTP.GetFile('mbtrace', '.', trace_name, local_name):
print('Get trace "{:<10}" for "{}" completed ...'.format(
strTraceDes, self._host))
return True
else:
print('Get trace "{:<10}" for engine "{}" failed!!!\
'.format(strTraceDes, self._host))
# s.ShowErr(self.__class__.__name__,
# sys._getframe().f_code.co_name,
# 'Get Trace "{:<10}" for Engine "{}" Failed!!!\
# '.format(strTraceDes, self._host))
oddCommand = _get_oddCommand(intTraceLevel)
lstCommand = list(oddCommand.values())
lstDescribe = list(oddCommand.keys())
if s.GotoFolder(strBaseFolder):
for i in range(len(lstDescribe)):
_get_trace_file(lstCommand[i], lstDescribe[i])
time.sleep(0.1)
@s.deco_OutFromFolder
def periodic_check(self, lstCommand, strResultFolder, strResultFile):
if self.AHStatus:
print("Engine '%s' is at AH status(AH Code %d)"
% (self.host, self.AHStatus))
return
tn = self._TN_Conn
s.GotoFolder(strResultFolder)
if tn.exctCMD('\n'):
with open(strResultFile, 'w') as f:
for strCMD in lstCommand:
time.sleep(0.1)
strResult = tn.exctCMD(strCMD)
if strResult:
print(strResult)
f.write(strResult)
else:
strErr = '\n*** Execute command "{}" failed\n'.format(
strCMD)
print(strErr)
f.write(strErr)
def set_time(self):
if self.AHStatus:
print("Engine '%s' is at AH status(AH Code %d)"
% (self.host, self.AHStatus))
return
def _exct_cmd():
t = s.TimeNow()
def complete_print(strDesc):
print(' Set %-13s for engine "%s" completed...\
' % ('"%s"' % strDesc, self._host))
time.sleep(0.25)
try:
# Set Time
if self._TN_Conn.exctCMD('rtc set time %d %d %d' % (
t.h(), t.mi(), t.s())):
complete_print('Time')
# Set Date
if self._TN_Conn.exctCMD('rtc set date %d %d %d' % (
t.y() - 2000, t.mo(), t.d())):
complete_print('Date')
# Set Day of the Week
DoW = t.wd() + 2
if DoW == 8:
DoW - 7
if self._TN_Conn.exctCMD('rtc set day %d' % DoW):
complete_print('Day_of_Week')
return True
except Exception as E:
s.ShowErr(self.__class__.__name__,
sys._getframe().f_code.co_name,
'rtc set faild for engine "{}" with error:'.format(
self._host),
'"{}"'.format(E))
if self._TN_Conn:
if _exct_cmd():
print(
'\nSetting time for engine "%s" completed...' %
self._host)
else:
print('\nSetting time for engine "%s" failed!!!' % self._host)
else:
print('\nSetting time for engine "%s" failed!!!' % self._host)
def show_time(self):
if self.AHStatus:
print("Engine '%s' is at AH Status(AH Code %d)"
% (self._host, self.AHStatus))
return
if self._TN_Conn:
try:
print(self._TN_Conn.exctCMD('rtc').replace(
'\nCLI>', '').replace('rtc\r\n', ''))
except BaseException:
print('Get time of engine "%s" failed' % self._host)
class Uptime(object):
"""docstring for uptime"""
def __init__(self, strVPD):
self.vpd = strVPD
self.list_uptime = self._uptime_list()
def _uptime_list(self):
if self.vpd:
reUpTime = re.compile(
r'Uptime\s*:\s*((\d*)d*\s*(\d{2}):(\d{2}):(\d{2}))')
objReUpTime = reUpTime.search(self.vpd)
lstUpTime = []
# add day to list
try:
lstUpTime.append(int(objReUpTime.group(2)))
except ValueError:
lstUpTime.append(0)
# add hr, min, sec to list
for i in range(3, 6):
lstUpTime.append(int(objReUpTime.group(i)))
return lstUpTime
def uptime_list(self):
if self.vpd:
return self._uptime_list()
def uptime_second(self):
uptime_list = self.uptime_list()
if uptime_list:
intSecond = 0
# d, h, m, s means days hours minutes seconds
d = uptime_list[0]
h = uptime_list[1]
m = uptime_list[2]
s = uptime_list[3]
if d:
intSecond += d * 24 * 3600
if h:
intSecond += h * 3600
if m:
intSecond += m * 60
if s:
intSecond += s
return intSecond
else:
return 0
def uptime_to_show(self):
uptime_list = self.uptime_list()
if uptime_list:
# d, h, m, s means days hours minutes seconds
d = uptime_list[0]
h = uptime_list[1]
m = uptime_list[2]
s = uptime_list[3]
if d:
return '%dd %dh %dm' % (d, h, m)
elif h:
return '%dh %dm %ds' % (h, m, s)
elif m:
return '%dm %ds' % (m, s)
else:
return '%d Seconds' % s
class Status(Action):
def __init__(self, strIP, intTNPort, strPassword,
intFTPPort, intTimeout=5):
Action.__init__(self, strIP, intTNPort, strPassword,
intFTPPort, intTimeout)
# self._telnet_connect()
self.dictInfo = self._get_info_to_dict()
if self.dictInfo:
self.objUpTime = Uptime(self.dictInfo['vpd'])
else:
self.objUpTime = None
#print ('dictinfo:',self.dictInfo)
@s.deco_Exception
def _get_info_to_dict(self):
if self.AHStatus:
# print("Engine '%s' is at AH Status(AH Code %d)"
# % (self._host, self.AHStatus))
return
lstCommand = ['vpd', 'engine', 'mirror']
dictInfo = {}
if self._TN_Connect_Status:
for command in lstCommand:
dictInfo[command] = self._executeCMD(command)
time.sleep(0.2)
return dictInfo
def uptime_list(self):
if self.objUpTime:
return self.objUpTime.uptime_list()
def uptime_second(self):
if self.objUpTime:
return self.objUpTime.uptime_second()
else:
return 0
def uptime_to_show(self):
if self.objUpTime:
return self.objUpTime.uptime_to_show()
@s.deco_Exception
def _is_master(self, strEngine):
if strEngine is None:
return 0
if re.search(r'>>', strEngine):
reMaster = re.compile(r'(>>)\s*\d*\s*(\(M\))')
objReMaster = reMaster.search(strEngine)
if objReMaster:
return 1
@s.deco_Exception
def is_master(self):
if self.dictInfo['engine']:
return self._is_master(self.dictInfo['engine'])
def cluster_status(self):
if self.dictInfo['engine']:
if 'offline' in self.dictInfo['engine']:
return 1
else:
return 0
def get_version(self):
if self.dictInfo['vpd'] is None:
return
strVPD = self.dictInfo['vpd']
reFirmWare = re.compile(r'Firmware\sV\d+(.\d+)*')
resultFW = reFirmWare.search(strVPD)
if resultFW:
return resultFW.group().replace('Firmware ', '')
else:
print('Get firmware version failed for engine "%s"' % self._host)
# ## Matt Need to be optimise...
@s.deco_Exception
def get_mirror_status(self):
strMirror = self.dictInfo['mirror']
if strMirror is None:
print('Get mirror status failed for engine "%s"' % self._host)
else:
reMirrorID = re.compile(r'\s\d+\(0x\d+\)') # e.g." 33281(0x8201)"
reNoMirror = re.compile(r'No mirrors defined')
if reMirrorID.search(strMirror):
error_line = ""
reMirrorStatus = re.compile(r'\d+\s\((\D*)\)') # e.g."2 (OK )"
lines = list(filter(None, strMirror.split("\n")))
for line in lines:
if reMirrorID.match(line):
mirror_ok = True
mem_stat = reMirrorStatus.findall(line)
for status in mem_stat:
if status.strip() != 'OK':
mirror_ok = False
if not mirror_ok:
error_line += line + "\n"
if error_line:
# print('mirror:',error_line)
return 1 # means mirror not okay
else:
return 0 # 0 means mirror all okay
else:
if reNoMirror.search(strMirror):
return -1 # -1 means no mirror defined
else:
print('Get mirror status failed for engine "%s"' %
self._host)
# update lststatus
def over_all(self):
'''list of over all'''
lstOverAll = []
lstOverAll.append(self._host)
if self._TN_Connect_Status:
if self.AHStatus:
lstOverAll.append(str(self.AHStatus))
for i in range(4):
lstOverAll.append('--')
else:
lstOverAll.append(0)
lstOverAll.append(self.uptime_to_show())
lstOverAll.append(self.is_master())
lstOverAll.append(self.cluster_status())
lstOverAll.append(self.get_mirror_status())
return lstOverAll
else:
for i in range(5):
lstOverAll.append('--')
return lstOverAll
def status_to_show(self):
lstStatus = self.over_all()
if lstStatus[1] > 0:
pass
elif lstStatus[1] == 0:
lstStatus[1] = 'OK'
# lstStatus[3] means Master Status
if lstStatus[3] == '--':
pass
elif lstStatus[3]:
lstStatus[3] = 'M'
else:
lstStatus[3] = ''
# cluster_status = lstStatus[4]
if lstStatus[4] == '--':
pass
elif lstStatus[4]:
lstStatus[4] = 'Warning'
else:
lstStatus[4] = 'OK'
#mirror_status = lstStatus[5]
if lstStatus[5] == '--':
pass
elif lstStatus[5] == 1:
lstStatus[5] = 'Warning'
elif lstStatus[5] == -1:
lstStatus[5] = 'No Mirror'
else:
lstStatus[5] = 'OK'
return lstStatus
def status_to_show_and_warning(self):
satatus_to_show = self.status_to_show()
lstStatus = self.over_all()
if any([lstStatus[i] for i in [1, 4, 5]]):
if lstStatus[1] == '--':
satatus_to_show.append(1)
else:
satatus_to_show.append(2)
elif not self._TN_Connect_Status:
satatus_to_show.append(1)
else:
satatus_to_show.append(0)
return satatus_to_show
if __name__ == '__main__':
# Paul = Status('10.10.88.94',23,'abc',21,5)
#print(Paul.dictInfo())
pass