zabbix系统邮件告警Python脚本

时间:2021-09-05 23:41:36

zabbix系统邮件告警配置有很多方式,如系统自带的mail,还有zabbix+msmtp+mutt,但是使用Python脚本应该来说还是一个非常简捷有效的方式。主要有如下步骤;

1、设置zabbix_server.conf文件

AlterScript = /usr/local/zabbix/share/zabbix/alterscript

2、建立告警脚本

cd /usr/local/zabbix/share/zabbix/alterscript/

sudo vim sendEmail.sh

#!/usr/bin/env python
#coding:utf-8
import time
import smtplib
import logging
from email.mime.text import MIMEText
import sys LOG_FILENAME="/var/log/email_python.log" mail_host = 'mstp.163.com' mail_user = 'xxxx@xxxx.com' mail_pass = 'xxxxx' mail_postfix = 'xxxxx' def send_mail(to_list,subject,content,format='html'): try: me=mail_user+"<"+mail_user+"@"+mail_postfix+">" msg=MIMEText(content,format,'utf-8') msg["Accept-Language"]="zh-CN" msg["Accept-Charset"]="ISO-8859-1,utf-8" msg['Subject']=subject msg['From']=me msg['to']=to_list s=smtplib.SMTP() s.connect(mail_host,"25") s.login(mail_user,mail_pass) s.sendmail(me,to_list,msg.as_string()) s.close() except Exception,e: logging.basicConfig(filename = LOG_FILENAME, level = logging.DEBUG) logging.error(time.strftime('%Y-%m-%d %H:%I:%M',time.localtime(time.time()))+e) if __name__ == "__main__": send_mail(sys.argv[1],sys.argv[2],sys.argv[3])

3、设置告警脚本的权限

sudo chmod zabbix:zabbix sendEmail.sh

sudo chown 755 sendEmail.sh

4、最后只需要在zabbix前端进行相应的配置即可。