Zabbix 3.0 邮件/微信报警

1.Zabbix服务器端配置

邮件服务安装配置  请查看此文章!


Zabbix server 配置文件修改

[root@zabbix-server ~]# vim /usr/local/zabbix/etc/zabbix_server.conf 
添加或修改配置文件: 
AlertScriptsPath=/usr/local/zabbix/alertscripts #开启zabbix调用脚本存放路径位置

Zabbix 邮件报警脚本:

[root@zabbix-server alertscripts]# vim sendmail.sh
#!/bin/bash echo "$3" > /tmp/zabbix_mail.txt #此文件要有zabbix 权限不然乱码
dos2unix -k /tmp/zabbix_mail.txt #转换编码
/bin/mail -s "$2" $1 < /tmp/zabbix_mail.txt 

重启zabbix服务生效
[root@zabbix-server ~]# /etc/init.d/zabbix_server restart

Zabbix 微信报警脚本:

[root@zabbix-server alertscripts]# vim weixin.py
#!/usr/bin/python
#_*_coding:utf-8 _*_
__author__ = 'lvnian'

import urllib,urllib2
import json
import sys

def gettoken(corpid,corpsecret):
    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
    try:
        token_file = urllib2.urlopen(gettoken_url)
    except urllib2.HTTPError as e:
        print e.code
        print e.read().decode("utf8")
        sys.exit()
    token_data = token_file.read().decode('utf-8')
    token_json = json.loads(token_data)
    token_json.keys()
    token = token_json['access_token']
    return token

def senddata(access_token,user,content):
    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
    send_values = {
        "touser":user ,    #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        "toparty":"1",    #企业号中的部门id
        "msgtype":"text",  #消息类型。
        "agentid":"2",     #企业号中的应用id,
        "text":{
            "content":content
           },
        "safe":"0"
        }
    send_data = json.dumps(send_values, ensure_ascii=False)
    send_request = urllib2.Request(send_url, send_data)
    response = json.loads(urllib2.urlopen(send_request).read())
    print str(response)

if __name__ == '__main__':
    user = str(sys.argv[1])     #zabbix传过来的第一个参数
    subject = str(sys.argv[2])  #zabbix传过来的第二个参数
    content = str(sys.argv[3])  #zabbix传过来的第三个参数
    corpid =  'wx8649e3f1bba2jinc'   #CorpID是企业号的标识
    corpsecret = '3NDb5cKp-VisdJ7WGiJ6Yw_R_jRj2cUODuTmFLsOQGQYGPcc5wNLhPiAyTnpjinc'  #corpsecretSecret是管理组凭证密钥
    accesstoken = gettoken(corpid,corpsecret)
senddata(accesstoken,user,content)

关于企业号的申请和 zabbix 端配置请看完整的配置文档,图片太多步骤繁琐,就不写出来了

#注意:现在微信企业号升级为企业微信了,现在申请要企业信息注册了

zabbix 3.0 邮件微信报警.doc

 

分享