Zabbbix 微信报警

生产环境中,我们通常使用邮件和短信接受zabbix报警信息,但是邮件经常被工作人员搁置在角落中甚至被设置为垃圾邮件被过滤掉。公司的短信接口又太贵,复杂环境中使用短息报警会使运维成本增加很多。微信 提供了很好的第三方接口,我们可以利用 微信报警以求降低运维成本。此文章是我看到网上的翻译及添加了我在生产环境中使用的问题

微信的第三方接口要求我们先申请一个企业号——传送门https://qy.weixin.qq.com/

关于企业号的申请可以参考以下链接我就在这里多说了http://jingyan.baidu.com/article/6525d4b1210921ac7d2e941b.html

如何操作企业号?

1.通讯录添加企业成员

       我们要提前把成员信息添加进组织部门,必填项+手机号或者微信号,这样别人扫描二维码的时候才能成功关注企业号。

      注意:这里有两个我们要用到信息,一个组织部门的ID,一个部门成员的账号(账号是自己手动指定的,不同于微信号,最好是字母加数字) 在这里我们只需要用到成员账号。

                            spacer.gif650) this.width=650;” width=”553″ height=”185″ title=”1.png” style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);width:598px;height:189px;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/97283523791476752.png” class=”aligncenter”>

spacer.gif650) this.width=650;” width=”600″ height=”602″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

2.应用中心创建应用

       我们要在这里创建应用,因为要通过应用发送消息给部门成员

      注意:这里要记住一个值,应用ID  如果要发给那个部门需要对那个部门可见

spacer.gif650) this.width=650;” width=”554″ height=”172″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

spacer.gif650) this.width=650;” width=”553″ height=”445″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

3.给部门设置管理员

spacer.gif650) this.width=650;” width=”554″ height=”214″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

     设置—>功能设置—->权限管理—->新建管理组

     管理员必须事先已经关注了企业号,并且已经设置好邮箱地址

spacer.gif650) this.width=650;” width=”559″ height=”482″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

     确定管理员可以读取通讯录,可以使用应用发消息。

     注意:我们需要管理员的CorpID和Secret

spacer.gif650) this.width=650;” width=”553″ height=”231″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

我们要准备这些东西:

  •     一个微信企业号

  •     企业号已经被部门成员关注

  •     企业号里有一个可以发消息的应用

  •     一个授权管理员,可以使用该应用给成员发消息

我们要取到这些信息:

  •     成员账号

  •     组织部门ID

  •     应用ID

  •     CropID

  •     Secret

 


 

如何调用微信接口?

    调用微信接口需要一个调用接口的凭证:access_token

    通过 :CropID 、Secret  才能获取到access_token,但是获取到的token有效期为两分钟

 

spacer.gif650) this.width=650;” width=”649″ height=”441″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

spacer.gif650) this.width=650;” width=”650″ height=”436″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

    微信企业号接口调试工具传送门:http://qydev.weixin.qq.com/debug


 

Shell脚本原理

    使用:

     curl -s -G  url      获取 AccessToken

    使用:

     curl –data  url   传送凭证调用企业号接口

     zabbix会传递三个参数给脚本,$1是消息接收账号,$2报警标题,$3报警内容

 

#!/bin/bash

CropID=‘xxxxxx‘

Secret=‘xxxxxx‘

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=2                        企业号中的应用id

        local UserID=$1                          部门成员idzabbix中定义的微信接收者

        #local PartyID=1                          部门id,定义了范围组内成员都可接收到消息,选择禁用不然会所有人都会收到消息的

        local Msg=$(echo “$@” | cut -d” ” -f3-)  过滤出zabbix中传递的第三个参数

        printf ‘{\n‘

        printf ‘\t”touser”: “‘”$User”\””,\n”

        printf ‘\t”toparty”: “‘”$PartyID”\””,\n”

        printf ‘\t”msgtype”: “text”,\n‘

        printf ‘\t”agentid”: “‘” $AppID “\””,\n”

        printf ‘\t”text”: {\n‘

        printf ‘\t\t”content”: “‘”$Msg”\””\n”

        printf ‘\t},\n‘

        printf ‘\t”safe”:”0″\n‘

        printf ‘}\n‘

}

/usr/bin/curl –data-ascii “$(body $1 $2 $3)” $PURL

为什么要这样写脚本?

   因为微信企业号开放的端口有固定的格式限制

   企业号支持的格式:http://qydev.weixin.qq.com/wiki/index.php?title=消息类型及数据格式

 

spacer.gif650) this.width=650;” width=”650″ height=”476″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

将脚本放入zabbix默认执行路径下

mv wechat.sh /usr/local/zabbix/share/zabbix/alertscripts    ##Zabbix 默认脚步路径

chown zabbix.zabbix /usr/local/zabbix/share/zabbix/alertscripts/wechat.sh

chmod +x /usr/local/zabbix/share/zabbix/alertscripts/wechat.sh

测试脚步是否可用

./Wechat.sh wang415 111 wang     # 第一个wang415 是你要发的人,第二个111 是可以随意写, wang 是你要发个测试消息


 

    服务器端

  1. 创建媒介

spacer.gif650) this.width=650;” width=”553″ height=”178″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

Script name 就是的脚步名字

     2.创建用户添加WeChat 组

spacer.gif650) this.width=650;” width=”553″ height=”333″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

创建Media 选择wechat 脚本发给wang415

spacer.gif650) this.width=650;” width=”417″ height=”260″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

spacer.gif650) this.width=650;” width=”553″ height=”49″ style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4463071004201618543.gif” class=”aligncenter”>

  1. 创建触发器动作发送内容复制以下消息到Default message 内

 \

IP: {IPADDRESS}\n

Host Name: {HOST.NAME1}\n

Status: {TRIGGER.STATUS}\n

Severity: {TRIGGER.SEVERITY}\n

Date: {EVENT.DATE} {EVENT.TIME}\n

\n

{ITEM.NAME1} ({ITEM.KEY1}): {ITEM.VALUE1}\n

\n

spacer.gif650) this.width=650;” width=”553″ height=”257″ title=”15.png” style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/2780011445787580172.png” class=”aligncenter”>     4.设置触发器动作发送方式给Wechat组spacer.gif650) this.width=650;” width=”554″ height=”95″ title=”16.png” style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/5477703271161835435.png” class=”aligncenter”> 


 

spacer.gif650) this.width=650;” width=”554″ height=”296″ title=”17.png” style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/6015960098448196486.png” class=”aligncenter”>

收到的微信消息:

spacer.gif650) this.width=650;” width=”554″ height=”385″ title=”19.png” style=”background:url("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid rgb(221,221,221);” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/4432095196575250369.png” class=”aligncenter”>

 

 

本文出自 “11628678” 博客,请务必保留此出处http://11638678.blog.51cto.com/11628678/1786674