zabbix问端设置:
1,设置报警类型
2、设置用户及媒体类型
send_to 就是zabbix输出的$1,脚本中药发给谁
3、设置触发器和动作
触发器 test.temp_conn (自己写一个简单的key做测试)
4、设置触发器,阈值为最后一次获取的值大于1
5、设置动作
关联触发器
2、服务器端配置
vim zabbix.server.conf. 修改脚本位置
AlertScriptsPath=/opt/svr/zabbix2.4.5/share/zabbix/alertscripts/weixin_media/bin/
设置web端的key
UserParameter=test.temp_conn,ls -l /tmp/test | grep '^-' | wc -l
3、shell脚本
zabbix 2.4中建议使用shell脚本发送信息,因为2.4中不能自定义发送信息的格式
CropID="wx5a109e09d5044152" Secret="0fiTLHoU0SBtNcBm4huzPILDt0QxjhdsVcnNBPVw9lVCR3vGVf7wf2p4HDTiilaQ" GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F" '{print $4}') PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken" function body() { local int AppID=1 #企业号中的应用id local UserID=$1 #部门成员id,zabbix中定义的微信接收者 # local PartyID=2 #部门id,定义了范围,组内成员都可接收到消息 local Msg=$(echo "$@" | cut -d" " -f3-) #过滤出zabbix传递的第三个参数 printf '{ ' printf ' "touser":"'"$UserID""", " # printf ' "toparty":"'"$PartyID""", " printf ' "msgtype":"text", ' printf ' "agentid":"'" $AppID """, " printf ' "text":{ ' printf ' "content":"'"$Msg""" " printf ' }, ' printf ' "safe":"0" ' printf '} ' } /usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
4、python脚本
#! /usr/bin/env python3 import requests import json import logging import sys, os print(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # print(sys.path) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) print(BASE_DIR) class MyLogger: def __init__(self, msg): self.logger = self.initlog() self.msg = msg def initlog(self): logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) fh = logging.FileHandler(BASE_DIR + '/log/' + 'char.log') fh.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) logger.addHandler(fh) return logger def savelog(self): self.logger.debug(self.msg) class ZabbixSendMail(): def __init__(self,sys_argv): self.argv = sys_argv self.url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" self.value = { 'corpid':"wx5a109e09d5044152", 'corpsecret' : "0fiTLHoU0SBtNcBm4huzPILDt0QxjhdsVcnNBPVw9lVCR3vGVf7wf2p4HDTiilaQ" } self.sendmail_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send" self.access_resprons = requests.get(self.url, params=self.value) # self.token = json.loads(self.access_resprons.text)['access_token'] self.respons = self.access_resprons.text #获取返回结果 self.log = MyLogger("action") self.log.savelog() def get_token(self): ret = json.loads(self.respons) return ret["access_token"] def send_mail(self): try: send_mail_url = self.sendmail_url + "?access_token=%s"% self.get_token() #print(send_mail_url) msg = "".join(self.argv[2:]) payload = """{"touser": "%s", "msgtype": "text", "agentid": 1, "text":{"content": "内容:%s"},"safe":0}""" % (self.argv[0], msg) dic_payload = eval(payload) # ret = requests.post(send_mail_url, data=json.dumps(payload, ensure_ascii=False)) ret = requests.post(send_mail_url, data=json.dumps(dic_payload, ensure_ascii=False).encode('UTF-8')) #发送中文时就需要ensure_ascii与.encode('utf-8')这两个参数配合才可以否则会报错 log = MyLogger(ret.text) log.savelog() except Exception as e: # print(e) log = MyLogger(e) log.savelog() if __name__ == "__main__": send = ZabbixSendMail(sys.argv[1:]) send.send_mail()