设为首页 收藏本站
查看: 1111|回复: 0

[经验分享] zabbix通过微信企业号发送图文消息

[复制链接]

尚未签到

发表于 2019-1-19 07:57:49 | 显示全部楼层 |阅读模式
  我有篇博客写到如何用微信发送告警消息,实现了发送文字消息,不能带图片,这样不是很直观,最近又研究了一下如何发送图片,写了脚本实现了发送文字+图片的告警。

  效果如下:


  先发送文字消息,下面挨着graph。
  这里只提供脚本和思路,具体配置请看我的另一篇博客:(http://wuhf2015.blog.运维网.com/8213008/1688614#662543)
  

  实现方式:


  •   在Action中设置Default Subject的格式为"状态:#{TRIGGER.STATUS}#主机:#{HOST.NAME}#键名:#{ITEM.KEY}#"。这样可以在脚本里做判断,如果状态为OK则不发送图片,如果状态为problem则发送图片。

  •   脚本有了{HOST.NAME}和{ITEM.KEY}这两个参数后,可以通过查询Mysql或者调用zabbix_api的方式得到我们必要的变量ItemID,有了这个变量才能获取图片。
  •   通过itemid从zabbix中获取图片后,我们需要将图片上传到微信企业号的临时素材里,上传后我们会得到一个media_id
  •   我们将media_id通过image格式发送出来就能收到图片消息了。
      


  脚本:

#!/bin/bash
#SCRIPT_NAME:weixin.sh
#get graph to you
#V2-2016-05-23
#wuhf
#email:whf2chen@126.com
gettoken() {
ID='xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
SECRET='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
URL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$ID&corpsecret=$SECRET"
Gtoken=$(/usr/bin/curl -s -G $URL | awk -F\" '{print $4}')
}
gettoken
AppID=3
PartyID=2
UserID="$1"
Title="$2"
Msg="$3"
getitemid() {
DBServer="127.0.0.1"
DBUser="root"
DBName="zabbix"
DBPort="3306"
DBPassword="123456"
Hostname=$(echo $Title |awk -F"#" '{print $4}')
Key=$(echo $Title |awk -F"#" '{print $6}')
Return=$(sudo mysql -u$DBUser -h$DBServer -P$DBPort -p$DBPassword --database=$DBName -e "select itemid from items where hostid=(select hostid from hosts where name = \"$Hostname\" ) and key_ = \"$Key\";")
Itemid=$(echo $Return |awk '{print $2}')
}
#获取itemid可以通过数据库查询,也可以通过zabbix_api,我自己用的是api
#getitemid
getgraph() {
zabbix_user='xxxxxxxxxxxxxxxxxx'
zabbix_pass='xxxxxxxxxxxxxxxxxx'
zabbix_url="http://127.0.0.1/zabbix/"
cookie="/tmp/cookie"
image_path="/tmp/"
STime=$(date +%Y%m%d%H%M%S)
Period=7200
Width=640
High=200
sudo /usr/bin/curl -s -c $cookie -b $cookie -d "name=$zabbix_user&password=$zabbix_pass&autologin=1&enter=Sign+in" $zabbix_url/index.php
sudo /usr/bin/curl -s -b $cookie -F "itemids=$Itemid" -F "period=$Period" -F "stime=$STime" -F "width=$Width" -F "high=$High" $zabbix_url/chart.php > $image_path$Itemid.png
}
#定义这个函数是要利用上面的itemid获取图片保存到/tmp下面
#getgraph
postgraph() {
PIC_URL="$image_path$Itemid.png"
M_URL="https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=$Gtoken&type=image"
Media=$(sudo curl -s -F media=@"$PIC_URL" "$M_URL" | awk -F"\"" '{print $8}')
}
#定义这个函数是要将/tmp下面的png文件上传到微信临时素材接口,永久素材接口有上限5000,所以不建议使用
#postgraph
#debug
#echo $PIC_URL$Media > /tmp/pic.txt
image() {
    printf '{\n'
    printf '\t"touser": "'"$UserID"\"",\n"
    printf '\t"toparty": "'"$PartyID"\"",\n"
    printf '\t"msgtype": "image",\n'
    printf '\t"agentid": "'" $AppID "\"",\n"
    printf '\t"image": {\n'
    printf '\t\t"media_id": "'"$Media"\""\n"
    printf '\t},\n'
    printf '\t"safe":"0"\n'
    printf '}\n'
}
text() {
    printf '{\n'
    printf '\t"touser": "'"$UserID"\"",\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'
}
#我这里定义image和text两个格式,是要将一条告警消息分两次发送,先发送text然后发送图片,原因就是微信企业号提供的news发送的为图片链接,如果zabbix是内网监听,那么链接就无意义了,而mpnews每天最多发送100条,超过就返回错误,所以放弃。
url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
if echo $Title | grep "PROBLEM" ;then
getitemid
getgraph
postgraph
sudo /usr/bin/curl --data-ascii "$(text)" $url
sudo /usr/bin/curl --data-ascii "$(image)" $url
sudo rm -f $PIC_URL
else
sudo /usr/bin/curl --data-ascii "$(text)" $url
fi  互相学习:
  脚本参照了下面这两篇博客:


  •   http://babyfenei.blog.运维网.com/443861/1728604
  •   http://grass51.blog.运维网.com/4356355/1094098
  

  注意:
  1.key_ 中不能带引号,例如grpsum{"zabbix server",net.if.in[eth0],last,0}这样就不正确

  2.Action中的Default Subject项一定按照我写的来




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-664977-1-1.html 上篇帖子: Zabbix3.2告警升级机制 下篇帖子: LNMP+Zabbix
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表