我是靠谱客的博主 酷酷钢笔,最近开发中收集的这篇文章主要介绍zabbix wechat告警,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

注:注册企业微信号这里省略了。空间有限一般都没有发图片。

一、修改zabbix scripts指向保证指向wechat.pypi
# grep alertscripts /etc/zabbix/zabbix_server.conf
AlertScriptsPath=/usr/lib/zabbix/alertscripts

二、下载python调用企业微信脚本
wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz
tar zxvf simplejson-3.8.2.tar.gz && cd simplejson-3.8.2
python setup.py build
python setup.py install

git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.git
cp Zabbix-Alert-WeChat/wechat.py /usr/lib/zabbix/alertscripts/
cd /usr/lib/zabbix/alertscripts/
chmod +x wechat.py && chown zabbix:zabbix wechat.py

三、配置wechat.py
# cat /usr/lib/zabbix/alertscripts/wechat.py
#!/usr/bin/python
#_*_coding:utf-8 _*_

import urllib,urllib2
import json
import sys
import simplejson

reload(sys)
sys.setdefaultencoding(‘utf-8’)

def gettoken(corpid,corpsecret):
gettoken_url = ‘https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=’ + corpid + ‘&corpsecret=’ + corpsecret
print  gettoken_url
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,subject,content):

send_url = ‘https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=’ + access_token
send_values = {
“touser”:user,    #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
“toparty”:”2″,    #企业号中的部门id。
“msgtype”:”text”, #消息类型。
“agentid”:”1000002″,    #企业号中的应用id。
“text”:{
“content”:subject + ‘n’ + content
},
“safe”:”0″
}
#    send_data = json.dumps(send_values, ensure_ascii=False)
send_data = simplejson.dumps(send_values, ensure_ascii=False).encode(‘utf-8’)
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 =  ‘ww0c838086fde336f4’   #CorpID是企业号的标识
corpsecret = ‘ZTazP-sue-MfKRrdiK2hzKCsp2bxc7RBKnpWI0k1mM0’  #corpsecretSecret是管理组凭证密钥
accesstoken = gettoken(corpid,corpsecret)
senddata(accesstoken,user,subject,content)

四、测试脚本
# ./wechat.py ZhongMingJia www 123jfkldsj123
五、创建media
Name:wechat
Type:script
Script name:wechat.py
Script parameters
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
六创建用户组以及用户
Group name:Alert wechat
enable

Alias:weixin
Name:wechatuser
Surname:wechat
Groups:Alert wechat

Type:wechat
Send to:1000002 企业微信发送告警的程序的id
When active:1-7,00:00-24:00 接受告警的时间
Use if severity:告警的那些级别全选吧,实际根据需要

Not classified
Information
Warning
Average
High
Disaster

Enabled

User type:super admin

七、创建一个host group——->template——>host—–>iterm —–>trigger —-> action(这个估计也只有我自己才能看得懂了,不过您可以问我)

Group:
Group name:test wechat

template:
template:Template name
Visible name:…
Groups In groups:test wechat
Hosts / templates In:test wechat

host:
Host name:118.88.0.0
Visible name:test wechat
Groups In groups:test wechat
SNMP interfaces : 118.88.0.0 ip 161

Linked templates Name:test wechat1

{$SNMP_COMMUNITY} xxxxxxxxx

iterm:
Name:Interface Vlan-interface1000(Vlan-interface1000 Interface): Operational status
Type:snmpv2
Key:net.if.status[ifOperStatus.11]
Host interface:118.88.0.0:161
SNMP OID:1.3.6.1.2.1.2.2.1.8.11
SNMP community:{$SNMP_COMMUNITY}

Port
Type of information
Units
Update interval
Custom intervals
Type Interval Period Action

FlexibleScheduling
History storage period
Trend storage period
Show value: IF-MIB:ifOperStatus
Applications:test wechat
trigger:
Name:Interface Vlan-interface1000(Vlan-interface1000 Interface): Link down
Severity:Average

Problem expression:
{118.88.0.0:net.if.status[ifOperStatus.11].last()}=2 and {119.6.103.70:net.if.status[ifOperStatus.11].diff()}=1

OK event generation:Recovery expression

Recovery expression:
{118.88.0.0:net.if.status[ifOperStatus.11].last()}=1

PROBLEM event generation mode:Multiple

OK event closes:All problems

Description:
Last value: {ITEM.LASTVALUE1}.
Interface is down
action:
Name:test wechat and email
Type of calculation:A and B
Conditions
Label Name Action
A Maintenance status not in maintenance
B Host = test wechat

Steps 1-1(0 – infinitely)
Step duration(0 – use action default)
Operation type: send message
Send to User groups:
xxxx

Send to Users:
weixin (wechatuser wechat)

最后

以上就是酷酷钢笔为你收集整理的zabbix wechat告警的全部内容,希望文章能够帮你解决zabbix wechat告警所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部