zabbix 自定义脚本发送邮件

Posted on Posted in zabbix

zabbix 自定义脚本发送邮件

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
import smtplib
import configparser

from email.mime.text import MIMEText

def sendZabbix(mailway):
    host,port,user,passwd,ssl = configParsing(mailway)
    to,subject,message = getArgv()
    message = message.replace('\n','<br />')
    to = to.split(',')
    templateMail = """<div style='border-bottom-width: 1px; font-weight: bold; color: #ff0000; border-bottom-style: dashed; border-bottom-color: rgb(204, 204, 204); z-index: 1; position: static;'>%s</div>""" % message
    msg = MIMEText(templateMail,"html","utf-8")
    msg['Subject'] = subject
    msg['From'] = "monitor<"+user+">"
    msg['To'] = ','.join(to)
    try:
        if ssl == '0':
            server = smtplib.SMTP()
            server.connect(host,port)
            server.starttls()
        elif ssl == '1':
            server = smtplib.SMTP_SSL()
            server.connect(host,port)
        else:
            print('参数错误')
            sys.exit()
        server.login(user,passwd)
        server.sendmail(user,to,msg.as_string())
        server.quit()
        print('发送成功')
    except Exception as e:
        print('发送失败 {}'.format(str(e)))


def getArgv():
    '''获取参数'''
    TO = sys.argv[1]
    SUBJECT = sys.argv[2]
    MESSAGE = sys.argv[3]
    return TO,SUBJECT,MESSAGE

def configParsing(mail):
    '''
    读取ini配置文件
    '''
    config = configparser.ConfigParser()
    '''
    ini 文件格式
    [mailmonitor]
    host=smtp.xxx.com
    port=25
    user=monitor@xxx.com
    passwd=xxx
    ssl=0
    [qq]
    host=smtp.qq.com
    port=465
    user=123456@qq.com
    passwd=xxx
    ssl=1
    '''
    config.read("/usr/local/zabbix/share/zabbix/alertscripts/zabbix-mail.ini")
    mailmonitorOptions = config[mail]
    host = mailmonitorOptions['host']
    port = mailmonitorOptions['port']
    user = mailmonitorOptions['user']
    passwd = mailmonitorOptions['passwd']
    ssl = mailmonitorOptions['ssl']
    return host,port,user,passwd,ssl

if __name__=="__main__":
    '''
    zabbix 对应的参数
    1. {ALERT.SENDTO}   发送对方
    2. {ALERT.SUBJECT}  主题
    3. {ALERT.MESSAGE}  内容
    '''
    usage = '需要三个参数'
    argvlen = len(sys.argv)
    if argvlen != 4:
        print(usage)
        sys.exit()
    sendZabbix('qq')

zabbix 触发器通知模板

操作

默认标题

{TRIGGER.STATUS}.{TRIGGER.NAME}

消息内容

Problem started at {EVENT.TIME} on {EVENT.DATE}
Problem name: {TRIGGER.NAME}
Host: {HOST.NAME}
主机IP: {HOST.IP}
问题详情:{ITEM.NAME}:{ITEM.VALUE} 
目前状态status:{TRIGGER.STATUS}
告警级别Severity: {TRIGGER.SEVERITY}

Original problem ID: {EVENT.ID}
{TRIGGER.URL}

恢复操作

默认标题

{TRIGGER.STATUS}: {TRIGGER.NAME}

消息内容

接收人:{TRIGGER.STATUS}: {TRIGGER.NAME} 
恢复主机:{HOST.NAME} 
主机地址:{HOST.IP} 
恢复时间:{EVENT.DATE} {EVENT.TIME} 
恢复等级:{TRIGGER.SEVERITY} 
恢复信息:{TRIGGER.NAME} 
问题详情:{ITEM.NAME}:{ITEM.VALUE} 
事件代码:{EVENT.ID}

确认操作

默认标题

{TRIGGER.NAME}

消息内容

{USER.FULLNAME} acknowledged problem at {ACK.DATE} {ACK.TIME} with the following message:
{ACK.MESSAGE}

Current problem status is {EVENT.STATUS}
» 转载请注明来源:呢喃 » zabbix 自定义脚本发送邮件