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\d+)%\s+(?P.+)')
        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(["运维专用小号", '[email protected]'])
msg['To'] = formataddr(["大汉", '[email protected]'])
msg['Subject'] = "磁盘监控报警"
server = smtplib.SMTP_SSL("smtp.qq.com", 465)
server.login("[email protected]", "xxxxxxxxxx")
server.sendmail('[email protected]', ['[email protected]'], 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)
6fc9b9972856b0f9fca1bbb2f77337a2688b5788 6fc9b9972856b0f9fca1bbb2f77337a2688b5788
更多教程

你可能感兴趣的:(python代码如何利用阿里云发送邮件)