闲来无事干,玩玩zabbix
zabbix添加企业微信报警
说下:这个必须下载企业微信app,或者企业微信桌面客户端才可以接收消息
使用到了这个大佬的发消息代码: https://github.com/X-Mars/Zabbix-Alert-WeChat
一、申请一个企业微信号
这个公司名字不行额,我重新编了个:白鹿网游有限公司,可以了

二、创建应用
最后生成的agentid和secret后面要用



三、发个消息测试下



四、查看企业ID

五、添加运维部门,开发部门,测试部门
这样待会儿给每个部门成员发消息,如果不需要给各部门发消息,而是只需要给zabbix用户发消息,那就不用配置这些


六、准备发送报警的脚本
微信官方api文档:https://work.weixin.qq.com/api/doc#90000/90003/90487
参考:https://www.v2ex.com/t/499075
#查看zabbix默认脚本路径
[root@d685a1603b4d zabbix]# grep AlertScriptsPath /etc/zabbix/zabbix_server.conf | grep -v ^#
AlertScriptsPath=/usr/lib/zabbix/alertscripts
#克隆代码到这个目录中
[root@logstash opt]# git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.git
[root@logstash opt]# cd Zabbix-Alert-WeChat/
[root@logstash Zabbix-Alert-WeChat]# docker cp wechat.py zabbix-server:/usr/lib/zabbix/alertscripts/
#因为zabbix-server装在docker中,因此安装费了点事
curl -Lo /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install python2-pip
pip install requests
pip install --upgrade requests
代码详情
CorpID:见第四步
Agentid:见第一步
Secret:见第一步
#!/usr/bin/python2.7
#_*_coding:utf-8 _*_
#auther:火星小刘
import requests,sys,json
import urllib3
urllib3.disable_warnings()
reload(sys)
sys.setdefaultencoding('utf-8')
def GetTokenFromServer(Corpid,Secret):
Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
Data = {
"corpid":Corpid,
"corpsecret":Secret
}
r = requests.get(url=Url,params=Data,verify=False)
print(r.json())
if r.json()['errcode'] != 0:
return False
else:
Token = r.json()['access_token']
file = open('/tmp/zabbix_wechat_config.json', 'w')
file.write(r.text)
file.close()
return Token
def SendMessage(User,Agentid,Subject,Content):
try:
file = open('/tmp/zabbix_wechat_config.json', 'r')
Token = json.load(file)['access_token']
file.close()
except:
Token = GetTokenFromServer(Corpid, Secret)
n = 0
Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
Data = {
"touser": User, # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
#"totag": Tagid, # 企业号中的标签id,群发使用(推荐)
#"toparty": Partyid, # 企业号中的部门id,群发时使用。
"msgtype": "text", # 消息类型。
"agentid": Agentid, # 企业号中的应用id。
"text": {
"content": Subject + '\n' + Content
},
"safe": "0"
}
r = requests.post(url=Url,data=json.dumps(Data),verify=False)
while r.json()['errcode'] != 0 and n < 4:
n+=1
Token = GetTokenFromServer(Corpid, Secret)
if Token:
Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
r = requests.post(url=Url,data=json.dumps(Data),verify=False)
print(r.json())
return r.json()
if __name__ == '__main__':
User = sys.argv[1] # zabbix传过来的第一个参数
Subject = str(sys.argv[2]) # zabbix传过来的第二个参数
Content = str(sys.argv[3]) # zabbix传过来的第三个参数
Corpid = "wxaf" # CorpID是企业号的标识
Secret = "aKDdCRT76" # Secret是管理组凭证密钥
#Tagid = "1" # 通讯录标签ID
Agentid = "1000001" # 应用ID
#Partyid = "1" # 部门ID
Status = SendMessage(User,Agentid,Subject,Content)
print Status
如果只需要给用户发送而不需要给指定部门发送的话只需要配置Corpid,Secret,Agentid三项即可
"touser": User, # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
#"totag": Tagid, # 企业号中的标签id,群发使用(推荐)
#"toparty": Partyid, # 企业号中的部门id,群发时使用。
"msgtype": "text", # 消息类型。
"agentid": Agentid, # 企业号中的应用id。
Corpid = "wxxxxxxxxxxxxe" # CorpID是企业号的标识
Secret = "ozzuxxxxxxxxxxxxxx1nk" # Secret是管理组凭证密钥
#Tagid = "1" # 通讯录标签ID
Agentid = "1000002" # 应用ID
#Partyid = "1" # 部门ID
七、测试能否发送消息
脚本中传递了三个参数,第一个是要发送给的用户,第二个是邮件标题,第三个是报警内容
[root@d685a1603b4d alertscripts]# python wechat.py Qxxxxxxxi "subject" "hello,world"
{u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}

八、zabbix web界面上配置报警媒介
脚本参数这样填:
- {ALERT.SENDTO}
- {ALERT.SUBJECT}
- {ALERT.MESSAGE}

测试发送下:


九、给每个用户添加报警媒介

十、上面忘了添加动作,补上



十一、效果

补充:
最后给/tmp目录设置acl权限,给zabbix加上权限,因为这个脚本要在/tmp目录下面生成临时文件
setfacl -m u:zabbix:rwx /tmp