思想
1、python抓取交換機的信息
2、提取需要的檢查結果,并寫入excel表格
3、如果某一檢查結果超出閾值或不符合某一關鍵字,自動發送報警郵件
4、每十分鐘執行一次python代碼
代碼
抓取交換機信息
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()
代碼
匹配閾值,報警
#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:#匹配檢查結果,超出閾值,把結果換成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']?#設置單元格背景色為藍色 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'?#字體類型 font.colour_index?=?0?#字體顏色 font.height?=?280?#字體大小 style.font?=?font al?=?xlwt.Alignment() al.horz?=?0x02??????#?設置水平居中 al.vert?=?0x01??????#?設置垂直居中 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??????#?設置水平居中 al.vert?=?0x01??????#?設置垂直居中 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??????#?設置水平居中 al.vert?=?0x01??????#?設置垂直居中 style3.alignment?=?al F5Core?=?workbook.add_sheet('F5核心狀態信息',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,'設備名稱',style) F5Core.write(0,1,'管理地址',style) F5Core.write(0,2,'檢查項',style) F5Core.write_merge(0,0,3,4,'位置',style) F5Core.write(0,5,'檢查結果',style) F5Core.write_merge(1,8,2,2,'設備狀態',style3) F5Core.write_merge(9,12,2,2,'運行時間',style3) F5Core.write_merge(13,16,2,2,'運行溫度',style3) F5Core.write_merge(17,20,2,2,'風扇狀態',style3) F5Core.write_merge(21,24,2,2,'CPU使用率',style3) F5Core.write_merge(25,28,2,2,'內存空置率',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?('創建excel文件完成!') workbook.save('/root/F5Core/F5Core.xls') workbook?=?xlrd.open_workbook('/root/F5Core/F5Core.xls') #輸出Excel文件中所有sheet的名字 print(workbook.sheet_names()) #?根據sheet索引或者名稱獲取sheet內容 Data_sheet?=?workbook.sheets()[0]??#?通過索引獲取 #?Data_sheet?=?workbook.sheet_by_index(0)??#?通過索引獲取 #?Data_sheet?=?workbook.sheet_by_name(u'名稱')??#?通過名稱獲取 #?輸出所有單元格的內容 """for?i?in?range(rowNum): ????for?j?in?range(colNum): ????????print(list[i][j],?' ',?end="") ????print()""" print(Data_sheet.name)??#?獲取sheet名稱 rowNum?=?Data_sheet.nrows??#?sheet行數 colNum?=?Data_sheet.ncols??#?sheet列數 #?獲取所有單元格的內容 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(): ????????????#創建一個帶附件的實例 ????????????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核心出現異常',?'plain',?'utf-8')) ????????????#發送郵件 ????????????msg['to']?=','.join(msg_to)#群發需要增加的,隱藏收件人不需要此行,直接調用msg_to就可以 ????????????server?=?smtplib.SMTP() ????????????server.connect('10.17.37.96',25) ????????????#server.sendmail(msg['from'],?msg['to'],msg.as_string())?單獨一個收件人 ????????????server.sendmail(msg['from'],?msg['to'].split(','),?msg.as_string())#收件人為多個 ????????????#server.sendmail(msg['from'],?msg_to,?msg.as_string()) ????????????server.quit() ????????????return?'發送成功' ????????print?(sendmail())
自動執行代碼
Linux crontab
結果
1、正常巡檢結果
2、出現異常報警的巡檢結果
審核編輯:湯梓紅
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
交換機
+關注
關注
21文章
2637瀏覽量
99537 -
網絡設備
+關注
關注
0文章
315瀏覽量
29636 -
python
+關注
關注
56文章
4792瀏覽量
84630
原文標題:Python網絡設備巡檢(異常自動報警)
文章出處:【微信號:網絡技術干貨圈,微信公眾號:網絡技術干貨圈】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
網絡設備選擇技巧
。為什么會這樣呢?使用過計算機的讀者都知道,當CPU風扇散熱不佳時計算機系統經常會死機或自動重啟,網絡設備更是如此,高速運行的CPU與核心組件需要在一個合適的工作環境下運轉,溫度太高會使它們損壞。設備散熱工作
發表于 04-08 09:34
變電站巡檢管理系統
:電纜接頭接觸良好、觀察電纜接頭有無發熱放電現象)等,并輸入設備實際運行狀態及參數,或選擇相應分類(正常/異常,良好/發熱),提交即可。如設備超標、異常監控中心會
發表于 11-21 11:10
HZD-HX型微機智能自動巡檢控制設備
HZD-HX型微機智能自動巡檢控制設備技術參數 1、對于消防水泵來說,普遍存在一個問題:那就是在建筑物發生火災時,有相當多的消防泵在接到啟動信號后不能啟動,沒有水無法滅火,延誤了滅火時機,給國家財產
發表于 05-26 14:06
Linux常用網絡設備
網絡設備是計算機體系結構中必不可少的一部分,處理器如果想與外界通信,通常都會選擇網絡設備作為通信接口。眾所周知,在 OSI(Open Systems Interconnection,開放網際互連)中,網絡被劃分為七個層次,從下到
發表于 07-25 07:37
家用網絡設備簡介
家用網絡設備簡介
就在幾年前,家用網絡設備還相當簡單。使用一根直連電纜、一部網絡集線器或以太網路由器,您可以將兩部或多部計算機連接在一起,共享外設、
發表于 08-05 10:26
?1058次閱讀
用Python寫SecureCRT批量登錄/巡檢網絡設備腳本
因為有約200臺的網絡設備需要每天登錄巡檢、查看配置、保存配置、定期更改密碼(每周改一次),如果都手動去登錄、配置將是非常耗費時間和人力的,所以就有了整理出腳本的想法。查看了網上大多是VBS編寫的腳本,我對VBS不熟悉,剛好前段時間初步學習了
如何使用Python通過SNMP監控網絡設備?
前段時間,為了實現自動化巡檢,我開發了自動化巡檢工具,由于我的系統設備版本比較多,所以我是分別開發的客戶端程序,服務端使用dll文件與客戶端
使用Python批量連接華為網絡設備
隨著網絡規模的擴大和設備數量的增加,手動配置和管理每臺網絡設備變得越來越不現實。因此,自動化工具和腳本變得尤為重要。Python語言以其簡潔
Juniper設備常用的巡檢命令介紹
在網絡管理中,設備巡檢是確保網絡設備運行正常、性能最佳的重要環節。Juniper Networks設備廣泛應用于各類
評論