zabbix企业微信报警

一、企业微信号申请

https://work.weixin.qq.com/

1.申请企业微信账号并设置

(此处步骤省略)

2.添加应用

在这里插入图片描述

1)编辑weixin.py脚本文件
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#author: zhang
#date: 2019-04-07
#comment: zabbix接入微信报警脚本
 
import requests
import sys
import os
import json
import logging
 
logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
                datefmt = '%a, %d %b %Y %H:%M:%S',
                filename = os.path.join('/tmp','weixin.log'),
                filemode = 'a')
 
corpid = 'ww4adcac9b11fd1981'
appsecret = 'kI3rXnybHpw-BX-po6hxGdYM217wo0dwtZOlKkKVZ38'
agentid = 1000003
 
#获取accesstoken
token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']
 
#发送消息
msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
 
touser=sys.argv[1]
subject=sys.argv[2]
#toparty='3|4|5|6'
message=sys.argv[3]
 
params={
        "touser": touser,
#       "toparty": toparty,
        "msgtype": "text",
        "agentid": agentid,
        "text": {
                "content": message
        },
        "safe":0
}

req=requests.post(msgsend_url, data=json.dumps(params))
 47 
 48 logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:'     + message) 
2)上传脚本文件到数据库并修改
[root@zabbix ~]# rz weixin.py
[root@zabbix ~]# vim weixin.py
corpid = 'ww50a15d892a539965'
appsecret = '_eWeLhHDjylIrQoPIE9k2C_w205cWOxL6lmjSB7FMF8'
agentid = 1000002

分别对应刚才记录的值
3)安装pip
[root@Zabbix ~]# yum install python-pip -y
4)安装requests
[root@Zabbix ~]# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
[root@Zabbix alertscripts]# chmod +x weixin.py
[root@Zabbix alertscripts]# ./weixin.py 
5)用命令行发送微信消息
[root@zabbix ~]# python weixin.py 'WuYiShuai' 'text' 2222

需要传参
第一个参数:企业微信用户名
第二个参数:标题
第三个参数:文章内容

4.配置zabbix微信报警

1)查看报警脚本存放的目录
[root@zabbix ~]# grep script /etc/zabbix/zabbix_server.conf|grep Alert
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/usr/lib/zabbix/alertscripts
2)将脚本移动到此目录并添加可执行权限
[root@zabbix ~]# mv weixin.py /usr/lib/zabbix/alertscripts/
[root@zabbix /usr/lib/zabbix/alertscripts]# chmod +x weixin.py 
3)给微信告警日志授予zabbix用户
[root@zabbix ~]# chown zabbix.zabbix /tmp/weixin.log 

二、zabbix如何设置企业微信报警?

1.单机管理->报警媒介类型-创建媒介类型

在这里插入图片描述

2.填写微信报警名称,以及脚本需要传入的参数

在这里插入图片描述

3.配置接收的企业微信号

在这里插入图片描述
在这里插入图片描述

4.动作配置

在这里插入图片描述
在这里插入图片描述

5.测试企业微信报警

6.恢复

[root@zabbix ~]# swapon -a
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值