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 %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= " ".join(title)
#print content
print line
#调用发送方法
"""
@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 %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= " ".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
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。