第四篇、【Zabbix增加微信告警】[必备]
0、说明
这里采用的是Python程序发送企业微信,所以需要安装Python环境
1、下载Python 3版本以上的版本
下载地址:https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
2、开始安装Python环境
#安装依赖库 [root@filestore-v2 ~]# yum install gcc gcc-c++ glibc-devel glibc zlib-devel zlib openssl-devel openssl sqlite-devel readline-devel -y #解压进入安装目录 [root@filestore-v2 Python-3.6.6]# tar xvf Python-3.6.6.tgz && cd Python-3.6.6 #编译安装软件 [root@filestore-v2 Python-3.6.6]# ./configure --prefix=/usr/local/Python-3.6.6 --enable-shared --with-ssl && make && make install
#查看安装的目录
[root@filestore-v2 Python-3.6.6]# tree -L 1 /usr/local/Python-3.6.6/
/usr/local/Python-3.6.6/
├── bin
├── include
├── lib
└── share
#配置动态连接库
[root@filestore-v2 Python-3.6.6]# vi /etc/ld.so.conf.d/Python-3.6.6.conf
/usr/local/Python-3.6.6/lib
#刷新动态连接库
[root@filestore-v2 ~]# ldconfig
#这里不增加环境变量,只做软链接(看个人喜好决定)
[root@filestore-v2 ~]# ln -s /usr/local/Python-3.6.6/bin/python3 /sbin/python3
[root@filestore-v2 ~]# ln -s /usr/local/Python-3.6.6/bin/pip3 /sbin/pip3
#测试是否安装成功
[root@filestore-v2 ~]# python3 #这样子表示成功
Python 3.6.6 (default, Dec 25 2019, 15:40:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
3、安装requests模块
[root@filestore-v2 ~]# pip3 install requests
4、创建发送脚本程序
#!/usr/bin/env python # -*- coding: utf-8 -*- import requests import json import sys # 企业号及应用相关信息 corp_id = '企业ID' corp_secret = '应用的密钥' agent_id = '应用的ID' # 存放access_token文件路径 file_path = '/tmp/access_token.log' def get_access_token_from_file(): try: f = open(file_path, 'r+') this_access_token = f.read() print('get success %s' % this_access_token) f.close() return this_access_token except Exception as e: print(e) # 获取token函数,文本里记录的token失效时调用 def get_access_token(): get_token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (corp_id, corp_secret) print(get_token_url) r = requests.get(get_token_url) request_json = r.json() this_access_token = request_json['access_token'] print(this_access_token) r.close() # 把获取到的access_token写入文本 try: f = open(file_path, 'w+') f.write(this_access_token) f.close() except Exception as e: print(e) # 返回获取到的access_token值 return this_access_token # snedMessage # 死循环,直到消息成功发送 flag = True while (flag): # 从文本获取access_token access_token = get_access_token_from_file() try: to_user = '@all' message = sys.argv[3] send_message_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s' % access_token # print(send_message_url) message_params = { "touser": to_user, "msgtype": "text", "agentid": agent_id, "text": { "content": message }, "safe": 0 } r = requests.post(send_message_url, data=json.dumps(message_params)) print('post success %s ' % r.text) # 判断是否发送成功,如不成功则跑出异常,让其执行异常处理里的函数 request_json = r.json() errmsg = request_json['errmsg'] if errmsg != 'ok': raise ValueError('发送告警微信失败!') # 消息成功发送,停止死循环 flag = False except Exception as e: print(e) access_token = get_access_token()
测试结果
[root@filestore-v2 ~]# python3 send_wx_alarm.py "arg1" "arg2" "arg3" #<==只接收第三个值 get success mp-TSxVbLrMC_L2OgB63LTIVxoWyyZ5IpmWwqdxnmsE3zdk0TFsB3o0Z20ar9FCziFv7zNtGIANbimMpEKPiFkfiaB_xYeIZl_dEkc_l6Pwl8jxcNd82my7I9Ry5ht4BYp9FzlT4V-Tc3wC9Qpt3vekaIiQxFtqPfDiA00TkXs5xcfPuNH6owb4_sqIdS7RmC1oWboloLANq7qdJxP6PRg post success {"errcode":0,"errmsg":"ok","invaliduser":""}
5、拷贝告脚本到zabbix服务外部脚本目录
#剪切到zabbix服务调用脚本的目录 [root@filestore-v2 ~]# mv send_wx_alerm.py /data/application/zabbix-4.4.3/share/zabbix/externalscripts/ #授权用户和组 [root@filestore-v2 ~]# chown zabbix.zabbix /data/application/zabbix-4.4.3/share/zabbix/externalscripts/send_wx_alarm.py #增加可执行权限 [root@filestore-v2 ~]# chmod 755 /data/application/zabbix-4.4.3/share/zabbix/externalscripts/send_wx_alarm.py #删除手动测试脚本产生的日志,这部很重要,如果临时文件是root的话,用zabbix用户永远是运行不的 [root@filestore-v2 ~]# rm -f /tmp/access_token.log
6、创建告警脚本
[root@filestore-v2 ~]# cat /data/application/zabbix-4.4.3/share/zabbix/alertscripts/send_wx.sh #!/bin/bash #$1 : 发送相关人员 #$2 : 告警标题 #$3 : 告警内容 to=$1 subject=$2 body=$3 /usr/local/Python-3.6.6/bin/python3 /data/application/zabbix-4.4.3/share/zabbix/externalscripts/send_wx_alerm.py "$to" "$subject" "$body"
#授权用户 [root@filestore-v2 ~]# chown zabbix.zabbix /data/application/zabbix-4.4.3/share/zabbix/alertscripts/send_wx.sh #设置可执行权限 [root@filestore-v2 ~]# chmod 755 /data/application/zabbix-4.4.3/share/zabbix/alertscripts/send_wx.sh
7、创建【报警媒介类型】
7.1、【管理】=>【报警媒介类型】=>【创建媒体类型】
7.2、填写配置的数据,如下配置即可
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
7.3、直接测试是否微信告警生效
开始下发微信告警内容
查看测试的结果
下一篇【微信告警与监控项触发关联】请点击查看