1、python抓取交换机的信息
2、提取需要的检查结果,并写入excel表格
3、如果某一检查结果超出阈值或不符合某一关键字,自动发送报警邮件
4、每十分钟执行一次python代码
抓取交换机信息
import pexpect
import sys
import datetime
import os
#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("display power")
child.expect("%s"%name)
child.sendline("display version")
for i in range(20):
index = child.expect([name1,"%s"%name])
if ( index == 0 ):
child.send(" ")
else:
child.sendline("display environment")
break
for i in range(20):
index = child.expect([name1,"%s"%name])
if ( index == 0 ):
child.send(" ")
else:
child.sendline("display fan")
break
for i in range(10):
index = child.expect([name1,"%s"%name])
if ( index == 0 ):
child.send(" ")
else:
child.sendline("display cpu-usage")
break
for i in range(10):
index = child.expect([name1,"%s"%name])
if ( index == 0 ):
child.send(" ")
else:
child.sendline("display memory")
break
for i in range(10):
index = child.expect([name1,"%s"%name])
if ( index == 0 ):
child.send(" ")
else:
child.sendline("display interface brief")
break
for i in range(10):
index = child.expect([name1,"%s"%name])
if ( index == 0 ):
child.send(" ")
else:
child.sendline("dis logbuffer")
break
for i in range(50):
index = child.expect([name1,"%s"%name])
if ( index == 0 ):
child.send(" ")
else:
child.sendline("display ip routing-table")
break
for i in range(50):
index = child.expect([name1,"%s"%name])
if ( index == 0 ):
child.send(" ")
else:
child.sendline("display irf link ")
break
for i in range(3):
index = child.expect([name1,"%s"%name])
if ( index == 0 ):
child.send(" ")
else:
child.sendline("quit")
sys.exit()
匹配阈值,报警
#coding:utf-8
import pymssql
import xlwt
import datetime
from xlwt import *
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
import datetime
from email import encoders
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
import re
import xlrd
file = open('/root/F5Core/Core/F5hexin.txt','r+')
listlist=file.readlines()
i=1
for line in listlist:#匹配检查结果,超出阈值,把结果换成error。
if 'display power' in line:
power11=listlist[i+6][8:15].strip()
if power11 != 'Normal':
power11='Error'
print(power11)
power12=listlist[i+8][8:15].strip()
if power12 != 'Normal':
power12='Error'
print(power11)
power13=listlist[i+10][8:15].strip()
if power13 != 'Normal':
power13='Error'
print(power11)
power14=listlist[i+12][8:15].strip()
if power14 != 'Normal':
power14='Error'
print(power11)
power21=listlist[i+28][8:15].strip()
if power21 != 'Normal':
power21='Error'
print(power11)
power22=listlist[i+30][8:15].strip()
if power22 != 'Normal':
power22='Error'
print(power11)
power23=listlist[i+32][8:15].strip()
if power23 != 'Normal':
power23='Error'
print(power23)
power24=listlist[i+34][8:15].strip()
if power24 != 'Normal':
power24='Error'
print(power24)
if 'MPU(M) Chassis 1 Slot 0:' in line:
time11=listlist[i+1][9:].strip()
print(time11)
if 'LPU Chassis 1 Slot 2:' in line:
time12=listlist[i+1][9:].strip()
print(time12)
if 'MPU(S) Chassis 2 Slot 0:' in line:
time13=listlist[i+1][9:].strip()
print(time13)
if 'LPU Chassis 2 Slot 2:' in line:
time14=listlist[i+1][9:].strip()
print(time14)
if 'display environment' in line:
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()
if int(listlist[i+10][26:29].strip()) > 70:
environment11='Error'
print(environment11)
if int(listlist[i+12][26:29].strip()) > 70:
environment11='Error'
print(environment11)
if int(listlist[i+14][26:29].strip()) > 70:
environment12='Error'
print(environment12)
if int(listlist[i+16][26:29].strip()) > 70:
environment12='Error'
print(environment12)
if int(listlist[i+18][26:29].strip()) > 70:
environment12='Error'
print(environment12)
if int(listlist[i+46][26:29].strip()) > 70:
environment13='Error'
print(environment13)
if int(listlist[i+51][26:29].strip()) > 70:
environment13='Error'
print(environment13)
if int(listlist[i+53][26:29].strip()) > 70:
environment14='Error'
print(environment14)
if int(listlist[i+55][26:29].strip()) > 70:
environment14='Error'
print(environment14)
if int(listlist[i+57][26:29].strip()) > 70:
environment14='Error'
print(environment14)
if 'display fan' in line:
fana11=listlist[i+6][-8:].strip()
if fana11 != 'Normal':
fana11='Error'
print(fana11)
fana12=listlist[i+26][-8:].strip()
if fana12 != 'Normal':
fana12='Error'
print(fana12)
fana13=listlist[i+53][-8:].strip()
if fana13 != 'Normal':
fana13='Error'
print(fana13)
fana14=listlist[i+73][-8:].strip()
if fana14 != 'Normal':
fana14='Error'
print(fana14)
if 'Chassis 1 Slot 0 CPU 0 CPU usage:' in line:
cpu11=listlist[i+5][6:10].strip()
if int(re.sub('%','',cpu11)) > 30:
cpu11='Error'
print(cpu11)
if 'Chassis 1 Slot 2 CPU 0 CPU usage:' in line:
cpu12=listlist[i+5][6:10].strip()
if int(re.sub('%','',cpu12)) > 30:
cpu12='Error'
print(cpu12)
if 'Chassis 2 Slot 0 CPU 0 CPU usage:' in line:
cpu13=listlist[i+5][6:10].strip()
if int(re.sub('%','',cpu13)) > 30:
cpu13='Error'
print(cpu13)
if 'Chassis 2 Slot 2 CPU 0 CPU usage:' in line:
cpu14=listlist[i+8][6:10].strip()
if int(re.sub('%','',cpu14)) > 30:
cpu14='Error'
print(cpu14)
if 'display memory' in line:
memory11=listlist[i+8][-6:].strip()
if float(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 :' in line:
routingtable11=line[-5:].strip()
if int(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], '\t\t', 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=['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]']
msg['from'] = '[email protected]'
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())