1.注册企业微信账号
邀请好友
查看通讯录信息
创建应用信息
记录用户ID和密码
编辑微信报警的python.py脚本
[root@myserver_1 test]# cat wechat.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
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='ww0ffff4baf7b8a1bf'
appsecret='gmSS3j4seH2TGQh0fEOMLjH-2NT146TWaT2Vfr8jafg'
agentid=1000002
#获取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[2] + "\n\n" +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))
logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)
下载脚本中依赖插件(pip 依赖epel.repo)
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y python-pip
pip install request -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
[root@myserver_1 test]# python wechat.py 'YangHuaDong' '微信报警' '小伙子赶紧查看'
[root@myserver_1 test]#
手机端查收验证:
以上是代码脚本验证微信报警,下面是对zabbix发件人和收件人实现zabbix报警
[root@myserver_1 test]# egrep -Ev '^$|#' /etc/zabbix/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=123456
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
将zabbix报警脚本放到 /usr/lib/zabbix/alertscripts 目录下并赋予执行权限和用户权限
编辑zabbix-web客户端的收件人信息
上面填写的参数可以参考官网,粘贴复制即可
https://www.zabbix.com/documentation/3.0/manual/introduction/whatsnew300
官网参数如下
点击添加后返回管理页面查看
编辑收件人信息
点击添加后测试结果如下: