python定时任务巡检写入excel_服务器巡检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+""

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','p_w_upload', 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

最后生成的excel图:

效果在上面

ps:上传文件非法,需要脚本的留个邮箱吧,不好意思

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值