def __getattr__(self, item): log.info(f"-> Find Item: {item}") pic_path, offset, position = self.pic_settings.get(item, (None, ())) if not pic_path: raise KeyError(f"pic settings doesn't have Item: {item}") icon_path = get_current_dir(pic_path) if not os.path.exists(icon_path): raise PathNotFoundError( f"Pic Path: '{icon_path}' Not Exists, Current Path: {icon_path}" ) if not self.loc: element_shape = picture_operator.wait_element(icon_path, offset=(0, 0)) else: element_shape = self.__capture_part_screen(icon_path=icon_path, position=position) if not element_shape: raise PicNotFoundError( f"Item: '{item}' Icon not found, Current Path: {icon_path}") loc = self.__calculate_midpoint(loc=element_shape[0], size=element_shape[1], offset=offset) flow_obj = self.__class__(loc=loc, name=item, pic_path=pic_path) log.info(flow_obj) return flow_obj
def ping_server(ip="15.83.240.98"): log.info("start ping " + ip) s = subprocess.Popen("ping {} -c 50 | grep -i received".format(ip), stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) out, error = s.communicate(timeout=60) out = out.decode() out_list = out.split(',') try: s.kill() res = re.search(r"(\d+)", out_list[2]) result = res.group(1) if int(result) <= 25: log.info("ping " + ip + " success") return True # if out_list[2] == ' 0% packet loss': # log.info("ping " + ip + " success") # return True else: log.error("ping " + ip + " fail") log.error(f'ping {ip} result: {out}') return False except Exception as e: log.error(e) log.error("ping " + ip + " fail") return False
def deleteDetails(subject,body,db): try: city="" nodeList="" match=re.match(r'.*City:(.*)',str(subject),re.M|re.I) if match: city=match.group(1) #nodeList=str(match.group(2)).split(",") else: return 0 bodyMsg=body.split("\n") for line in bodyMsg: if "node" in line.lower(): match=re.match(r'.*Nodes\s*Down\s*:(.*)<o:p>.*',str(line),re.DOTALL) if match: nodes=match.group(1) nodes=nodes.split(",") delete(city,nodes,db) return 1 else: log.warning("Parsing bosy message doesn't match pattern. Body message %s",str(body)) return 0 except Exception as e: log.info("Exception occured while deleting records .Error info %s",str(e)) return -1
def deleteDetails(subject, body, db): try: city = "" nodeList = "" match = re.match(r'.*City:(.*)', str(subject), re.M | re.I) if match: city = match.group(1) #nodeList=str(match.group(2)).split(",") else: return 0 bodyMsg = body.split("\n") for line in bodyMsg: if "node" in line.lower(): match = re.match(r'.*Nodes\s*Down\s*:(.*)<o:p>.*', str(line), re.DOTALL) if match: nodes = match.group(1) nodes = nodes.split(",") delete(city, nodes, db) return 1 else: log.warning( "Parsing bosy message doesn't match pattern. Body message %s", str(body)) return 0 except Exception as e: log.info("Exception occured while deleting records .Error info %s", str(e)) return -1
def website_test(self, url, flag, case_name, report_file): if flag: expect = 200 msg = "verify {} can be accessed".format(url) else: expect = 'Error' msg = "verify {} can not be accessed".format(url) steps = { 'step_name': "test {}".format(url), 'result': '', 'expect': 'status code should be {}'.format(expect), 'actual': '', 'note': '' } log.info(msg) log.info('current ip: {}'.format(common_function.get_ip())) log.info('get status code from {}'.format(url)) data = self.get_website_response() actual = 'status code is {}'.format(data) log.info(actual) if data == expect: s = 'Pass' rs = True else: s = 'Fail' rs = False log.info('test {}'.format(s)) steps['result'] = s steps['actual'] = actual common_function.update_cases_result(report_file, case_name, steps) return rs
def send_mail(recipient, subject='Automation Report Linux', text='', attachment=''): mailUser = "******" msg = MIMEMultipart('related') msg['From'] = mailUser msg['To'] = ','.join(recipient) msg['Subject'] = subject # "AddonCatalog check result" msg.attach(MIMEText(text, 'html', 'utf-8')) msg.attach(getAttachment(attachment)) try: mailServer = smtplib.SMTP(host='15.73.212.81', port=25, local_hostname=socket.gethostname()) mailServer.ehlo() mailServer.starttls() mailServer.ehlo() mailServer.sendmail(mailUser, recipient, msg.as_string()) mailServer.close() log.info("Sent email to %s success" % recipient) except: import traceback log.info(traceback.format_exc())
def access_website_before(proxy_test, case_name, report_file): msg = 'step - - access website before setting proxy' log.info(msg) steps = { 'step_name': '', 'result': '', 'expect': '', 'actual': '', 'note': '' } url = proxy_test.host_name if not proxy_test.access_website(): step_rs = 'Pass' fun_rs = True msg2 = 'failed to access {}'.format(url) else: step_rs = 'Fail' fun_rs = False msg2 = 'succeed to access {}'.format(url) msg3 = '{} can not be accessed'.format(url) steps['step_name'] = msg steps['actual'] = msg2 steps['expect'] = msg3 steps['result'] = step_rs log.info('test {}'.format(step_rs)) common_function.update_cases_result(report_file, case_name, steps) # if fun_rs: # proxy_test.reboot() # else: # proxy_test.reset_env_halfway() return fun_rs
def import_cert(self, file_path): dst = f"/etc/ThinProCertificates/{os.path.basename(file_path)}" shutil.copy(file_path, dst) file_name = os.path.basename(file_path).replace(".cer", ".pem") source = f"/etc/ssl/certs/{file_name}" dist = f"/etc/ThinProCertificates/{file_name}" if file_path in self._certs: log.warning(f'Cert: {file_path} has imported !') return log.info(f"Start Import Cert {file_path}") CertificateUtil._certs.append(file_path) pipe = os.popen(f'hptc-cert-mgr --non-interactive -i {file_path}') if '802' not in file_path: pipe.read() shutil.copy(source, dist) return try: time.sleep(3) input_window = self.cert_password input_window.cert_blank.send_keys(self._cert_password) self.cert_ok.click() time.sleep(10) log.info(f"Import Cert {file_path} Success!") except PicNotFoundError as e: raise CaseRunningError(f"Import Cert Fail! {str(e)}") finally: self.close()
def clear_proxy(proxy_test, case_name, report_file): protocol_name = proxy_test.protocol msg = 'step - - clear {} proxy'.format(protocol_name) log.info(msg) steps = { 'step_name': '', 'result': '', 'expect': '', 'actual': '', 'note': '' } if proxy_test.set_proxy(''): step_rs = 'Pass' fun_rs = True msg2 = 'succeed to clear proxy' else: step_rs = 'Fail' fun_rs = False msg2 = 'Failed to clear proxy' msg3 = 'network panel can be edit' steps['step_name'] = msg steps['actual'] = msg2 steps['expect'] = msg3 steps['result'] = step_rs log.info('test {}'.format(step_rs)) common_function.update_cases_result(report_file, case_name, steps) return fun_rs
def double_click(self): if not self.loc: raise ClickError( f"Can't click, Operation object not declared, Current object: {self.name}, Current Path:{self.pic_path}" ) log.info(f"Click Loc: X: {self.loc[0]}, Y: {self.loc[1]}") pyautogui.click(*self.loc, clicks=2, interval=0.05)
def import_cert(cert="ROOTCA.pem"): time.sleep(1) log.info("start import certificate") rootca_pem_1 = os.path.exists("/etc/ssl/certs/ROOTCA.pem") if rootca_pem_1: log.info("certificate is already exist") return True else: log.info("certificate not exist, start install cert") shutil.copy( os.path.join(get_current_dir(), 'Test_Utility', 'ROOTCA.pem'), '/usr/local/share/ca-certificates/ROOTCA.pem') time.sleep(0.2) c = os.path.exists("/usr/local/share/ca-certificates/{}".format(cert)) if not c: log.error('copy cert fail') return False log.info('copy cert success') subprocess.getstatusoutput("/usr/bin/hptc-cert-mgr --apply") time.sleep(4) rootca_pem_2 = os.path.exists("/etc/ssl/certs/ROOTCA.pem") if not rootca_pem_2: log.error('install certificates fail') return False else: log.info('install certificates success') time.sleep(1) return True
def check_instance(self): if self.instance: log.info("instance exist") return True else: log.info("instance is None") return False
def save_info_to_yaml(cls) -> dict: if not cls.network_information: cls.get_network_info() log.info(f"StaticIpUtils Save: {cls.network_information}") file_oper = YamlOperator(cls.temp_net_info_file) file_oper.write(cls.network_information) return cls.network_information
def set_proxy_common(proxy_test, proxy_server, case_name, report_file): protocol_name = proxy_test.protocol msg = 'step - - set {} proxy to {}'.format(protocol_name, proxy_server) log.info(msg) steps = { 'step_name': '', 'result': '', 'expect': '', 'actual': '', 'note': '' } if proxy_test.set_proxy(proxy_server): step_rs = 'Pass' fun_rs = True msg2 = 'succeed to clear http proxy' else: step_rs = 'Fail' fun_rs = False msg2 = 'Failed to clear http proxy' msg3 = 'network panel can be edit' steps['step_name'] = msg steps['actual'] = msg2 steps['expect'] = msg3 steps['result'] = step_rs log.info('test {}'.format(step_rs)) common_function.update_cases_result(report_file, case_name, steps) # if fun_rs: # proxy_test.reboot() # else: # proxy_test.reset_env_halfway() return fun_rs
def __get_src_files(self): static_path = os.path.join(os.getcwd(), self.__static_src) if os.path.exists(get_current_dir('Test_Report', 'static')): shutil.rmtree(get_current_dir('Test_Report', 'static')) log.info('Target static folder exist, remove the old folder') shutil.copytree(static_path, get_current_dir('Test_Report', 'static')) log.info('Copy static folder to report folder finished') return True
def check_status(self): if self.check_instance(): if self.instance.poll() is None: log.info("instance is running") return True else: log.info("instance have end") return False
def reset_env_last(self): log.info('restore env default after the last step') # common_function.import_profile() self.dns.open_dns() self.dns.clear_text(self.protocol) self.dns.close_dns() subprocess.run('reboot') time.sleep(10)
def click(self): time.sleep(1) if not self.loc: raise ClickError( f"Can't click, Operation object not declared, Current object: {self.name}, Current Path:{self.pic_path}" ) log.info(f"Click Loc: X: {self.loc[0]}, Y: {self.loc[1]}") pyautogui.click(*self.loc)
def get_platform(): if __OS == 'Linux': platform = subprocess.getoutput('/usr/bin/hptc-hwsw-id --hw') if platform == '': log.info('Platform is empty.') return platform else: return False
def local_start(case_name, result_file, test_data, local_pic_name): """ make sure mouse position will not affect testing """ pic_min_sim = 0.99 x, y = mouse.position() if x >= y > 200: pyautogui.moveTo(200, 200) elif 200 > x >= y: pyautogui.moveTo(x + 20, y + 20) else: pyautogui.moveTo(250, 250) time.sleep(20) # check local resolution pic_sim, diff_res, first_collect_flag = check_layout(os.path.join(test_data, 'td_multiple_display', 'local_pic', local_pic_name)) if "1_ms" in local_pic_name: log.info("Change Sim to 0.93") pic_min_sim = 0.93 if pic_sim >= pic_min_sim: steps = { 'step_name': 'check local layout', 'result': 'Pass', 'expect': 'similarity > 99%', # can be string or pic path 'actual': 'layout and resolution set correctly', 'note': ''} common_function.update_cases_result(result_file, case_name, steps) return True, first_collect_flag else: if not os.path.exists(common_function.get_current_dir('Test_Report', 'img')): os.mkdir(common_function.get_current_dir('Test_Report', 'img')) try: common_function.check_free_memory() picture_operator.save_from_data('{}'.format(common_function.get_current_dir('Test_Report', 'img', '{}.png'.format(case_name))), diff_res) save_path = common_function.get_current_dir('Test_Report', 'img', '{}.jpg'.format(case_name)) shutil.copy(common_function.get_current_dir('temp.png'), save_path) common_function.check_free_memory("after") except AssertionError as e: raise e except: save_fail_path = common_function.get_current_dir('Test_Report', 'img', 'save_fail_{}.txt'.format(case_name)) f = open(save_fail_path, "w") f.close() finally: log.debug(gc.garbage) gc.collect() steps = { 'step_name': 'check local layout', 'result': 'Fail', 'expect': 'similarity > 99%', # can be string or pic path 'actual': 'img/{}.png'.format(case_name), # can be string or pic path 'note': 'actual similarity: {}'.format(pic_sim)} common_function.update_cases_result(result_file, case_name, steps) return False, first_collect_flag
def __wait_until_ap_appear(self, time_out=60, interval=5): while time_out: try: return self.network.signal except PicNotFoundError: time_out -= interval log.info("Waiting AP Appear") time.sleep(interval) raise CaseRunningError(f"Click Scan AP, But Waiting AP Appear Fail")
def enable_lan_filber(device_name): try: subprocess.Popen( "/usr/lib/hptc-network-mgr/common/netmgr_wired --up {}".format( device_name), shell=True) log.info("enable {}".format(device_name)) except Exception as e: log.error(e)
def enable_wlan(device_name): try: subprocess.run( "/usr/lib/hptc-network-mgr/wireless/hptc-wireless-manager --up {}". format(device_name), shell=True) log.info("enable {}".format(device_name)) except Exception as e: log.error(e)
def exec_callback(self, around_list: list): while around_list: around_name = around_list[0] around_list.remove(around_name) around_method = self.__dic.get(around_name) assert around_method, "{} Not Exist".format(around_name) log.info("Start Callback: {}".format(around_name)) around_method(self) self.__work_around_list_success = [] self.__work_around_list_fail = []
def __wait_until_rdp_logon(self, time_out=60, interval=5): while time_out: try: if self.rdp.into_rdp: return True except PicNotFoundError: time_out -= interval log.info("Waiting RDP Logon") time.sleep(interval) raise CaseRunningError(f"VDI Logon Fail VDI_ID {self.rdp.vdi_id}")
def __ping_method(self, target_ip="15.83.240.98", expectation=True): ping_result = ping(target_ip, count=15) if ping_result is expectation: log.info( f"Ping {target_ip} Success! Expect: {expectation} Actual: {ping_result}" ) else: raise CaseRunningError( f"Ping {target_ip} Fail! Expect: {expectation} Actual: {ping_result}" )
def wait_until_wireless_connected(self, time_out=60, interval=5): while time_out: try: connected = self.wireless_connected return except PicNotFoundError: time_out -= interval log.info("Waiting Wireless Connect") time.sleep(interval) raise CaseRunningError(f"Wireless Connect Fail")
def access_website(self): log.info('current ip: {}'.format(common_function.get_ip())) expect = 200 data = self.get_website_response() log.info('status code is: {}'.format(data)) if data == expect: rs = True else: rs = False return rs
def usb_is_ready(self, time_out=4, interval=2): while time_out: try: if self.usb_ready: return True except PicNotFoundError: time_out -= interval log.info("Waiting USB Ready") time.sleep(interval) return False
def domains_to_ip_list(domains): ip_list = [] log.info("domains: {}".format(domains)) for domain in domains: try: ip_list.append(socket.getaddrinfo(domain, None)[0][4][0]) except: log.info(" Can't connect {}".format(domain)) traceback.print_exc() # return ip_list return ip_list
def search_file_from_usb(file_name): """ :return str, absolute file path, name can be a grep in linux """ folder = run_command("ls /media").strip() result = run_command( f"find /media/{folder} -type f -iname '{file_name}'").strip() if not result: raise CaseRunningError( f"File {file_name} not found in /media/{folder}") log.info(f'Find the File: {result}') return result
def nodePingtest(ipaddress): try: status,result=cm.getstatusoutput("ping -c100 "+str(ipaddress)) log.info("Node ping result : %s",str(result)) if status == 0: log.info("Ping to device got passed") return "0" else: return str(result) except Exception as e: log.warning("Ping test for Node Reboot got exception . Error msg : %s",str(e)) return "1"
def sendMail(subject,message,to_addr_list): try: smtpserver= "relay.emc-corp.net:25" #to_addr_list = common.getEmailList(sev.lower()) from_addr_list = "Network-engineering" log.info("Send mail content. Subject %s .message %s",str(subject),str(message)) msg = MIMEText(message) msg['Subject'] = subject msg['From'] = from_addr_list msg['To'] = ",".join(to_addr_list) server = smtplib.SMTP(smtpserver) server.ehlo() send = server.sendmail(from_addr_list, to_addr_list, msg.as_string()) server.quit() except Exception as e: log.warning("Exception occured during send mail . Error Msg : %s",str(e))
def sendMail(subject,message): try: smtpserver= "relay.emc-corp.net:25" #to_addr_list = ["*****@*****.**"] to_addr_list = common.getEmailList('exception') from_addr_list = "Network-engineering" log.info("Node -- Send mail content. Subject %s .message %s",str(subject),str(message)) msg = MIMEText(message) msg['Subject'] = subject msg['From'] = from_addr_list msg['To'] = ",".join(to_addr_list) server = smtplib.SMTP(smtpserver) server.ehlo() send = server.sendmail(from_addr_list, to_addr_list, msg.as_string()) log.info("Node-- Mail has been sent to %s",str(to_addr_list)) server.quit() except Exception as e: log.warning("Node -- Send mail got error %s",str(e))
def delete(city,nodes,dbs): try: print "city",city record=dbs.selectNodeState(city.strip()) dbNodeValue="" print "yet to delete nodes : -",nodes for rec in record: dbNodeValue=str(rec[2]).split(",") dbIpaddress=str(rec[4]).split(",") print "DBNode",dbNodeValue for node in nodes: if str(node).strip() in str(rec[2]).strip(): index=dbNodeValue.index(str(node).strip()) del dbIpaddress[index] dbNodeValue.remove(str(node).strip()) print "DBNode:",dbNodeValue if dbNodeValue: value=",".join(dbNodeValue) Count=len(dbNodeValue) ipadd=",".join(dbIpaddress) values=[] values.append(str(rec[0])) values.append(Count) values.append(str(value)) values.append('NO') values.append(str(ipadd)) dbs.updateNodeStateTable(values) log.info('Node record got updated.values are %s',str(values)) else: dbs.deleteRowNode(str(rec[0])) log.info('Node %s info got deleted.',str(rec[0])) except Exception as e: log.warning("Exception occured while deleting records.Error info %s",str(e))
def pingTest(device1,device2,aIntf,bIntf,latencyValue,latency): try: username,password=common.getRouterCredentials() #password="******" delayFactor,loop = common.getSendCommandDelayFactor() timeout=getTimeout() slaState="0" AEnddevice = ConnectHandler(device_type="cisco_ios", ip=device1, username=username, password=password) command='sh run interface '+aIntf AEnddevice.clear_buffer() output1 = AEnddevice.send_command(command,int(delayFactor),int(loop)) output1 = str(output1) log.info("Device %s. Command %s. Output %s",str(device1),str(command),str(output1)) match=re.match(r'.*ip\s+address\s+(.*)\s+255.*',output1,re.DOTALL) if match: AEndIntf=match.group(1) BEnddevice = ConnectHandler(device_type="cisco_ios", ip=device2, username=username, password=password) command='sh run interface '+bIntf BEnddevice.clear_buffer() output1 = BEnddevice.send_command(command,int(delayFactor),int(loop)) output1 = str(output1) log.info("Device %s. Command %s. Output %s",str(device2),str(command),str(output1)) match=re.match(r'.*ip\s+address\s+(.*)\s+255.*',output1,re.DOTALL) if match: BEndIntf=match.group(1) command='ping '+BEndIntf.strip()+' source '+AEndIntf.strip()+' repeat 30 timeout '+timeout AEnddevice.clear_buffer() output1 = AEnddevice.send_command(command,int(delayFactor),int(loop)) output1 = str(output1) pingA = output1 log.info("Device %s.Ping Command %s. Output %s",str(device1),str(command),str(output1)) Alatency=1 pingResultA=-1 match=re.match(r'.*rate\s+is\s+(.*)\s+percent.*',output1,re.DOTALL) if match: pingResultA=match.group(1) try: if "latency" in latency.lower(): lValue=latencyValue.split(",") match1=re.match(r'.*min/avg/max\s*=\s*(\d+)/(\d+)/(\d+).*',output1,re.DOTALL) if match1: AminValue=match1.group(1) AavgValue=match1.group(2) AmaxValue=match1.group(3) if int(lValue[0]) > int(AavgValue) or int(lValue[2]) < int(AavgValue): if int(lValue[0]) > int(AavgValue) and int(lValue[2]) < int(AavgValue): slaState="Latency-Above & Below threshold" elif int(lValue[0]) > int(AavgValue): slaState="Latency-Below threshold" else: slaState="Latency-Above threshold" Alatency=0 except Exception as e: log.warning("parsing Latency Message got failed.Error info : %s",str(e)) else: if "Invalid" in output1: pingResultA="0" elif pingResultA == -1: pingResultA = output1 # pingResultA="0" command='ping '+AEndIntf.strip()+' source '+BEndIntf.strip()+' time '+timeout+' repeat 30' BEnddevice.clear_buffer() output1 = BEnddevice.send_command(command,int(delayFactor),int(loop)) output1 = str(output1) pingB = output1 print "PingB",pingB log.info("Device %s. Ping Command %s. Output %s",str(device2),str(command),str(output1)) Blatency=1 pingResultB=-1 match=re.match(r'.*rate\s+is\s+(.*)\s+percent.*',output1,re.DOTALL) if match: pingResultB=match.group(1) try: if "latency" in latency.lower(): lValue=latencyValue.split(",") match1=re.match(r'.*min/avg/max\s*=\s*(\d+)/(\d+)/(\d+).*',output1,re.DOTALL) if match1: BminValue=match1.group(1) BavgValue=match1.group(2) BmaxValue=match1.group(3) if int(lValue[0]) > int(BavgValue) or int(lValue[2]) < int(BavgValue): if int(lValue[0]) > int(BavgValue) and lValue[2] < int(BavgValue) : slaState="Latency-Above & Below threshold" elif int(lValue[0]) > int(BavgValue): slaState="Latency-Below threshold" else: slaState="Latency-Above threshold" Blatency=0 except Exception as e: log.warning("parsing Latency Message got failed.Error info : %s",str(e)) else: if "Invalid" in output1: pingResultB="0" elif pingResultB == -1: pingResultB = output1 try: log.info("Ping result success rate for A ENd Device : %s , For Z End Device : %s ",str(pingResultA),str(pingResultB)) if int(pingResultA.strip()) > 99 and int(pingResultB.strip()) > 99: if Blatency == 0 or Alatency == 0: return 0,AEndIntf,BEndIntf,pingA,pingB,slaState else: return 1,AEndIntf,BEndIntf,pingA,pingB,slaState else: return 0,AEndIntf,BEndIntf,pingA,pingB,slaState except Exception as e: return 0,AEndIntf,BEndIntf,pingA,pingB,slaState except Exception as e: if "Authentication" in str(e): subject="Authentication failed on routers"+device1+","+device2 message="Error while login to Routers.Authentication failed" sendMail(subject,message,'exception') return 0,0,0,"Fail",0,"" elif "timed-out" in str(e): log.warning("Device timed out error %s",str(e)) return 0,0,0,"0",0,"" else: log.warning("Error occured while doing pingtest %s",str(e))
def read(username, password,db): # Login to INBOX imap = imaplib.IMAP4_SSL("outlook.office365.com", 993) imap.login(username, password) imap.select('EMC-Core-Mon') #imap.select('INBOX') for line in open('/opt/CoreMonitoringApp/Common/secretKey','r').readlines(): # Print all unread messages from a certain sender of interest passwordStr=str(line).strip() #print "MailID",line.strip() hash_object=hashlib.md5(passwordStr.encode()) hashPass=hash_object.hexdigest() status, response = imap.search(None, '(UNSEEN)') unread_msg_nums = response[0].split() for e_id in unread_msg_nums: #print "full msg : ",e_id _, response = imap.fetch(e_id, '(UID BODY[TEXT])') subject=0 body=0 flag=0 fromMailID="" typemsg,data = imap.fetch(e_id,'(RFC822.SIZE BODY[HEADER.FIELDS (SUBJECT)])') subject = data[0][1].lstrip('Subject: ').strip() + ' ' body = response[0][1] typemssg,data= imap.fetch(e_id,'(RFC822.SIZE BODY[HEADER.FIELDS (FROM)])') fromMailID = data[0][1].lstrip('From: ').strip() print "From MailID :-",fromMailID mailID="" match=re.match(r'\s*\"(.*)\"\s*\<.*',str(fromMailID),re.M|re.I) nodeStringPresent=0 cidStringPresent=0 if match: mailID=str(match.group(1).strip()) else: mailID=fromMailID #print body print subject print "hashvalue",hashPass mdHash=0 mdStringPresent=0 if "md5" in str(body).lower(): bodyMsg=body.split("\n") for line in bodyMsg: #print "line",line if "md5" in line.lower(): mdStringPresent = 1 match=re.match(r'.*MD5hash\s*:(\s*.*<span.*white">|\s*)([a-zA-Z0-9]+)(</span><o:p>.*|</span><span .*>.*|<o:p>)',str(line),re.M|re.I) if match: hashValue=str(match.group(2)).strip() print hashValue print hashPass if hashValue.strip() == hashPass.strip(): mdHash=1 print "matched",mdHash break if mdHash == 0: if mdStringPresent == 0: to_addr_list=str(common.getAdditionalMailID()).split(",") to_addr_list.append(str(fromMailID)) #to_addr_list.append('*****@*****.**') subjectHeading=subject message="Hi "+mailID+",\n\n The MD5 Hash key is missing from the email sent,Please add the correct MD5 Hash when sending the Acknowledgement Mail.\n\n\n Example: MD5hash: XXXXXXXXXXXXXXXXXXXXXXXX \n\n Thanks\n Core Montiorng App\n [email protected]" sendMail(subjectHeading,message,to_addr_list) else: print mdHash to_addr_list=str(common.getAdditionalMailID()).split(",") to_addr_list.append(str(fromMailID)) #to_addr_list.append('*****@*****.**') subjectHeading=subject message="Hi "+mailID+",\n\n The MD5 HASH sent for the acknowledge of Circut ID doesn't match.Please enter the correct MD5 hash.Please do not reply to this email.\n\n Thanks\n Core Montirong App\n [email protected]" sendMail(subjectHeading,message,to_addr_list) log.warning('Unable to match MDSHash value.Please use proper hash value in the mail.') continue #print circuitIDs[1] if "acknowledge" in str(body).lower(): flag = 1 log.info("Acknowledge string present in the Body message: ") if flag == 1: if "node" in str(subject).lower(): nodeStringPresent=1 success=deleteDetails(subject,body,db) if success == 0: to_addr_list=str(common.getAdditionalMailID()).split(',') to_addr_list.append(str(fromMailID)) subjectHeading="Nodes Down,Device-Type: XXXXXX, City: XXXXXXXX" message="Hi "+str(mailID)+", \n\n The EMail Acknowledge format for Node Down is like below. \n\n Nodes Down:XXXX,XXXXX\n Status: Acknowledge\n MD5hash: xxxxxxxxxxxxxxxxxx.Please do not reply to this mail. \n\n Thanks\n Core Motinoring App\n [email protected]" sendMail(subjectHeading,message,to_addr_list) else: #bodyMsg=body.split("\n") #for line in bodyMsg: match=re.match(r'(.*Circuit\s*ID\s*|[a-zA-Z]*\s*:\s*CID|\s*CID\s*):(.*)',str(subject),re.M|re.I) #print "line",line if match: circuitIDs=str(match.group(2)).split(',') CIDs=circuitIDs #print CIDs #log.info("Going to delete CircuitID record from CoreCircuitState table which has been resolved .") for cid in CIDs: check=db.checkInTable(str(cid).strip()) if int(check) == 1: db.deleteRow(cid.strip()) log.info("Record Deleted Successfully. Internal Circuit Id : %s ",str(cid)) else: #sendMail() to_addr_list=str(common.getAdditionalMailID()).split(",") to_addr_list.append(fromMailID) #to_addr_list.append('*****@*****.**') subject="Circuit ID:"+str(cid) message=" Hi Team, \n\n The Circuit ID sent "+str(cid)+" for acknowledgement does not exists in the Circuit State Table.Please enter the correct CircuitID or check if the incident has already been approved.\n\n\n Thanks,\n Core Monitoring App\n [email protected] " sendMail(subject,message,to_addr_list) else: to_addr_list=str(common.getAdditionalMailID()).split(',') to_addr_list.append(str(fromMailID)) subjectHeading=subject message="Hi "+str(mailID)+"\n\n The subject mail should like either \"Circuit ID: XXXXXXXXXXXXXXXXXX\" or \"CID: XXXXXXXXXXXXXXX\". Please check the Email Acknowledge mail format and send.\n\n Thanks\n Core Monitoring App\n [email protected]" sendMail(subjectHeading,message,to_addr_list) # print "Internal Circuit ID "+cid.strip()+" Record Deleted Succeffully from mCoreCircuitStates Table" #db.deleteRow(value) # Mark them as seen for e_id in unread_msg_nums: imap.store(e_id, '+FLAGS', '\Seen')
def checkDataBase(dbs): try: record=dbs.selectTable('nodeState') for rec in record: flag=str(rec[3]) if flag.strip() == 'YES': Count=len(str(rec[2]).split(',')) if int(Count) == 1: result=nodePingtest(str(rec[4]).strip()) if result == "0": log.info("Skipping mail operations and delete info from nodeState table.") self.db.deleteRowNode(str(rec[0])) print 'Node info got deleted.',str(rec[0]) continue elif result == "1": msg="" subject="Sev2 Node Down, Device-Type :"+str(rec[5])+", City:"+str(rec[0]) msg+=" Device Name :"+str(rec[2])+" IP-Address :"+str(rec[4])+" Device-Type : Core City : "+str(rec[0])+" State : Packet Loss\n Ping Result \n Got exception while doing ping test, please refer logs for more info " message="Team \n\n\n Please investigate this node down incident \n\n "+msg else: msg="" subject="Sev2 Node Down, Device-Type :"+str(rec[5])+", City:"+str(rec[0]) msg+=" Device Name :"+str(rec[2])+" IP-Address :"+str(rec[4])+" Device-Type : Core City : "+str(rec[0])+" State : Packet Loss\n Ping Result \n "+str(result) message="Team \n\n\n Please investigate this node down incident \n\n "+msg else: # subject="Sev1 Nodes Down, Device-Type :"+str(rec[5])+" ,City:"+str(rec[0]) nodeNames=str(rec[2]).split(',') ipadd=str(rec[4]).split(',') msg="" removeNodes=[] counting=0 for i in range(0,len(nodeNames)): result=nodePingtest(str(ipadd[i]).strip()) if result == "0": removeNodes.append(str(nodeNames[i])) elif reult == "1": counting = counting + 1 msg+=" Device Name :"+str(nodeNames[i])+" IP-Address :"+str(ipadd[i])+" Device-Type : Core City : "+str(rec[0])+" State : Packet Loss\n Ping Result \n Got exception while doing ping test, please refer logs for more info " else: counting=counting+1 msg+=" Device Name :"+str(nodeNames[i])+" IP-Address :"+str(ipadd[i])+" Device-Type : Core City : "+str(rec[0])+" State : Packet Loss \n Ping Result \n "+str(result) if counting > 1: subject="Sev1 Nodes Down, Device-Type :"+str(rec[5])+" ,City:"+str(rec[0]) elif counting == 1: subject="Sev2 Nodes Down, Device-Type :"+str(rec[5])+" ,City:"+str(rec[0]) else: log.info("Skipping mail operations and delete info from nodeState table.") self.db.deleteRowNode(str(rec[0])) print 'Node info got deleted.',str(rec[0]) continue message="Team \n\n\n Please find details about down nodes \n\n "+msg if removeNodes: dbNodeValue=str(rec[2]).split(",") dbIpaddress=str(rec[4]).split(",") for node in removeNodes: index=dbNodeValue.index(str(node)) del dbIpaddress[index] dbNodeValue.remove(str(node)) value=",".join(dbNodeValue) Count1=len(dbNodeValue) ipadd=",".join(dbIpaddress) values=[] values.append(str(rec[0])) values.append(str(Count1)) values.append(str(value)) values.append('NO') values.append(str(ipadd)) dbs.updateNodeStateTable(values) print'Node record got updated.values are',str(values) sendMail(subject,message) continue sendMail(subject,message) value=[] value.append(str(rec[0])) value.append('NO') dbs.updateNodeStateTableAfterMailSend(value) except Exception as e: log.warning("Node Down-- Exception occured(CheckDatabse Module). Error info : %s",str(e))
def checkAfterTimeout(db): while 1: try: record=db.selectTable('CoreCircuitStates') seconds=getDateTime() for rec1 in record: if str(rec1[3]) == 'YES': #log.info('GOC Flag set as YES.so move to next record') continue diff=Decimal(str(seconds)) - Decimal(str(rec1[4])) if int(diff) >= 60: setFlag=[] SqlQuery="" row=db.selectCoreRouterDetails(rec1[0]) for rec in row: log.info('Going to perform ping from both ends') result,AEndIntf,BEndIntf,pingA,pingB,slaState=pingTest(str(rec[10]),str(rec[11]),str(rec[5]),str(rec[7]),str(rec[12]),str(rec1[1])) if result == 0: #setFlag.append(rec1[0]) #setFlag.append('YES') #setFlag.append('0') if slaState.strip() == "0": SqlQuery="update CoreCircuitStates set GOCFlag = LTRIM(\'YES\'), Time = LTRIM(\'0\') where InternalCircuitID = LTRIM(\'"+str(rec1[0])+"\')" else: SqlQuery="update CoreCircuitStates set GOCFlag = LTRIM(\'YES\'), Time = LTRIM(\'0\') , SLAState = LTRIM(\'"+str(slaState)+"\') where InternalCircuitID = LTRIM(\'"+str(rec1[0])+"\')" phNumber="" if "NO" in str(rec[13]).strip(): phNumber="" else: phNumber=str(rec[13]).strip() setGOCFlag(db,SqlQuery,str(rec[4]),str(rec[5]),str(rec[6]),str(rec[7]),str(rec[8]),str(rec1[2]),str(rec1[0]),AEndIntf,BEndIntf,pingA,pingB,str(rec[1]),str(rec[2]),slaState,phNumber) else: log.info("Ping Result got passed.so going to delete InternalCircuitID state details from CoreCircuitState table") try: db.deleteRow(str(rec1[0]).strip()) except Exception as e: log.warning("Error occured while deleting record in CoreCircuitStates table.Error info : %s",str(e)) if "down" in rec1[2]: setFlag.append("UP") db.updateSLAState(setFlag) else: db.updateTime(setFlag) except Exception as e: #log.warning("Error Occured") pass