思想
1、python抓取交換機(jī)的信息
2、提取需要的檢查結(jié)果,并寫(xiě)入excel表格
3、如果某一檢查結(jié)果超出閾值或不符合某一關(guān)鍵字,自動(dòng)發(fā)送報(bào)警郵件
4、每十分鐘執(zhí)行一次python代碼
代碼
抓取交換機(jī)信息
importpexpect importsys importdatetime importos #today=datetime.date.today().strftime('%Y%m%d') print(3) ip='x.x.x.x' passwd='xxxx' txt='F5hexin.txt' name='' name1="----More----" child=pexpect.spawn('telnet%s'%ip) fout=open('/root/F5Core/Core/'+txt,'wb+') child.logfile=fout child.expect('login:') child.sendline("admin") child.expect('(?i)ssword:') child.sendline("%s"%passwd) child.expect("%s"%name) child.sendline("displaypower") child.expect("%s"%name) child.sendline("displayversion") foriinrange(20): index=child.expect([name1,"%s"%name]) if(index==0): child.send("") else: child.sendline("displayenvironment") break foriinrange(20): index=child.expect([name1,"%s"%name]) if(index==0): child.send("") else: child.sendline("displayfan") break foriinrange(10): index=child.expect([name1,"%s"%name]) if(index==0): child.send("") else: child.sendline("displaycpu-usage") break foriinrange(10): index=child.expect([name1,"%s"%name]) if(index==0): child.send("") else: child.sendline("displaymemory") break foriinrange(10): index=child.expect([name1,"%s"%name]) if(index==0): child.send("") else: child.sendline("displayinterfacebrief") break foriinrange(10): index=child.expect([name1,"%s"%name]) if(index==0): child.send("") else: child.sendline("dislogbuffer") break foriinrange(50): index=child.expect([name1,"%s"%name]) if(index==0): child.send("") else: child.sendline("displayiprouting-table") break foriinrange(50): index=child.expect([name1,"%s"%name]) if(index==0): child.send("") else: child.sendline("displayirflink") break foriinrange(3): index=child.expect([name1,"%s"%name]) if(index==0): child.send("") else: child.sendline("quit") sys.exit()
代碼
匹配閾值,報(bào)警
#coding:utf-8 importpymssql importxlwt importdatetime fromxlwtimport* fromemail.mime.textimportMIMEText fromemail.mime.multipartimportMIMEMultipart importsmtplib importdatetime fromemailimportencoders fromemail.mime.imageimportMIMEImage fromemail.mime.baseimportMIMEBase importre importxlrd file=open('/root/F5Core/Core/F5hexin.txt','r+') listlist=file.readlines() i=1 forlineinlistlist:#匹配檢查結(jié)果,超出閾值,把結(jié)果換成error。 if'displaypower'inline: power11=listlist[i+6][8:15].strip() ifpower11!='Normal': power11='Error' print(power11) power12=listlist[i+8][8:15].strip() ifpower12!='Normal': power12='Error' print(power11) power13=listlist[i+10][8:15].strip() ifpower13!='Normal': power13='Error' print(power11) power14=listlist[i+12][8:15].strip() ifpower14!='Normal': power14='Error' print(power11) power21=listlist[i+28][8:15].strip() ifpower21!='Normal': power21='Error' print(power11) power22=listlist[i+30][8:15].strip() ifpower22!='Normal': power22='Error' print(power11) power23=listlist[i+32][8:15].strip() ifpower23!='Normal': power23='Error' print(power23) power24=listlist[i+34][8:15].strip() ifpower24!='Normal': power24='Error' print(power24) if'MPU(M)Chassis1Slot0:'inline: time11=listlist[i+1][9:].strip() print(time11) if'LPUChassis1Slot2:'inline: time12=listlist[i+1][9:].strip() print(time12) if'MPU(S)Chassis2Slot0:'inline: time13=listlist[i+1][9:].strip() print(time13) if'LPUChassis2Slot2:'inline: time14=listlist[i+1][9:].strip() print(time14) if' displayenvironment'inline: environment11=listlist[i+10][26:29].strip()+''+listlist[i+12][26:29].strip() environment12=listlist[i+14][26:29].strip()+''+listlist[i+16][26:29].strip()+''+listlist[i+18][26:29].strip() environment13=listlist[i+46][26:29].strip()+''+listlist[i+51][26:29].strip() environment14=listlist[i+53][26:29].strip()+''+listlist[i+55][26:29].strip()+''+listlist[i+57][26:29].strip() ifint(listlist[i+10][26:29].strip())>70: environment11='Error' print(environment11) ifint(listlist[i+12][26:29].strip())>70: environment11='Error' print(environment11) ifint(listlist[i+14][26:29].strip())>70: environment12='Error' print(environment12) ifint(listlist[i+16][26:29].strip())>70: environment12='Error' print(environment12) ifint(listlist[i+18][26:29].strip())>70: environment12='Error' print(environment12) ifint(listlist[i+46][26:29].strip())>70: environment13='Error' print(environment13) ifint(listlist[i+51][26:29].strip())>70: environment13='Error' print(environment13) ifint(listlist[i+53][26:29].strip())>70: environment14='Error' print(environment14) ifint(listlist[i+55][26:29].strip())>70: environment14='Error' print(environment14) ifint(listlist[i+57][26:29].strip())>70: environment14='Error' print(environment14) if' displayfan'inline: fana11=listlist[i+6][-8:].strip() iffana11!='Normal': fana11='Error' print(fana11) fana12=listlist[i+26][-8:].strip() iffana12!='Normal': fana12='Error' print(fana12) fana13=listlist[i+53][-8:].strip() iffana13!='Normal': fana13='Error' print(fana13) fana14=listlist[i+73][-8:].strip() iffana14!='Normal': fana14='Error' print(fana14) if'Chassis1Slot0CPU0CPUusage:'inline: cpu11=listlist[i+5][6:10].strip() ifint(re.sub('%','',cpu11))>30: cpu11='Error' print(cpu11) if'Chassis1Slot2CPU0CPUusage:'inline: cpu12=listlist[i+5][6:10].strip() ifint(re.sub('%','',cpu12))>30: cpu12='Error' print(cpu12) if'Chassis2Slot0CPU0CPUusage:'inline: cpu13=listlist[i+5][6:10].strip() ifint(re.sub('%','',cpu13))>30: cpu13='Error' print(cpu13) if'Chassis2Slot2CPU0CPUusage:'inline: cpu14=listlist[i+8][6:10].strip() ifint(re.sub('%','',cpu14))>30: cpu14='Error' print(cpu14) if' displaymemory'inline: memory11=listlist[i+8][-6:].strip() iffloat(re.sub('%','',memory11))30.0: ????????????memory11='Error' ????????????print(memory11) ????????memory12=listlist[i+20][-6:].strip() ????????if?float(re.sub('%','',memory12))?30.0: ????????????memory12='Error' ????????????print(memory12) ????????memory13=listlist[i+110][-6:].strip() ????????if?float(re.sub('%','',memory13))?30.0: ????????????memory13='Error' ????????????print(memory13) ????????memory14=listlist[i+122][-6:].strip() ????????if?float(re.sub('%','',memory14))30.0: ????????????memory14='Error' ????????????print(memory14) ????if?'RAGG1?'?in?line: ????????briefa11=line[20:30].strip() ????????if?briefa11?!=?'UP???UP': ????????????briefa11='Error' ????????????print(briefa11) ????if?'RAGG2?'?in?line: ????????briefa12=line[20:30].strip() ????????if?briefa12?!=?'UP???UP': ????????????briefa12='Error' ????????????print(briefa12) ????if?'RAGG3?'?in?line: ????????briefa13=line[20:30].strip() ????????if?briefa13?!=?'UP???UP': ????????????briefa13='Error' ????????????print(briefa13) ????if?'RAGG4?'?in?line: ????????briefa14=line[20:30].strip() ????????if?briefa14?!=?'UP???UP': ????????????briefa14='Error' ????????????print(briefa14) ????if?'RAGG125?'?in?line: ????????briefa15=line[20:30].strip() ????????if?briefa15?!=?'UP???UP': ????????????briefa15='Error' ????????????print(briefa15) ????if?'Current?messages:'?in?line: ????????log11=line[-5:].strip() ????????if??int(log11)>530: log11='Error' print(log11) if'Routes:'inline: routingtable11=line[-5:].strip() ifint(routingtable11)240: ????????????routingtable11?=?'Error' ????????????print(routingtable11) ????if?'Ten-GigabitEthernet1/2/0/47(MDC1)'?in?line: ????????IRF11=line[-10:].strip() ????????if?IRF11?!=?'UP': ????????????IRF11='Error' ????????????print(IRF11) ????if?'Ten-GigabitEthernet1/2/0/48(MDC1)'?in?line: ????????IRF12=line[-10:].strip() ????????if?IRF12?!=?'UP': ????????????IRF12='Error' ????????????print(IRF12) ????if?'Ten-GigabitEthernet2/2/0/47(MDC1)'?in?line: ????????IRF13=line[-10:].strip() ????????if?IRF13?!=?'UP': ????????????IRF13='Error' ????????????print(IRF13) ????if?'Ten-GigabitEthernet2/2/0/48(MDC1)'?in?line: ????????IRF14=line[-10:].strip() ????????if?IRF14?!=?'UP': ????????????IRF14='Error' ????????????print(IRF14) ????i?+=?1 workbook?=?xlwt.Workbook() style?=?XFStyle() pattern?=?Pattern() pattern.pattern?=?Pattern.SOLID_PATTERN pattern.pattern_fore_colour?=?Style.colour_map['red']?#設(shè)置單元格背景色為藍(lán)色 style.pattern?=?pattern borders?=?xlwt.Borders() borders.left?=?1 borders.right?=?1 borders.top?=?1 borders.bottom?=?1 style.borders?=?borders font?=?xlwt.Font() font.name?=?'Arial'?#字體類(lèi)型 font.colour_index?=?0?#字體顏色 font.height?=?280?#字體大小 style.font?=?font al?=?xlwt.Alignment() al.horz?=?0x02??????#?設(shè)置水平居中 al.vert?=?0x01??????#?設(shè)置垂直居中 style.alignment?=?al style1?=?XFStyle() borders?=?xlwt.Borders() borders.left?=?1 borders.right?=?1 borders.top?=?1 borders.bottom?=?1 style1.borders?=?borders style2?=?XFStyle() al?=?xlwt.Alignment() al.horz?=?0x02??????#?設(shè)置水平居中 al.vert?=?0x01??????#?設(shè)置垂直居中 style2.alignment?=?al style3?=?XFStyle() borders?=?xlwt.Borders() borders.left?=?1 #borders.left?=?xlwt.Borders.THIN borders.right?=?1 borders.top?=?1 borders.bottom?=?1 style3.borders?=?borders al?=?xlwt.Alignment() al.horz?=?0x02??????#?設(shè)置水平居中 al.vert?=?0x01??????#?設(shè)置垂直居中 style3.alignment?=?al F5Core?=?workbook.add_sheet('F5核心狀態(tài)信息',cell_overwrite_ok=True) first_col=F5Core.col(0) sec_col=F5Core.col(1) thr_col=F5Core.col(2) for_col=F5Core.col(3) five_col=F5Core.col(4) six_col=F5Core.col(5) first_col.width=150*25 sec_col.width=150*25 thr_col.width=120*25 for_col.width=180*25 five_col.width=360*25 six_col.width=400*25 F5Core.write_merge(1,39,0,0,'F5Core_12508',style3) F5Core.write_merge(1,39,1,1,'10.20.5.254',style3) F5Core.write(0,0,'設(shè)備名稱(chēng)',style) F5Core.write(0,1,'管理地址',style) F5Core.write(0,2,'檢查項(xiàng)',style) F5Core.write_merge(0,0,3,4,'位置',style) F5Core.write(0,5,'檢查結(jié)果',style) F5Core.write_merge(1,8,2,2,'設(shè)備狀態(tài)',style3) F5Core.write_merge(9,12,2,2,'運(yùn)行時(shí)間',style3) F5Core.write_merge(13,16,2,2,'運(yùn)行溫度',style3) F5Core.write_merge(17,20,2,2,'風(fēng)扇狀態(tài)',style3) F5Core.write_merge(21,24,2,2,'CPU使用率',style3) F5Core.write_merge(25,28,2,2,'內(nèi)存空置率',style3) F5Core.write_merge(29,33,2,2,'聚合口',style3) F5Core.write_merge(34,37,2,2,'IRF',style3) F5Core.write(38,2,'日志條目',style3) F5Core.write(39,2,'路由條目',style3) F5Core.write_merge(1,4,3,4,'Chassis?1',style3) F5Core.write_merge(5,8,3,4,'Chassis?2',style3) F5Core.write_merge(9,9,3,4,'MPU(M)?Chassis?1?Slot?0',style3) F5Core.write_merge(10,10,3,4,'LPU?Chassis?1?Slot?2',style3) F5Core.write_merge(11,11,3,4,'MPU(S)?Chassis?2?Slot?0',style3) F5Core.write_merge(12,12,3,4,'LPU?Chassis?2?Slot?2',style3) F5Core.write_merge(13,14,3,3,'Chassis?1',style3) F5Core.write_merge(15,16,3,3,'Chassis?2',style3) F5Core.write_merge(17,18,3,3,'Chassis?1',style3) F5Core.write_merge(19,20,3,3,'Chassis?2',style3) F5Core.write_merge(21,22,3,3,'Chassis?1',style3) F5Core.write_merge(23,24,3,3,'Chassis?2',style3) F5Core.write_merge(25,26,3,3,'Chassis?1',style3) F5Core.write_merge(27,28,3,3,'Chassis?2',style3) F5Core.write_merge(29,29,3,3,'RAGG1',style3) F5Core.write_merge(30,30,3,3,'RAGG2',style3) F5Core.write_merge(31,31,3,3,'RAGG3',style3) F5Core.write_merge(32,32,3,3,'RAGG4',style3) F5Core.write_merge(33,33,3,3,'RAGG125',style3) F5Core.write_merge(34,35,3,3,'Member?1',style3) F5Core.write_merge(36,37,3,3,'Member?2',style3) F5Core.write_merge(38,38,3,4,'',style3) F5Core.write_merge(39,39,3,4,'',style3) F5Core.write_merge(13,13,4,4,'slot0',style3) F5Core.write_merge(14,14,4,4,'slot2',style3) F5Core.write_merge(15,15,4,4,'slot0',style3) F5Core.write_merge(16,16,4,4,'slot2',style3) F5Core.write_merge(17,17,4,4,'Fan-tray?1',style3) F5Core.write_merge(18,18,4,4,'Fan-tray?2',style3) F5Core.write_merge(19,19,4,4,'Fan-tray?1',style3) F5Core.write_merge(20,20,4,4,'Fan-tray?2',style3) F5Core.write_merge(21,21,4,4,'slot0',style3) F5Core.write_merge(22,22,4,4,'slot2',style3) F5Core.write_merge(23,23,4,4,'slot0',style3) F5Core.write_merge(24,24,4,4,'slot2',style3) F5Core.write_merge(25,25,4,4,'slot0',style3) F5Core.write_merge(26,26,4,4,'slot2',style3) F5Core.write_merge(27,27,4,4,'slot0',style3) F5Core.write_merge(28,28,4,4,'slot2',style3) F5Core.write_merge(29,29,4,4,'To_F1-N7K',style3) F5Core.write_merge(30,30,4,4,'To_F1-N7K-S',style3) F5Core.write_merge(31,31,4,4,'F5?MIS?TO?F5?QMS',style3) F5Core.write_merge(32,32,4,4,'F5?MIS?TO?F5?JMET',style3) F5Core.write_merge(33,33,4,4,'To_F5-server-HJ-S7502E_RAGG',style3) F5Core.write_merge(34,34,4,4,'Ten-GigabitEthernet1/2/0/47(MDC1)',style3) F5Core.write_merge(35,35,4,4,'Ten-GigabitEthernet1/2/0/48(MDC1)',style3) F5Core.write_merge(36,36,4,4,'Ten-GigabitEthernet2/2/0/47(MDC1)?',style3) F5Core.write_merge(37,37,4,4,'Ten-GigabitEthernet2/2/0/48(MDC1)',style3) F5Core.write(1,5,power11,style3) F5Core.write(2,5,power12,style3) F5Core.write(3,5,power13,style3) F5Core.write(4,5,power14,style3) F5Core.write(5,5,power21,style3) F5Core.write(6,5,power22,style3) F5Core.write(7,5,power23,style3) F5Core.write(8,5,power24,style3) F5Core.write(9,5,time11,style3) F5Core.write(10,5,time12,style3) F5Core.write(11,5,time13,style3) F5Core.write(12,5,time14,style3) F5Core.write(13,5,environment11,style3) F5Core.write(14,5,environment12,style3) F5Core.write(15,5,environment13,style3) F5Core.write(16,5,environment14,style3) F5Core.write(17,5,fana11,style3) F5Core.write(18,5,fana12,style3) F5Core.write(19,5,fana13,style3) F5Core.write(20,5,fana14,style3) F5Core.write(21,5,cpu11,style3) F5Core.write(22,5,cpu12,style3) F5Core.write(23,5,cpu13,style3) F5Core.write(24,5,cpu14,style3) F5Core.write(25,5,memory11,style3) F5Core.write(26,5,memory12,style3) F5Core.write(27,5,memory13,style3) F5Core.write(28,5,memory14,style3) F5Core.write(29,5,briefa11,style3) F5Core.write(30,5,briefa12,style3) F5Core.write(31,5,briefa13,style3) F5Core.write(32,5,briefa14,style3) F5Core.write(33,5,briefa15,style3) F5Core.write(34,5,IRF11,style3) F5Core.write(35,5,IRF12,style3) F5Core.write(36,5,IRF13,style3) F5Core.write(37,5,IRF14,style3) F5Core.write(38,5,log11,style3) F5Core.write(39,5,routingtable11,style3) print?('創(chuàng)建excel文件完成!') workbook.save('/root/F5Core/F5Core.xls') workbook?=?xlrd.open_workbook('/root/F5Core/F5Core.xls') #輸出Excel文件中所有sheet的名字 print(workbook.sheet_names()) #?根據(jù)sheet索引或者名稱(chēng)獲取sheet內(nèi)容 Data_sheet?=?workbook.sheets()[0]??#?通過(guò)索引獲取 #?Data_sheet?=?workbook.sheet_by_index(0)??#?通過(guò)索引獲取 #?Data_sheet?=?workbook.sheet_by_name(u'名稱(chēng)')??#?通過(guò)名稱(chēng)獲取 #?輸出所有單元格的內(nèi)容 """for?i?in?range(rowNum): ????for?j?in?range(colNum): ????????print(list[i][j],?' ',?end="") ????print()""" print(Data_sheet.name)??#?獲取sheet名稱(chēng) rowNum?=?Data_sheet.nrows??#?sheet行數(shù) colNum?=?Data_sheet.ncols??#?sheet列數(shù) #?獲取所有單元格的內(nèi)容 list?=?[] for?i?in?range(rowNum): ????rowlist?=?[] ????for?j?in?range(colNum): ????????rowlist.append(Data_sheet.cell_value(i,?j)) ????list.append(rowlist) ????print(rowlist) for?listlist?in?list: ????if?'Error'?==?listlist[-1]: ????????def?sendmail(): ????????????#創(chuàng)建一個(gè)帶附件的實(shí)例 ????????????msg?=?MIMEMultipart() ????????????ctype?=?'application/octet-stream' ????????????maintype,?subtype?=?ctype.split('/',?1) ????????????file1?=?MIMEBase(maintype,?subtype) ????????????file1.set_payload(open(r'/root/F5Core/F5Core.xls',?'rb').read()) ????????????file1.add_header('Content-Disposition',?'attachment',?filename='F5核心巡檢異常.xls') ????????????encoders.encode_base64(file1) ????????????msg.attach(file1) ????????????msg_to=['x.liu@x.com','levin.xie@x.com','Care.xiang@x.com','Klaus.Wang@x.com','Eric.lai@x.com'] ????????????msg['from']?=?'Levin.xie@quantacn.com' ????????????msg['subject']?=?u"F5核心有異常" ????????????msg.attach(MIMEText('F5核心出現(xiàn)異常',?'plain',?'utf-8')) ????????????#發(fā)送郵件 ????????????msg['to']?=','.join(msg_to)#群發(fā)需要增加的,隱藏收件人不需要此行,直接調(diào)用msg_to就可以 ????????????server?=?smtplib.SMTP() ????????????server.connect('10.17.37.96',25) ????????????#server.sendmail(msg['from'],?msg['to'],msg.as_string())?單獨(dú)一個(gè)收件人 ????????????server.sendmail(msg['from'],?msg['to'].split(','),?msg.as_string())#收件人為多個(gè) ????????????#server.sendmail(msg['from'],?msg_to,?msg.as_string()) ????????????server.quit() ????????????return?'發(fā)送成功' ????????print?(sendmail())
自動(dòng)執(zhí)行代碼
Linux crontab
結(jié)果
1、正常巡檢結(jié)果
2、出現(xiàn)異常報(bào)警的巡檢結(jié)果
審核編輯:湯梓紅
-
交換機(jī)
+關(guān)注
關(guān)注
21文章
2637瀏覽量
99529 -
網(wǎng)絡(luò)設(shè)備
+關(guān)注
關(guān)注
0文章
315瀏覽量
29636 -
python
+關(guān)注
關(guān)注
56文章
4792瀏覽量
84627
原文標(biāo)題:Python網(wǎng)絡(luò)設(shè)備巡檢(異常自動(dòng)報(bào)警)
文章出處:【微信號(hào):網(wǎng)絡(luò)技術(shù)干貨圈,微信公眾號(hào):網(wǎng)絡(luò)技術(shù)干貨圈】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論