zabbix-3.2 配置邮件告警

时间:2021-09-17 08:48:10

在/usr/lib/zabbix/alertscripts 目录下建立send_mail.py的python脚本。如下:

#!/usr/bin/python
#coding:utf-8
import smtplib  
from email.mime.text import MIMEText  
from sys import argv
mailto_list=[] 
mail_host="smtp.126.com:25"  //如果是163邮箱这里改成163
mail_user="xxx@126.com"  //这里改     
mail_pass="abcd1234"  //这里填写的不是邮箱的登陆密码,是客户端授权密码
mail_postfix="126.com" 
debug_level=0       


def send_mail(to_list,sub,content):  
    me=mail_user
    msg = MIMEText(content,_subtype='plain',_charset='utf-8')  
    msg['Subject'] = sub  
    msg['From'] = me  
    msg['To'] = ";".join(to_list)  
    try:  
        server = smtplib.SMTP()  
        server.set_debuglevel(debug_level)    
        server.connect(mail_host)  
        server.login(mail_user,mail_pass)  
        server.sendmail(me, to_list, msg.as_string())  
        server.close()  
        return True  
    except Exception, e:  
        print str(e)  
        return False 
if __name__ == '__main__':
    try:
        mailto_list=argv[1].split(';')
        sub=argv[2]
        content=argv[3]
    except:
        print "python send_mail.py 'user1@xx.com;user2@xx.com' sub content"
        exit()
    if send_mail(mailto_list,sub,content):  
        print "send ok"  
    else:  
        print "send error"

脚本添加可执行权限:chmod a+x send_mail.py

客户端授权密码设置:

zabbix-3.2 配置邮件告警


测试邮件: 执行如下命令 

zabbix-3.2 配置邮件告警