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', 'pattern':'', 'pattern_re':'', 'bool': False} field_usecase = {'displayname':'usecase', 'fieldname':'usecase', 'pattern':'', 'pattern_re':'', 'bool': False} field_ctsname = {'displayname':'ctsEquipName', 'fieldname':'ctsname', 'pattern':'', 'pattern_re':'', 'bool': False} field_monitoredStatus = {'displayname':'Monitored?', 'fieldname':'monitoredStatus', 'pattern':'', 'pattern_re':'', 'bool': False} field_ipsecIpAddr = {'displayname':'ipsecIpAddr', 'fieldname':'ipsecIpAddr', 'pattern':'', 'pattern_re':'', 'bool': True} field_clusterId = {'displayname':'clstr', 'fieldname':'clusterId', 'pattern':'', 'pattern_re':'', 'bool': False} field_groupId = {'displayname':'grp', 'fieldname':'groupId', 'pattern':'', 'pattern_re':'', 'bool': False} field_fgw = {'displayname':'fgw', 'fieldname':'fgw', 'pattern':'', 'pattern_re':'', 'bool': False} field_usid = {'displayname':'usid', 'fieldname':'usid', 'pattern':'', 'pattern_re':'', 'bool': False} field_swVersion = {'displayname':'swVersion', 'fieldname':'swVersion', 'pattern':'', 'pattern_re':'', 'bool': False} field_adminStatus = {'displayname':'adminStatus', 'fieldname':'adminStatus', 'pattern':'', 'pattern_re':'', 'bool': False} field_inalarm = {'displayname':'alarm?', 'fieldname':'inalarm', 'pattern:':'', 'pattern_re':'', 'bool': True} lod_fields = [field_bsrName, field_groupName, field_usecase, field_ctsname, field_monitoredStatus, field_ipsecIpAddr, field_clusterId, field_groupId, field_fgw, field_usid, field_swVersion, field_adminStatus, field_inalarm] # now go through and collect data for the regex pattern fields exitflag = False for field in lod_fields: if not field.get('bool'): try: field['pattern'] = eeker.macrolist.pop() except: field['pattern'] = raw_input("\t\t\t Regex pattern for '" + field.get('displayname') + "' field. (enter for all): ") if field.get('pattern') == '+++': exitflag = True break elif field.get('pattern') == '': field['pattern'] = '.*' try: field['pattern_re'] = re.compile(field.get('pattern'),re.IGNORECASE) except: logging.debug('\t' + funcname + "Error compiling regex for field['displayname']: " + field.get('displayname')) field['pattern'] = '.*' # Handle the remaining yes/no questions print "" bool_onlyalarmed = False if not exitflag: try: field_inalarm['pattern'] = eeker.macrolist.pop() except: field_inalarm['pattern'] = raw_input("\t\tDo you want to only display AP's with alarms? (y/n): ") if field_inalarm['pattern'] == '+++': exitflag = True if field_inalarm['pattern'] == 'y': bool_onlyalarmed = True else: bool_onlyalarmed = False bool_onlyoffline = False if not exitflag: try: field_ipsecIpAddr['pattern'] = eeker.macrolist.pop() except: field_ipsecIpAddr['pattern'] = raw_input("\t\tDo you want to only display AP's that are offline? (y/n): ") if field_ipsecIpAddr['pattern'] == '+++': exitflag = True if field_ipsecIpAddr['pattern'] == 'y': bool_onlyoffline = True else: bool_onlyoffline = False # now we have a list of dictionaries with the fields we want and the desired search patterns # now build a result list based on the query def matches(obj_femto): match_groupName = re.search(field_groupName['pattern_re'],obj_femto.groupName) match_usecase = re.search(field_usecase['pattern_re'],obj_femto.usecase) match_ctsname = re.search(field_ctsname['pattern_re'],obj_femto.ctsname) match_monitoredStatus = re.search(field_monitoredStatus['pattern_re'],obj_femto.monitoredStatus) # match_ipsecIpAddr = re.search(field_ipsecIpAddr['pattern_re'],obj_femto.ipsecIpAddr) match_clusterId = re.search(field_clusterId['pattern_re'],obj_femto.clusterId) match_groupId = re.search(field_groupId['pattern_re'],obj_femto.groupId) match_fgw = re.search(field_fgw['pattern_re'],obj_femto.fgw) match_usid = re.search(field_usid['pattern_re'],obj_femto.usid) match_swVersion = re.search(field_swVersion['pattern_re'],obj_femto.swVersion) match_adminStatus = re.search(field_adminStatus['pattern_re'],obj_femto.adminStatus) match_alarmfield = False if bool_onlyalarmed: if obj_femto.inalarm: match_alarmfield = True else: match_alarmfield = False else: match_alarmfield = True if bool_onlyoffline: if obj_femto.ipsecIpAddr == 'NA': match_ipsecfield = True else: match_ipsecfield = False else: match_ipsecfield = True if (match_groupName and match_usecase and match_ctsname and match_monitoredStatus and match_clusterId and match_groupId and match_fgw and match_usid and match_swVersion and match_adminStatus ): match_nonbools = True else: match_nonbools = False if match_alarmfield and match_nonbools and match_ipsecfield: return True else: return False if not exitflag: eeker.dblock.acquire() t_resultlist = [] for ap in eeker.db_femtos: if matches(ap): t_resultlist.append(ap) coloredList(t_resultlist, 'metro', 'NA', eeker.db_femtos, True) try: try: wantrunonwholegroup = eeker.macrolist.pop() except: wantrunwholegroup_string = ("Do you want to " "run status check on this entire result list?? (y/n) ") wantrunonwholegroup = raw_input(wantrunwholegroup_string) if wantrunonwholegroup == 'y': # now call the pull_console_dump function on the group # send the deepcopied list to pull_ap_console_dump() pmLoaders.pull_ap_console_dump(t_resultlist) except Exception as e: logging.critical(funcname + '\t' + "Got an exception trying to do bulk operation: " + str(e)) eeker.dblock.release()
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 = []