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  
import smtplib  
import sys
from email.mime.text import MIMEText  
from email.header import Header  
from email.Utils import COMMASPACE
  
receiver = sys.argv[1]
subject = sys.argv[2]
mailbody =  sys.argv[3]
smtpserver = 'smtp.qq.com'  
username = 'username'  
password = 'passwd'  
sender = username

msg = MIMEText(mailbody,'html','utf-8')  
msg['Subject'] = Header(subject, 'utf-8')  
msg['From'] = username
msg['To'] = receiver
  
smtp = smtplib.SMTP()  
smtp.connect(smtpserver)  
smtp.login(username, password)  
smtp.starttls()
smtp.sendmail(msg['From'], msg['To'], msg.as_string())  
smtp.quit()

测试邮件发送

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

加入到告警脚本中

#!/bin/bash
/usr/local/bin/python /usr/lib/zabbix/alertscripts/pymail.py "$1" "$2" "$3"

作者:赵班长

地址:https://www.unixhot.com/article/139464

猜你喜欢

转载自blog.csdn.net/yetugeng/article/details/86635105