/
pmFunctions.py
1196 lines (1143 loc) · 48.4 KB
/
pmFunctions.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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
---ALU Metrocell WMS PowerMenu-----
# This program makes it easier to run
# diagnostic commands from the NESA
# ALU WMS.
#
# Created by Russell Endicott (rendicott@gmail.com)
# 2014-08-19
'''
'''
This PowerMenu module contains misc. functions required for operation
of the PowerMenu. It's primarily for taking bulky static code out of the
main pm.py file.
'''
#Standard Library imports
import re
import time
import datetime
import os
import sys
import getopt
import operator
import pickle
import logging
import inspect
import gc
import getpass
import zipfile
import zlib
#internal modules imports
from pmClasses import eeker # super important that this is first
import pmConfig
import pmLoaders
import pmClasses
from pmConfig import sversion
from pmConfig import header_alarm
from pmConfig import header_status
from pmConfig import header_metro
from pmConfig import header_metrofull
from pmConfig import sep_status
from pmConfig import sep_metro
from pmConfig import sep_metrofull
from pmConfig import sep_alarm
from pmConfig import header_alarm_csv
from pmConfig import header_metro_csv
from pmConfig import header_metro_full_csv
from pmConfig import header_status_csv
#create a new list of filelog class type
file_log = pmClasses.FileLog()
def return_file_log():
return file_log
def printLogoIntro():
print ""
print " _____ __ __ "
print "| __ \ | \/ | "
print "| |__) |____ _____ _ __| \ / | ___ _ __ _ _ "
print "| ___/ _ \ \ /\ / / _ \ '__| |\/| |/ _ \ '_ \| | | | "
print "| | | (_) \ V V / __/ | | | | | __/ | | | |_| | "
print "|_| \___/ \_/\_/ \___|_| |_| |_|\___|_| |_|\__,_| "
print " === ALU WMS Metrocell Power Menu ==="
print " | Creator: rendicott@gmail.com"
print " | Date: 2014-11-20"
print " | Version: " + pmConfig.sversion
print " ===================================="
print ""
def coloredList(list,
type,
thetimeyouwant,
datasourcelist,
showfgwbool,
suppress_csv=None):
myfunc = str(pmLoaders.giveupthefunc())
'''
list: Some sort of list of results you want displayed
type: options are "alarm, metro, metrofull, status"
#the 'type' specifies which footer to put on the data for column names.
thetimeyouwant: either a timestamp string for override or 'NA'
datasourcelist: either the inventory source or 'NA'
showfgwbool: (True/False) whether you want full fgw name in alarmedcomponent
suppress_csv: Boolean: Whether or not to bypass writing CSV
'''
#takes a list of objects and prints it in color
if suppress_csv==None:
suppress_csv = False
#first define the function that just dumps the raw colored data
def dumpdata_status(list,chosencolor):
colorflag = True
for item in list:
if colorflag and chosencolor != '':
print('\033[1;' + chosencolor +
item.rowstatus(chosencolor) + '\033[m')
if colorflag and chosencolor == '':
print item.rowstatus(chosencolor)
if not colorflag:
print item.rowstatus(chosencolor)
colorflag = not colorflag
def dumpdata_rowformat(list,chosencolor):
logging.debug("\t I'm inside the dumpdata_rowformat function")
colorflag = True
for item in list:
if colorflag and chosencolor != '':
print('\033[1;' + chosencolor +
item.rowformat(chosencolor) + '\033[m')
if colorflag and chosencolor == '':
print item.rowformat(chosencolor)
if not colorflag:
print item.rowformat(chosencolor)
colorflag = not colorflag
logging.debug("\t I'm leaving the dumpdata_rowformat function")
def dumpdata_rowformat_full(list,chosencolor):
logging.debug("\t I'm inside the dumpdata_rowformat_full function")
colorflag = True
for item in list:
if colorflag and chosencolor != '':
print('\033[1;' + chosencolor +
item.rowformat_full(chosencolor) + '\033[m')
if colorflag and chosencolor == '':
print item.rowformat_full(chosencolor)
if not colorflag:
print item.rowformat_full(chosencolor)
colorflag = not colorflag
logging.debug("\t I'm leaving the dumpdata_rowformat_full function")
print ""
try:
if thetimeyouwant != 'NA':
#meaning the function wanted to give a specific time
print "\t\t\t Data Timestamp: " + thetimeyouwant
else:
#we'll just pull timestamps from the data source
print("\t\t ipsecIpAddr's updated: " +
datasourcelist.timeupdated_bsg +
"\t Everything else updated: " +
datasourcelist.timeupdated_wms
)
except:
#for some reason couldn't process function arguments for timestamp
print "\t\t\t Data Timestamp = NA"
'''now define the type sections, this is mainly for separators
and column header lists'''
if type == 'status':
print sep_status
dumpdata_status(list,eeker.chosencolor)
print sep_status
print header_status
elif type == 'metro':
if eeker.fullformat:
print sep_metrofull
dumpdata_rowformat_full(list,eeker.chosencolor)
print sep_metrofull
print header_metrofull
print ""
else:
print sep_metro
dumpdata_rowformat(list,eeker.chosencolor)
print sep_metro
print header_metro
print ""
elif type == 'metrofull':
print sep_metrofull
dumpdata_rowformat_full(list,eeker.chosencolor)
print sep_metrofull
print header_metrofull
print ""
elif type == 'alarm':
try:
print sep_alarm
dumpdata_rowformat(list,eeker.chosencolor)
print sep_alarm
print header_alarm
print ""
except ValueError:
print("Something went wrong printing the table header row: '" +
str(sys.exc_info()[0]) + "'")
else:
print "============================================================="
for o in list:
print o.rowformat(eeker.chosencolor)
print "============================================================="
print "Number of results: " + str(len(list))
logging.debug(myfunc + '\t' +
"Search returned number of results: " + str(len(list)))
if eeker.csvdumpbool and not suppress_csv:
#now try and write the results to csv
logging.debug(myfunc + '\t' +
"Now attempting to send list to write_to_csv function")
write_to_csv(list,tag=type)
list = []
def write_to_csv(listofobj,tag=None,filename=None):
'''Takes a list of objects and a custom tagword then writes the
list of objects to csv with the "timestamp-tag.csv" format.
(e.g, 20141027_083000-alarms.csv)'''
myfunc = str(pmLoaders.giveupthefunc())
global lastresultfile
global file_log
#handle default tag if none is given
if tag == None:
tag = 'generic'
logging.debug(myfunc + '\t' + "Got list of length " + str(len(listofobj)))
logging.debug(myfunc + '\t' + "Got tag of type: " + str(tag))
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
if filename==None:
if eeker.fullformat and tag == 'metro':
filename = timestamp + '-' + 'metrofull' + '.csv'
else:
filename = timestamp + '-' + tag + '.csv'
else:
filename = filename + timestamp + '.csv'
try:
#first write the column headers (defined in global)
with open(filename,'wb') as f:
if tag == 'alarm':
f.write(header_alarm_csv + '\r')
elif tag == 'metro':
if eeker.fullformat:
f.write(header_metro_full_csv + '\r')
else:
f.write(header_metro_csv + '\r')
elif tag == 'metrofull':
f.write(header_metro_full_csv + '\r')
elif tag == 'status':
f.write(header_status_csv + '\r')
#now loop through the list and write data to csv
for o in listofobj:
try:
if tag == 'status':
f.write(o.csvformat_status() + '\r')
elif tag == 'metro' and eeker.fullformat:
f.write(o.csvformat_full() + '\r')
else:
f.write(o.csvformat() + '\r')
except Exception as ee:
f.write("ERROR OBJECT," + str(o) + '\r')
logging.error(myfunc + '\t' +
"Got exception with object " + str(o) + " : " + str(ee))
try:
env_abspath = os.path.abspath(filename)
print("\t\t\t\t\t\t\t\t\t\t\t\t\t WROTE ABOVE RESULTS TO : " +
env_abspath)
except Exception as ee:
logging.error(myfunc + '\t' +
"Exception pulling filename absolute path: " + str(ee))
env_abspath = ''
except Exception as e:
logging.critical(myfunc + '\t' + "Got exception writing csv data:" +
str(e))
#store the last filename in case we want to email it or something later
file_log.append(pmClasses.File_Log_Entry(env_abspath,filename,timestamp))
logging.debug(myfunc + '\t' + "Lenth of file_log: " + str(len(file_log)))
def email_result(subject=None,body=None,email=None,howmany=None):
myfunc = str(pmLoaders.giveupthefunc())
#make sure we're working with the global file_log
global file_log
#first check to see if there's anything in file_log
# if not, then we can't do anything
if len(file_log) != 0:
if howmany==None:
'''pull the last file log object from the file_log and add
it to a list'''
filelist = file_log.getlast()
else:
filelist = file_log.getlast(howmany)
'''
>>> print stringlist[:len(stringlist)-3:-1]
['dog', 'lazy']
'''
logging.debug(myfunc + '\t' +
"Length of file_list: " + str(len(filelist)))
if subject==None:
subject="PowerMenu Query Result"
if body==None:
body=("This report was generated by the Operations PowerMenu."
" Created by rendicott@gmail.com")
if email==None:
userinput_email = ''
try:
while userinput_email != 'q':
try:
userinput_email = eeker.macrolist.pop()
except:
userinput_email = raw_input(
"\t Enter an email address ('q' to cancel): ")
logging.debug(myfunc + '\t' +
"Got raw userinput_email: " +
str(userinput_email))
logging.debug(myfunc + '\t' +
"Validating userinput_email with regex...")
re_userinput_email = re.compile('(.*)(@)(.*)(\.)(.*)')
userinput_email_match = re.search(
re_userinput_email,userinput_email)
logging.debug(myfunc + '\t' +
"Successful regex email match? = " +
str(userinput_email_match))
if userinput_email_match:
email = userinput_email_match.group()
break
else:
print "Bad email format detected, try again."
except Exception as h:
logging.critical(myfunc + '\t' +
"Exception parsing email address: " + str(h))
successflag = False
try:
print "Sending email with the following information: "
print ""
print "\t Subject: \t" + subject
print "\t Body: \t\t" + body
print "\t Attach: \t",
for i,file in enumerate(filelist):
if i == len(filelist)-1:
print file.filename
else:
print file.filename + ",",
logging.debug(myfunc + '\t' +
"Now trying to print email address to screen...")
print "\t Recipient:\t" + email
print ""
try:
'''Now we'll try and build the 'mailx' command. it's a
strange one, got this command structure courtesy
of Lee Murphy (lm3521)'''
#first we have to write the body to a file
try:
with open('body.txt','wb') as f:
#Had to add the \n or else you get weird problems
f.write(body + '\n')
except Exception as ae:
logging.critical(myfunc + '\t' +
"Exception writing 'body' to body.txt :" + str(ae))
emailcommand_part01 = '( cat body.txt;'
emailcommand_part03 = (' ) | mailx -s "' +
subject + '" ' + email )
emailcommand_part02 = ''
for file in filelist:
emailcommand_part02 += (' uuencode ' + file.filename +
' ' + file.filename + ';')
emailcommand = (emailcommand_part01 + emailcommand_part02 +
emailcommand_part03)
logging.debug(myfunc + '\t' +
"Final email command is: " + emailcommand)
try:
logging.debug(myfunc + '\t' + pmConfig.rob)
email_result_file = os.popen(emailcommand)
email_result = email_result_file.readlines()
email_result_length = len(email_result)
for i,line in enumerate(email_result):
print("\t\t\t email_result line (" + str(i) + " of " +
str(remail_result_length) + ") :" + str(line))
if line != '':
print "Something went wrong: ", line
break
else:
successflag = True
break
if successflag:
print "Your email should arrive in the next 5 minutes."
except Exception as ee:
logging.critical(myfunc + '\t' +
"Exception issuing command: " + str(ee))
logging.debug(myfunc + '\t' +
"Command was: " + emailcommand)
except Exception as ef:
logging.critical(myfunc + '\t' +
"Got exception parsing email command parameters:" +
str(ef))
except Exception as e:
logging.critical(myfunc + '\t' + "Exception: " + str(e))
else:
print("Oops, the file_log history is empty. Are you sure you have "
"CSV dumping turned on? (HINT: 'n' to toggle, 'i' to check)")
def syslog_write_to_disk(loo_femtos):
''' takes a list of femto objects and then
writes the contents of their .list_rawdata property
to a file on disk.
Then it adds those files to a compressed zip archive
and adds the zip to the file_log so it can be emailed.
'''
myfunc = str(pmLoaders.giveupthefunc())
logging.debug(myfunc + '\t' +
"Received loo_femtos of length: " + str(len(loo_femtos)))
global file_log
t_filelist = []
for obj in loo_femtos:
nowtime = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
filename = nowtime + "-" + obj.bsrName + '.txt'
t_filelist.append(filename)
with open(filename,'wb') as f:
for line in obj.list_rawdata:
f.write(line)
logging.debug(myfunc + '\t' +
"Number of files written to disk: " + str(len(t_filelist)))
# now zip up the files
compression = zipfile.ZIP_DEFLATED
nowtime = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
zipfilename = nowtime + '-bsr_syslogs.zip'
try:
zf = zipfile.ZipFile(zipfilename,mode='w')
for f in t_filelist:
zf.write(f,compress_type=compression)
zf.close()
logging.debug(myfunc + '\t' +
"Wrote the following zipfile: " + zipfilename)
except Exception as ee:
logging.critical(myfunc + '\t' +
"Exception trying to write zip file: " + zipfilename
+ " - " + ee)
# now delete the raw syslogs to save space
try:
for f in t_filelist:
os.remove(f)
logging.debug(myfunc + '\t' +
"Deleted raw syslog txt files")
except Exception as ee:
logging.critical(myfunc + '\t' +
"Exception trying to delete raw data txt files: "+ ee)
# now go through and clear out the rawdata attached to femto object
for o in loo_femtos:
o.list_rawdata = []
logging.debug(myfunc + '\t' +
"Blanked out .list_rawdata property on all femtos in loo_femtos")
try:
env_abspath = os.path.abspath(zipfilename)
print("\t\t\t\t\t\t\t\t\t\t\t\t\t WROTE SYSLOGS TO : " +
env_abspath)
print("\t\t\t\t\t\t\t\t\t\t\t\t (Use e+N function to email zip file)")
# append zip file to file log
file_log.append(pmClasses.File_Log_Entry(env_abspath,zipfilename,nowtime))
except Exception as ee:
logging.error(myfunc + '\t' +
"Exception pulling filename absolute path: " + str(ee))
env_abspath = ''
def netwalk_export(resultlist):
''' Takes the results from a parse_ap_console_dump and builds
the xml file and adds it to the global file log for emailing.
'''
myfunc = str(pmLoaders.giveupthefunc())
# first, generate the actual XML
xmlstring = netwalk_genxml(resultlist)
global file_log
#pull site groupName to use as filename
try:
sitename = resultlist[0].groupName
except Exception as ex_sname:
logging.debug(myfunc + '\t' +
'Exception pulling sitename for xml filename: ' + str(ex_sname))
sitename = 'GENERICSITE'
nowtime = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
xmlfilename = 'nwinput-' + sitename + "--" + nowtime + '.xml'
xmlfilename = xmlfilename.replace(' ','_')
logging.debug(myfunc + '\t' +
"Generated filename: '" + xmlfilename + "'")
# try writing the file to disk
try:
with open(xmlfilename,'wb') as f:
fluffstring = '<?xml version="1.0" encoding="UTF-8"?>'
f.write(fluffstring)
f.write(xmlstring)
except Exception as ex_xwrfile:
logging.info(myfunc + '\t' +
"Exception writing to xml file: " + str(ex_xwrfile))
try:
env_abspath = os.path.abspath(xmlfilename)
print("\t\t\t\t\t\t\t\t\t\t\t\t\t WROTE XML TO : " +
env_abspath)
# append xml file to file log
file_log.append(pmClasses.File_Log_Entry(env_abspath,xmlfilename,nowtime))
except Exception as ee:
logging.error(myfunc + '\t' +
"Exception pulling filename absolute path: " + str(ee))
env_abspath = ''
def netwalk_genxml(resultlist):
''' Takes results from a parse_ap_console_dump and generates
xml for the file for the NetWalk API.
'''
myfunc = str(pmLoaders.giveupthefunc())
def grand():
''' Generates random number from pmClasses func'''
return(str(pmClasses.get_random_number(10)))
authpossibilities = [ {
'u': 'scs_admin',
'p': 'INSERTPASSWORDHERE',
},
{
'u': 'scr_admin',
'p': 'INSERTPASSWORDHERE',
},
{
'u': 'admin',
'p': 'INSERTPASSWORDHERE',
},
{
'u': 'admin',
'p': 'INSERTPASSWORDHERE',
},
]
try:
sitename = resultlist[0].groupName
except Exception as ex_sname:
logging.debug(myfunc + '\t' +
'Exception pulling sitename for xml generation: ' + str(ex_sname))
sitename = 'GENERICSITE'
import xml.etree.ElementTree as ET
# generate the entire netwalk-input xml root structure
try:
x_root = ET.Element('root')
x_tgt = ET.SubElement(x_root,'target',attrib={'id':grand()})
# find unique fpips in resultlist
for fpip in set(map((lambda x: x.fpip),resultlist)):
if fpip == '0.0.0.0' or fpip == '':
break
x_tgt_ent = ET.SubElement(x_tgt,'entrypoint',attrib={'id':grand()})
x_tgt_ent_attr = ET.SubElement(x_tgt_ent,'attributes')
x_tgt_ent_attr_ipaddr = ET.SubElement(x_tgt_ent_attr,'ipaddr')
x_tgt_ent_attr_port = ET.SubElement(x_tgt_ent_attr,'port')
x_tgt_ent_attr_hostname = ET.SubElement(x_tgt_ent_attr,'hostname')
x_tgt_ent_attr_ipaddr.text = fpip
x_tgt_ent_attr_port.text = '22'
x_tgt_ent_attr_hostname.text = 'SCSR--' + sitename + '-' + grand()
x_tgt_auth = ET.SubElement(x_tgt,'auth')
for poss in authpossibilities:
x_tgt_auth_poss = ET.SubElement(x_tgt_auth,'possibility',attrib={'id':grand()})
x_tgt_auth_poss_u = ET.SubElement(x_tgt_auth_poss,'username')
x_tgt_auth_poss_p = ET.SubElement(x_tgt_auth_poss,'password')
x_tgt_auth_poss_u.text = poss.get('u')
x_tgt_auth_poss_p.text = poss.get('p')
# Now create the members section and generate the member xml from resultlist
x_tgt_members = ET.SubElement(x_tgt,'members')
for metrocell in resultlist:
try:
x_tgt_members.append(metrocell.genxml())
except Exception as mex:
logging.debug(myfunc + '\t' +
"Exception Metrocell.genxml(): " + str(mex))
except Exception as nwex:
logging.debug(myfunc + '\t' +
"Exception netwalk_export() gen XML: " + str(nwex))
# finally, return the xml as string
try:
xmlstring = ET.tostring(x_root)
return(xmlstring)
msg = ":::::::::::BEGIN GENERATED XML STRING:::::::::::::::\n\r"
msg += xmlstring
msg += "\n\r:::::::::::END GENERATED XML STRING:::::::::::::::"
logging.debug(myfunc + '\t' + msg)
except Exception as exroot:
logging.debug(myfunc + '\t' +
"Exception returning x_root string: " + str(exroot))
logging.debug(myfunc + '\t' +
"::::::::::::::EXITING NETWALK_EXPORT:::::::::::")
#def find_stuck_alarms(inventorydb,loo_alarmdb):
def find_stuck_alarms():
myfunc = str(pmLoaders.giveupthefunc())
monfilter = True
bsroosfilter = True
flipbackcsvdump = False
rundetailed = True
verbosebool = False
emailbool = False
'''Below is a list of types of Metrocell AP alarms that are detectable
and undetectable by this function'''
list_detectable = ['bsrOutOfService',
'ipsecTunnelFailureNoResponse',
'sctpAssocAssociationDownAlarm',
'associationEstablishmentFailure',
'noResponseFromBGWY',
'csCnConnectivityLost',]
list_undetectable = ['lmcFailure','securityViolation']
def sifthealthy(stucklist,bsrlist):
myfunc_i = str(pmLoaders.giveupthefunc())
for obj in bsrlist:
try:
obj.healthy
except:
continue
if not obj.healthy:
for i,alarm in enumerate(stucklist):
try:
if obj.bsrName == alarm.bSRName:
stucklist.remove(alarm)
except Exception as f:
logging.debug(myfunc_i + '\t' +
("SIFTHEALTHY: Exception after cycling through "
"alarm list and removing alarms for ") +
obj.bsrName + " : " + str(f))
return stucklist
try:
wantseenotmon = eeker.macrolist.pop()
except:
wantseenotmon = raw_input(
"Do you want to see 'Not Monitored' AP's? (y/n): ")
if wantseenotmon == 'y':
monfilter = False
print ""
print "Types of stuck BSR alarms I can detect:"
for o in list_detectable:
print "\t DETECTABLE: " + o
print ""
print "Types of stuck BSR alarms I can't verify: "
for p in list_undetectable:
print "\t UNDETECTABLE: " + p
print ""
try:
wantlimitbsroos = eeker.macrolist.pop()
except:
wantlimitbsroos = raw_input(
"Do you want to limit search to 'bsrOutOfService' "
"alarms only? (y/n): ")
if wantlimitbsroos == 'n':
bsroosfilter = False
try:
wantseeverbose = eeker.macrolist.pop()
except:
wantseeverbose = raw_input(
"Do you want to see verbose results? (y/n): ")
if wantseeverbose == 'y':
verbosebool = True
if not eeker.csvdumpbool:
try:
fliponcsv = eeker.macrolist.pop()
except:
fliponcsv = raw_input(
"I noticed you had CSV output turned off. Do you want "
"to output results to CSV? (y/n) ")
if fliponcsv == 'y':
flipbackcsvdump = True
eeker.csvdumpbool = True
'''now check to see if user wants email. Since we can't email
unless csvdump is on, we have to check for it before asking.'''
if eeker.csvdumpbool:
try:
wantemail = eeker.macrolist.pop()
except:
wantemail = raw_input("Do you want to email the results? (y/n) ")
if wantemail == 'y':
try:
email_addy = eeker.macrolist.pop()
except:
email_addy = raw_input("\tEnter a single email address: ")
emailbool = True
print ""
print "Ok, checking for stuck alarms, this might take a bit..."
logging.info(myfunc + '\t' + "checking for stuck alarms...")
bsroosList = []
print("NOTE: AP's with administrativeState = 'locked' are "
"filtered from this check.")
eeker.dblock_alarmso.acquire()
for k in eeker.db_alarmso:
if k.adminStatus != 'locked':
if monfilter and bsroosfilter:
if (k.specificProblems == 'bsrOutOfService' and
k.monitoredStatus == 'Monitored'):
bsroosList.append(k)
if not monfilter and bsroosfilter:
if k.specificProblems == 'bsrOutOfService':
bsroosList.append(k)
#oos filter is off so we want all bsr related alarms
if not monfilter and not bsroosfilter:
if k.bSRName != '':
bsroosList.append(k)
if monfilter and not bsroosfilter:
if k.bSRName != '' and k.monitoredStatus == 'Monitored':
for alarmtext in list_detectable:
if k.specificProblems == alarmtext:
bsroosList.append(k)
eeker.dblock_alarmso.acquire()
print ""
if verbosebool:
try:
wantseebsralarms = eeker.macrolist.pop()
except:
wantseebsralarms = raw_input(
"Do you want to see list of BSR alarms? (y/n) ")
if wantseebsralarms == 'y':
eeker.dblock_alarmso.acquire()
coloredList(
bsroosList,
'alarm',
eeker.db_alarmso.timeupdated_alarm,
'NA',
True)
eeker.dblock_alarmso.release()
logging.info(myfunc + '\t' +
"Number of BSR OOS with alarm: " + str(len(bsroosList)))
objbsroosList = []
stuckalarmList = []
re_ipsecip_111 = '\.'
re_ipsecip_11 = re.compile(re_ipsecip_111)
print ""
print("Now checking status of alarmed AP's, "
"this could take up to 60 seconds...")
print ""
for bsroos in bsroosList:
mybsr = bsroos.bSRName
eeker.dblock.acquire()
for bsr in eeker.db_femtos:
match_ip = re.search(re_ipsecip_11,bsr.ipsecIpAddr)
if mybsr == bsr.bsrName:
if monfilter:
if (match_ip and
bsr.monitoredStatus == 'Monitored' and
bsr.adminStatus == 'unlocked'):
objbsroosList.append(bsr)
stuckalarmList.append(bsroos)
print ".",
if not monfilter:
if match_ip and bsr.adminStatus == 'unlocked':
objbsroosList.append(bsr)
stuckalarmList.append(bsroos)
print ".",
eeker.dblock.release()
logging.info(myfunc + '\t' +
"Number of BSRs with ipSecIpAddr's but still have alarm: " +
str(len(objbsroosList)))
print "Showing list of BSR's that have Alarms but appear to be online..."
print ""
coloredList(objbsroosList,'metro','NA',eeker.db_femtos,False)
sortedby='eventTime'
stuckalarmList.sort(key=operator.attrgetter(sortedby))
if verbosebool:
print "Showing initial list of likely stuck alarms..."
eeker.dblock_alarmso.acquire()
coloredList(
stuckalarmList,
'alarm',
eeker.db_alarmso.timeupdated_alarm,
'NA',
True)
eeker.dblock_alarmso.release()
if verbosebool:
try:
wantdetailedstatus = eeker.macrolist.pop()
except:
question_detailed = ("Do you want to run detailed status "
"checks on the list of APs that may have "
"stuck alarms? (y/n) ")
wantdetailedstatus = raw_input(question_detailed)
if wantdetailedstatus == 'y':
pmLoaders.pull_ap_console_dump(
objbsroosList,
False
)
#now write bsr list to csv without displaying on screen
logging.info(myfunc + '\t' +
"Now attempting to send list to write_to_csv function")
write_to_csv(objbsroosList,'metro','suspect-BSRs')
else:
rundetailed = False
else:
print("Now running detailed status check on APs with "
"potential stuck alarms...")
pmLoaders.pull_ap_console_dump(
objbsroosList,
False
)
#now write bsr list to csv without displaying on screen
logging.info(myfunc + '\t' +
"Now attempting to send list to write_to_csv function")
write_to_csv(objbsroosList,'metro','suspect-BSRs')
if rundetailed:
print ""
print "Now doing final comparison of AP's to OOS alarms..."
logging.debug(myfunc + '\t' +
"Before entering SIFTHEALTHY, length of 'stuckalarmList' = " +
str(len(stuckalarmList)))
logging.debug(myfunc + '\t' +
"Before entering SIFTHEALTHY, length of 'objbsroosList' = " +
str(len(objbsroosList)))
stuckalarmList02 = sifthealthy(stuckalarmList,objbsroosList)
logging.debug(myfunc + '\t' +
"After SIFTHEALTHY, length of 'stuckalarmList02' = " +
str(len(stuckalarmList02)))
logging.debug(myfunc + '\t' +
"After SIFTHEALTHY, length of 'objbsroosList' = " +
str(len(objbsroosList)))
print ""
print "Showing final list of stuck alarms..."
print ""
eeker.dblock_alarmso.acquire()
coloredList(
stuckalarmList02,
'alarm',
eeker.db_alarmso.timeupdated_alarm,
'NA',
True,
True)
eeker.dblock_alarmso.release()
write_to_csv(stuckalarmList02,'alarm','stuck-BSR-alarms')
else:
print("You chose not to run detailed checks. In that case, "
"here's the best guess: ")
eeker.dblock_alarmso.acquire()
coloredList(
stuckalarmList,
'alarm',
eeker.db_alarmso.timeupdated_alarm,
'NA',
True,
True)
eeker.dblock_alarmso.release()
write_to_csv(stuckalarmList,'alarm','stuck-BSR-alarms')
if emailbool:
body = ("Refer to the attached CSVs for stuck alarms "
"and list of corresponding BSR's.")
email_result('PowerMenu Stuck Alarms Report',
body,
email_addy,
2)
'''since we suppressed the writing of the stuck alarms csv through
coloredList we'll write one manually with our own custom filename
flip the CSV dump back off if it was off entering the function'''
if flipbackcsvdump:
eeker.csvdumpbool = False
#empty out unused lists
bsroosList = []
objbsroosList = []
stuckalarmList = []
#def reboot_ap(cmid,inventory):
def reboot_ap(cmid):
myfunc = str(pmLoaders.giveupthefunc())
print "Entering AP Reboot function."
logging.debug(myfunc + '\t' + "Got cmid of: '" + cmid + "'")
from pmConfig import femtostatus_script,reboot_script
fgwname = ''
mobject = ''
fgwip = ''
mip = ''
scannedser = ''
eeker.dblock.acquire()
for o in eeker.db_femtos:
if o.bsrName == cmid:
'''take this Metrocell object and hold it in case we need
more info from it'''
mobject = o
break
try:
fgwname = mobject.fgw
logging.debug(myfunc + '\t' + "Got fgw of: '" + fgwname + "'")
#cycle through fgwList to find IP
for i in pmConfig.fgwList:
if i.get('fgwName') == fgwname:
fgwip = i.get('fgwIP')
break
mip = mobject.ipsecIpAddr
logging.debug(myfunc + '\t' + "Got mip of: '" + mip + "'")
print "Checking to see if I have enough info.....",
if mip != 'NA':
''' First we need to check to see if it's 14.2 AP for new
passwords. Otherwise use the old passwords '''
if '14' in mobject.swVersion:
command_c = (femtostatus_script + ' ' +
fgwip + ' ' + mip + ' ' + eeker.fgwpw + ' ' +
eeker.apnopw + ' ' + eeker.apnapw + ' ' +
eeker.femtostatus_timeout)
command_r = (reboot_script + ' ' + fgwip + ' ' + mip + ' ' +
eeker.fgwpw + ' ' + eeker.apnopw + ' ' +
eeker.apnapw + ' ' + eeker.femtostatus_timeout)
logging.debug(myfunc + '\t' +
"Detected '14' in swVersion field, using new pwds")
else:
command_c = (femtostatus_script + ' ' +
fgwip + ' ' + mip + ' ' + eeker.fgwpw + ' ' +
eeker.apopw + ' ' + eeker.apapw + ' ' +
eeker.femtostatus_timeout)
command_r = (reboot_script + ' ' + fgwip + ' ' + mip + ' ' +
eeker.fgwpw + ' ' + eeker.apopw + ' ' +
eeker.apapw + ' ' + eeker.femtostatus_timeout)
logging.debug(myfunc + '\t' +
"Did not detect '14' in swVersion field, using old pwds")
print("\tFirst I'll connect to the AP and check "
"it's serial number...")
try:
logging.info(myfunc + '\t' + pmConfig.rob + command_c)
#print command_c
command_c_output = pmLoaders.runoscommand(command_c)
re_serial_111 = '( FSN )(?P<serial>\d*)'
re_serial_11 = re.compile(re_serial_111)
for o in command_c_output:
match_serial = re.search(re_serial_11,o)
if match_serial:
scannedser = match_serial.group('serial')
logging.debug(myfunc + '\t' +
"Detected serial from expect script: " + scannedser)
except Exception as aoe:
logging.debug(myfunc + '\t' +
"Exception running checker script: " + str(aoe))
logging.debug(myfunc + '\t' +
"Serial from existing inventory: " + mobject.serial)
print("\tI connected to the AP and saw that it "
"has serial number:\t" + scannedser)
print("\tThe serial I have in my database "
"for that AP is:\t\t" + mobject.serial)
if scannedser == mobject.serial:
print "\tI feel confident in running the reboot command."
print ""
try:
reboot_areyousure =("\tAre you absolutely sure you "
"want to reboot '" + mobject.bsrName +
"'? (y/n) ")
try:
suretoreboot = macrolist.pop()
except:
suretoreboot = raw_input(reboot_areyousure)
if suretoreboot == 'y':
logging.info(myfunc + '\t' + pmConfig.rob + command_r)
#print command_r
output = pmLoaders.runoscommand(command_r)
print ""
for line in output:
print '\t\t\t\t' + line.strip('\n')
print ""
print "\t\tI sent the reboot command! "
print("\t\tIn about 10 minutes refresh BSR "
"ipSecIpAddr registrations with command '112'")
print("\t\tAfter that you should be "
"able to status the AP")
except Exception as oor:
logging.debug(myfunc + '\t' +
"Exception running reboot script: " + str(oor))
else:
print "\tSerials do not match, too risky to run reboot command"
else:
print "FAILED"
listy = []
listy.append(mobject)
coloredList(listy,'metro','NA',eeker.db_femtos,True)
print ""
print("That AP appears to be offline. I have no "
"ipSecIPAddr to try and connect to.")
except Exception as hoho:
logging.debug(myfunc + '\t' +
"Exception running reboot function: " + str(hoho))
print "Something went wrong. Are you sure you entered a real commonID?"
def querybuilder():
funcname = str(pmLoaders.giveupthefunc())
print("\t\t This function allows you to build custom queries " +
"on the database. You'll be asked a series of questions regarding the " +
"Metrocell display fields. You'll eithe provide a 'yes/no' answer or " +
"provide a regular expression pattern for the specific field.")
print("")
print("\t\tTIPS:")
print("\t\t\tTo match everything except swVersion '14.02' use the regular expression 'BSR-(?!14.02).*'")
print("\t\t\tTo match only 'Monitored' AP's use '^monitored'")
print("\t\t\tWildcard character is '.*' EXAMPLE: '.*visa.*' to show anything with 'visa' in the name.")
print("")
print("\t\tProvide '+++' to any question to exit the query builder.")
print("")
field_bsrName = {'displayname':'bsrName',
'fieldname':'bsrName',
'pattern':'',
'pattern_re':'',
'bool': False}
field_groupName = {'displayname':'groupName',
'fieldname':'groupName',