python交换机自动化巡检
概述
脚本功能功能是全程模拟了人的手工巡检,并收集数据,处理后发送邮件。主要分为远程到交换机,查看转换机状态如:show process cpu | in CPU utilization查看cpu占用率;将查询后的结果记录并保存下来,如结果;11%;读取查询的数据文件;将文件中的数据如11%提取部分需要计算出来;创建excel表格,将上一步获取到的数据填写到表格中;编写固定格式的邮件内容,并将保存好的excel表格附到邮件中发送;编写crontab定时任务,实现全程自动化。
流程图
代码
''' Author:myfu Date:2019-08-26 E-mail:myfu@****.com ''' import pexpect import sys import time import os import xlwt def cisco_check(): ip=["10.3.240.14","10.3.240.15","10.3.240.17","10.3.240.20","10.3.240.21"] name=["SZNS-KJY4F-ACS-05#","SZNS-KJY4F-ACS-06#","SZNS-KJY4F-ACS-08#","SZNS-KJY9F-ACS-11#","SZNS-KJY9F-ACS-12#"] stat_time=time.time() stru_time=time.localtime(stat_time) strf_time=time.strftime("%Y-%m-%d") for i in range(0,len(ip)): child=pexpect.spawn('ssh fumy@%s'%ip[i]) fout=open('/root/switch_check/%s.txt'%(name[i][:-1]),'wb+') child.logfile = fout if ip[i]=="10.3.240.21": child.expect("fumy@10.3.240.21's password:") else: child.expect('password: ') child.sendline("你自己的密码") child.expect("%s"%name[i]) child.sendline("sh env power") child.expect("%s"%name[i]) child.sendline("show process cpu | in CPU utilization") child.expect("%s"%name[i]) child.sendline("sh version | in uptime") child.expect("%s"%name[i]) child.sendline("show processes memory | in Used") child.expect("%s"%name[i]) child.sendline("sh env fan") child.expect("%s"%name[i]) child.sendline("sh log | in Console logging") child.expect("%s"%name[i]) child.sendline("exit") time.sleep(3) file_list=os.listdir("/root/switch_check") with open("/root/switch_check/%s"%file_list[0]) as f1: for i in f1: if "POWER" in i: power1=i[8:].strip() if "CPU utilization" in i: cpu1=i[72:].strip() if "%s uptime is"%file_list[0][:-4] in i: uptime1="".join(i[27:].strip().split(",")) if "Processor Pool Total:" in i: memory1="%.2f%%"%((int(i[38:50].strip())/int(i[21:33].strip()))*100) if "FAN is" in i: fan1=i[6:].strip() if "level debugging" in i: log1=i[37:43].strip() with open("/root/switch_check/%s"%file_list[1]) as f2: for i in f2: if "POWER" in i: power2=i[8:].strip() if "CPU utilization" in i: cpu2=i[72:].strip() if "%s uptime is"%file_list[1][:-4] in i: uptime2="".join(i[27:].strip().split(",")) if "Processor Pool Total:" in i: memory2="%.2f%%"%((int(i[38:50].strip())/int(i[21:33].strip()))*100) if "FAN is" in i: fan2=i[6:].strip() if "level debugging" in i: log2=i[37:43].strip() with open("/root/switch_check/%s"%file_list[2]) as f3: for i in f3: if "POWER" in i: power3=i[8:].strip() if "CPU utilization" in i: cpu3=i[72:].strip() if "%s uptime is"%file_list[2][:-4] in i: uptime3="".join(i[27:].strip().split(",")) if "Processor Pool Total:" in i: memory3="%.2f%%"%((int(i[38:50].strip())/int(i[21:33].strip()))*100) if "FAN is" in i: fan3=i[6:].strip() if "level debugging" in i: log3=i[37:43].strip() with open("/root/switch_check/%s"%file_list[3]) as f4: for i in f4: if "POWER" in i: power4=i[24:].strip() if "CPU utilization" in i: cpu4=i[72:].strip() if "%s uptime is"%file_list[3][:-4] in i: uptime4="".join(i[27:].strip().split(",")) if "Total: " in i: memory4="%.2f%%"%((int(i[21:29].strip())/int(i[6:14].strip()))*100) if "FAN is" in i: fan4=i[6:].strip() if "level debugging" in i: log4=i[37:45].strip() with open("/root/switch_check/%s"%file_list[4]) as f5: for i in f5: if "POWER" in i: power5=i[24:].strip() if "CPU utilization" in i: cpu5=i[72:].strip() if "%s uptime is"%file_list[4][:-4] in i: uptime5="".join(i[27:].strip().split(",")) if "Total: " in i: memory5="%.2f%%"%((int(i[21:29].strip())/int(i[6:14].strip()))*100) if "FAN is" in i: fan5=i[6:].strip() if "level debugging" in i: log5=i[37:44].strip() workbook = xlwt.Workbook(encoding = 'utf-8') cisco = workbook.add_sheet('深圳网络设备巡检') style = xlwt.XFStyle() pattern = xlwt.Pattern() pattern.pattern = xlwt.Pattern.SOLID_PATTERN pattern.pattern_fore_colour = xlwt.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 al = xlwt.Alignment() al.horz = 0x02 al.vert = 0x01 style.alignment = al style1 = xlwt.XFStyle() borders = xlwt.Borders() borders.left = 1 borders.right = 1 borders.top = 1 borders.bottom = 1 style1.borders = borders al = xlwt.Alignment() al.horz = 0x02 al.vert = 0x01 style1.alignment = al style3 = xlwt.XFStyle() borders = xlwt.Borders() borders.left = 1 borders.right = 1 borders.top = 1 borders.bottom = 1 style3.borders = borders al = xlwt.Alignment() al.horz = 0x02 al.vert = 0x01 style3.alignment = al first_col=cisco.col(0) sec_col=cisco.col(1) thr_col=cisco.col(2) for_col=cisco.col(3) first_col.width=200*25 sec_col.width=120*25 thr_col.width=120*25 for_col.width=320*25 cisco.write(0, 0,'设备名称',style) cisco.write(0,1, '管理地址',style) cisco.write(0,2, '检查项',style) cisco.write(0,3, '检查结果',style) cisco.write_merge(1,6,0,0,'%s'%file_list[0][:-4],style3) cisco.write_merge(1,6,1,1,'%s'%ip[0],style3) cisco.write(1,2, '电源状态',style1) cisco.write(2,2, 'cpu使用率',style1) cisco.write(3,2, '运行时间',style1) cisco.write(4,2, '内存使用率',style1) cisco.write(5,2, '风扇状态',style1) cisco.write(6,2, '日志条目',style1) cisco.write(1,3, power1,style3) cisco.write(2,3, cpu1,style3) cisco.write(3,3, uptime1,style3) cisco.write(4,3, memory1,style3) cisco.write(5,3, fan1,style3) cisco.write(6,3, log1,style3) cisco.write_merge(7,12,0,0,'%s'%file_list[1][:-4],style3) cisco.write_merge(7,12,1,1,'%s'%ip[1],style3) cisco.write(7,2, '电源状态',style1) cisco.write(8,2, 'cpu使用率',style1) cisco.write(9,2, '运行时间',style1) cisco.write(10,2, '内存使用率',style1) cisco.write(11,2, '风扇状态',style1) cisco.write(12,2, '日志条目',style1) cisco.write(7,3, power2,style3) cisco.write(8,3, cpu2,style3) cisco.write(9,3, uptime2,style3) cisco.write(10,3, memory2,style3) cisco.write(11,3, fan2,style3) cisco.write(12,3, log2,style3) cisco.write_merge(13,18,0,0,'%s'%file_list[2][:-4],style3) cisco.write_merge(13,18,1,1,'%s'%ip[2],style3) cisco.write(13,2, '电源状态',style1) cisco.write(14,2, 'cpu使用率',style1) cisco.write(15,2, '运行时间',style1) cisco.write(16,2, '内存使用率',style1) cisco.write(17,2, '风扇状态',style1) cisco.write(18,2, '日志条目',style1) cisco.write(13,3, power3,style3) cisco.write(14,3, cpu3,style3) cisco.write(15,3, uptime3,style3) cisco.write(16,3, memory3,style3) cisco.write(17,3, fan3,style3) cisco.write(18,3, log3,style3) cisco.write_merge(19,24,0,0,'%s'%file_list[3][:-4],style3) cisco.write_merge(19,24,1,1,'%s'%ip[3],style3) cisco.write(19,2, '电源状态',style1) cisco.write(20,2, 'cpu使用率',style1) cisco.write(21,2, '运行时间',style1) cisco.write(22,2, '内存使用率',style1) cisco.write(23,2, '风扇状态',style1) cisco.write(24,2, '日志条目',style1) cisco.write(19,3, power4,style3) cisco.write(20,3, cpu4,style3) cisco.write(21,3, uptime4,style3) cisco.write(22,3, memory4,style3) cisco.write(23,3, fan4,style3) cisco.write(24,3, log4,style3) cisco.write_merge(25,30,0,0,'%s'%file_list[4][:-4],style3) cisco.write_merge(25,30,1,1,'%s'%ip[4],style3) cisco.write(25,2, '电源状态',style1) cisco.write(26,2, 'cpu使用率',style1) cisco.write(27,2, '运行时间',style1) cisco.write(28,2, '内存使用率',style1) cisco.write(29,2, '风扇状态',style1) cisco.write(30,2, '日志条目',style1) cisco.write(25,3, power5,style3) cisco.write(26,3, cpu5,style3) cisco.write(27,3, uptime5,style3) cisco.write(28,3, memory5,style3) cisco.write(29,3, fan5,style3) cisco.write(30,3, log5,style3) workbook.save('SZ_NDC_%s.xls'%strf_time) os.system("mail -a /root/python/SZ_NDC_%s.xls -s check_test 你的邮箱 < /root/python/mail.txt "%strf_time) os.system("find /root/python/ -type f -mtime +15 -name 'SZ_NDC_*'|xargs \rm -f") cisco_check()
mail.txt中的内容即邮件内容:
各位好:
巡检结果详情请看附件。
ps:本邮件由系统自动发送,如有任何问题请联系***
Best Wishes
邮件配置
/etc/mail.rc
set from=邮箱地址 smtp=邮箱服务器 smtp-auth-user=邮箱用户名 smtp-auth-password=密码 smtp-auth=login
定时任务
crontab -e
00 17 * * 5 /usr/bin/python3 /root/python/switch_check.py >/dev/null 2>&1 #每周五下午5点巡检
最终效果