python监控linux磁盘空间使用情况

# -*- coding: UTF-8 -*-
'''
@author: zhanglw
'''
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
from email.mime.multipart import MIMEMultipart
import smtplib
import subprocess
import os

def sendMesg(from_addr,password,smtp_server, to_addr, content,line, ipVal):
msg = MIMEText(u'%s%s\n%s'%(ipVal,content,line), 'plain', 'utf-8')
msg['From'] = _format_addr(u'电商平台服务器 <%s>' % from_addr)
msg['To'] = ";".join(to_addr)
msg['Subject'] = Header(u'磁盘空间检查', 'utf-8').encode()
server = smtplib.SMTP()
server.connect(smtp_server,25) #25 要根据具体的邮箱服务器来决定
server.set_debuglevel(0)
dd = from_addr.split("@")
#print dd[0]
server.login(dd[0], password)
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()

def _format_addr(s):
name, addr = parseaddr(s)
return formataddr(( \
        Header(name, 'utf-8').encode(), \
        addr.encode('utf-8') if isinstance(addr, unicode) else addr))

if __name__ == '__main__':

from_addr = '用户名称'
        password = '密码'
        smtp_server = 'mail.picclife.cn'

to_addr = ['接收邮箱地址1','接收邮箱地址2']

#获取机器的ip地址
ipVal = os.popen("/sbin/ifconfig eth0| grep 'Bcast' |awk '{print $2}' |awk -F: '{print $2}'").read()
print '====>',ipVal

child = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
out = child.stdout.readlines()

for item in out:
line = item.strip().split()
#获取picclife挂载点
if '/picclife' in line:
title =[u'--容量-',u'-已用-',u'-可用-',u'-已用-',u'-挂载点--']
content= "\t".join(title)
#print content
print line
                #调用发送方法

sendMesg(from_addr,password,smtp_server, to_addr, content,line, ipVal)

##定时任务 保存为sendMail.py 文件后,添加到linux定时任务中,每小时执行一次
01 * * * * /home/weblogic/python27/bin/python2.7 /home/weblogic/sendMail.py 1>/tmp/python_disk.log 2>&1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值