天天看点

zabbix 邮箱报警

   1.编写脚本

    进入zabbix目录:vi /usr/lib/zabbix/alertscripts/mailSend.py 

#!/usr/bin/python3
import smtplib
from email.mime.text import MIMEText
import sys

# 第三方 SMTP 服务
mail_host="smtp.mxhichina.com"  #设置服务器
mail_user="[email protected]"    #用户名
mail_pass="[email protected]"   #口令

#设置发送者及接收者
sender = '[email protected]'
receivers = sys.argv[1]  #收件人,多个收件人用逗号隔开
message = MIMEText(sys.argv[3])   #邮件内容
message['Subject'] = sys.argv[2]  #邮件主题
message['From'] = sender
message['To'] =  receivers

print(message.as_string())
try:
    smtp = smtplib.SMTP()  # 连接邮箱服务器,smtp的端口号是25
    smtp.connect(mail_host, 25)    # 25 为 SMTP 端口号
    smtp.login(mail_user,mail_pass) #登录邮箱
    smtp.sendmail(sender, receivers, message.as_string()) #参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
    smtp.quit() # 发送完毕后退出smtp
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")


# cmd 命令测试
# D:\Python_workplace>Python mailSend.py  "[email protected]" "zabbix service check" "zabbix service ok"