python代码如何利用阿里云发送邮件

此代码是磁盘使用率超过90%就会发送邮件
#!/usr/bin/env python
#coding:utf-8
import socket
import re
import os
import smtplib
import datetime
import commands,time 
from email.mime.text import MIMEText
from email.utils import formataddr
# 硬盘使用率报警阀值
m=socket.gethostname()
s="磁盘使用率超过:"
b="测试服务器"
d="%"
hd_usage_rate_threshold = 90
def check_hd_use():
        cmd_get_hd_use = '/bin/df'
        try:
            fp = os.popen(cmd_get_hd_use)
        except:
            ErrorInfo = r'get_hd_use_error'
            print ErrorInfo
            return ErrorInfo
        re_obj = re.compile(r'^/dev/.+\s+(?P<used>\d+)%\s+(?P<mount>.+)')
        hd_use = {}
        for line in fp:
            match = re_obj.search(line)
            if match is not None:
                hd_use[match.groupdict()['mount']] = match.groupdict()['used']
        fp.close()
        return  hd_use
def email(message):
msg = MIMEText(message, 'plain', 'utf-8')
msg['From'] = formataddr(["运维专用小号", '1195257477@qq.com'])
msg['To'] = formataddr(["大汉", '610323566@qq.com'])
msg['Subject'] = "磁盘监控报警"
server = smtplib.SMTP_SSL("smtp.qq.com", 465)
server.login("1195257477@qq.com", "xxxxxxxxxx")
server.sendmail('1195257477@qq.com', ['610323566@qq.com'], msg.as_string())
server.quit()
if __name__ == '__main__':
      for v in check_hd_use().values():
            if int(v)>hd_usage_rate_threshold:
               email(m+b+s+str(hd_usage_rate_threshold)+d)
python代码如何利用阿里云发送邮件python代码如何利用阿里云发送邮件

上一篇:Web服务器初始化脚本


下一篇:yum相关问题