Zabbix告警发送邮件时附带性能图(2)

mailText = """
<html xmlns="http://www.w3.org/1999/xhtml">
<html><head><meta charset="utf-8" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>$sub</title></head>
<body>
 
<table cellspacing="0" cellpadding="0">
<tbody><tr><td><center><div><div>
<img src="https://www.linuxidc.com/Linux/2018-05/%s"  alt="591" title="591"/>
</a></div></div></center></td></tr></tbody></table>
<table cellspacing="0" cellpadding="0">
<tbody><tr><td><center><div>
<table cellspacing="0" cellpadding="8"><tbody><tr><td>
%s</td></tr><tr><td>
%s</td></tr><tr><td>
%s</td></tr><tr><td>
%s</td></tr><tr><td>
%s</td></tr><tr><td>
%s</td></tr><tr><td>
%s</td></tr><tr><td>
%s</td></tr><tr><td>
<img src="https://www.linuxidc.com/Linux/2018-05/cid:p"></tr><tr><td>
</td></tbody></table></td></tr></tbody></table></div></center>    </td></tr></tbody></table>
<table cellspacing="0" cellpadding="0"></table></body>
</html>
""" % (logPng,mailSubject,triggerHost,triggerTime,triggerLevel,triggerName,triggerKey,triggerText,triggerStatus)
 
def SendMail(Recipient,Subject,Text,Img=None):
    msg = MIMEMultipart('alternative')
    msg['From']= "%s<%s>" % (MAILNAME,MAILUSER)
    msg['Subject'] = Header(Subject,'utf-8').encode()
    msg['To'] = Recipient
    if Img :
        fp = open(Img,'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID','p')
        msg.attach(msgImage)
    part = MIMEText(Text, 'html', 'utf-8')
    msg.attach(part)
 
    smtp = smtplib.SMTP_SSL(SMTPSERVER,'465')
    try:
        smtp.login(MAILUSER,MAILPASS)
        smtp.sendmail(MAILUSER,Recipient,msg.as_string())
    except Exception as e:
        print e
    finally:
        smtp.quit()

最后脚本最前面进行一个判断,判断一分钟内的性能图就不再重复去zabbix上读,直接从本地上读。

然后计划任务再计划清除图片

*/5 * * * * find /tmp  -cmin +1 -name 'zabbix-graph-itemID*' -delete

Zabbix告警发送邮件时附带性能图

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/466c8c54923684e3a6830346a6bdc51d.html