python 通过qq邮箱发送磁盘监控告警

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4632

2016/11/07 00:30


python通过qq邮箱发送磁盘监控告警

#encoding=gb2312
'''
@author: wangzi
'''
import psutil
import smtplib
from email.mime.text import MIMEText


mailto_list = ['277215243@qq.com']
mail_host = 'smtp.qq.com'
mail_user = '***@qq.com'
mail_pass = '***'
mail_postfix='qq.com'

def send_mail(to_list,sub,content):
    me = '王梓'+'<'+mail_user+'@'+mail_postfix+'>'
    msg = MIMEText(content,_subtype = 'html',_charset='gb2312')
    msg['Subject'] = sub
    msg['From'] = me
    msg['to'] = ';'.join(to_list)

    try:
        server = smtplib.SMTP()
        server.connect(mail_host)
        server.login(mail_user,mail_pass)
        server.sendmail(me,to_list,msg.as_string())
        server.close()
        return True
    except Exception as e:
        print (e)
        return False
if __name__ == '__main__':
    mainhtml = open('percent.html').read()
    fordivpercent = open('fordivpercent.html').read()
    percentcss = open('percent.css', encoding='utf-8').read()
    dist_str = ''
    for ps in psutil.disk_partitions():
        dist_str+= fordivpercent.format(ps[1],psutil.disk_usage(ps[1])[3])
    dist_str += fordivpercent.format('/Users/wangzi/www', psutil.disk_usage('/Users/wangzi/www')[3])
    mailhtml = mainhtml.format(percentcss, dist_str)
    print(mailhtml)
    if send_mail(mailto_list, '磁盘监控', mailhtml):
        print('发送成功')
    else:
        print('发送失败')

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1