思想
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、出现异常报警的巡检结果
审核编辑:汤梓红
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。
举报投诉
-
交换机
+关注
关注
20文章
2615浏览量
99180 -
网络设备
+关注
关注
0文章
307浏览量
29595 -
python
+关注
关注
55文章
4777浏览量
84417
原文标题: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
•1049次阅读
用Python写SecureCRT批量登录/巡检网络设备脚本
因为有约200台的网络设备需要每天登录巡检、查看配置、保存配置、定期更改密码(每周改一次),如果都手动去登录、配置将是非常耗费时间和人力的,所以就有了整理出脚本的想法。查看了网上大多是VBS编写的脚本,我对VBS不熟悉,刚好前段时间初步学习了
如何使用Python通过SNMP监控网络设备?
前段时间,为了实现自动化巡检,我开发了自动化巡检工具,由于我的系统设备版本比较多,所以我是分别开发的客户端程序,服务端使用dll文件与客户端
使用Python批量连接华为网络设备
随着网络规模的扩大和设备数量的增加,手动配置和管理每台网络设备变得越来越不现实。因此,自动化工具和脚本变得尤为重要。Python语言以其简洁
Juniper设备常用的巡检命令介绍
在网络管理中,设备巡检是确保网络设备运行正常、性能最佳的重要环节。Juniper Networks设备广泛应用于各类
评论