服务器巡检shell脚本,python生成excel文档并邮件发出
背景及思路:
五一小长假之前,公司要求我做一次服务器巡检。
1、写了一个简单的脚本获取服务器的各种基础信息:cpu,内存,swap分区使用情况,磁盘,网卡信息种种,具体见脚本,将这些信息追加到一个文件中,然后在监控机上做一次汇总,汇总方式就不详谈,我用的是for循环ssh追加
2、然后利用python的xlsxwriter模块生成excel
3、最后利用python发带附件为excel的邮件到指定邮箱
获取服务器信息部分脚本:
#取所需要的内网IP
Int_ip=`ifconfig|awk '/inet addr/ {gsub(/:/," ");print $3}'|egrep "^192.168.18"|head -1`
#取除该内网ip以及127.0.0.1之外的所有ip
Out_ip_list=`ifconfig|awk '/inet addr/ {gsub(/:/," ");print $3}'|grep -v $Int_ip|grep -v 127.0.0.1`
#取内存总值,单位为G,四舍五入
Memory=`free -m|awk '/Mem/ {printf ("%.f\n",$2/1024)}'`
#取内存fr
ee值,从系统角度看,取的是第一行的free
Memory_free=`free -m|awk '/Mem/ {printf "%.f\n",$4/1024}'`
#取CPU核数
Cpu_num=`grep processor /proc/cpuinfo|wc -l`
#取服务器运行时间
Uptime=`uptime|awk '{print $3}'`
#取最近15分的负载
Load=`uptime|awk '{print $12}'`
#取磁盘大于80%的磁盘目录
Disk=`df -h|awk '{a=+$(NF-1);if(a>=80)print $NF}'`
#swap分区总值,单位为G,四舍五入
Swap=`free -m|awk '/Swap/ {printf ("%.f\n",$2/1024)}'`
#swap分区
Swap_free=`free -m|awk '/Swap/ {printf "%.f\n",$4/1024}'`
生成excel
excel.py: #!/usr/bin/python # -*- coding: utf-8 -*- from xlsxwriter import workbook import ConfigParser import time import sendmail def write_excel(file): ''' 1、设置 Excel 样式 2、将数据写入到 Excel 中 ''' # 生成 Excel 文件 work = workbook.Workbook(file) # 建立工作表,表名默认 worksheet = work.add_worksheet() # 设置字体加粗、字体大小 format_title = work.add_format({'bold': True, 'font_size': 16}) # 设置水平对齐、垂直对齐 format_title.set_align('center') format_title.set_align('vcenter') format_body = work.add_format({'font_size': 14}) # 设置样式,行高、列宽 worksheet.set_row(0, 25) worksheet.set_column(0, 7, 30) worksheet.set_column(8,9,50) worksheet.set_column(9,10,100) # 定义表头 title = ( '服务器IP', '内存大小 GB', '内存剩余 GB', 'Swap大小 GB', 'Swap剩余 GB', '运行时间 天', '系统负载 ', 'CPU 核数', '磁盘超过80%', '其余IP', ) row = 0 col = 0 #写入表头 for item in title: item = unicode(item,"utf-8") worksheet.write(row, col, item, format_title) col+=1 #写入数据 cf = ConfigParser.ConfigParser() cf.read('/data/scripts/excel/config_total.txt') for ip in cf.sections(): row+=1 col = 0 for opt in cf.options(ip): key=cf.get(ip,opt) worksheet.write(row,col,key,format_body) col+=1 work.close() if __name__ == '__main__': Excel_name = "Server_%s.xls" %(time.strftime("%Y-%m-%d", time.localtime())) write_excel(Excel_name) sendmail.send_mail('********@139.com','服务器巡检表格','fuliao server message',Excel_name) sendmail.py: #!/usr/bin/python import smtplib from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication import sys mail_host = 'smtp.163.com' mail_user = '163邮箱账号' mail_pass = '163邮箱密码' mail_postfix = '163.com' def send_mail(to_list,subject,content,name): me = mail_user+"+mail_user+"@"+mail_postfix+">" msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = me msg['to'] = to_list msg.attach(MIMEText(content)) part = MIMEApplication(open(name,'rb').read()) part.add_header('Content-Disposition','attachment', filename=name) msg.attach(part) try: s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user,mail_pass) s.sendmail(me,to_list,msg.as_string()) s.close() return True except Exception,e: print str(e) return False
上一篇: ros操作系统笔记