天天看点

ZABBIX告警脚本——python发送邮件

自定义Zabbix告警,首先需要编写告警脚本,告警脚本需要支持3个参数:

1.告警接收人

2.主题

3.内容

生产建议使用Shell脚本作为告警脚本,同时需要日志记录,也可以在Shell脚本中完成,然后可以再使用Shell脚本调用其它的脚本。这样可以随时更换告警介质,也可以做好日志记录。

1.最简单的告警脚本

[ root @ linux-node1 alertscripts] #cat pymail.sh

#!/ bin / bash

echo $ 1 $ 2 $ 3 >> /tmp/alter.txt

2.编写告警脚本

#!/ usr / bin / python

#coding:utf-8              

从email.mime.text

导入smtplib

import sys 从email.header导入MIMEText导入

来自email.Utils的头

文件导入COMMASPACE

receiver = sys.argv [ 1]

subject = sys。的argv [ 2]

mailbody = sys.argv中[ 3]

SMTPSERVER = 'smtp.qq.com'

的用户名= '用户名'

口令= 'passwd的'

发送者的用户名=

味精= MimeText用于(mailbody, 'HTML', 'UTF-8')

msg [ 'Subject'] = Header(subject,'utf-8')

msg [ 'From'] = username

msg [ 'To'] =接收者

smtp = smtplib.SMTP()

smtp.connect(smtpserver)

smtp.login(用户名,密码)

smtp.starttls()

smtp.sendmail(msg [ 'From'],msg [ 'To'],msg.as_string())

smtp.quit()

测试邮件发送

[ root @ linux-node1 alertscripts] #python pymail.py [email protected] testmail test

加入到告警脚本中

/ usr / local / bin / python /usr/lib/zabbix/alertscripts/pymail.py“$ 1”“$ 2”“$ 3”