效果以下:api
環境:bash
可發送模板消息的認證號或測試號(無認證號,目前使用測試號) zabbix 2.4.7微信
微信測試號設置: 登錄測試號,得到appID及appsecretapp
關注測試號,得到圖中右側用戶的微信號curl
增長消息模板,記錄消息模板id 模板內容需填寫{{topic.DATA}} {{message.DATA}}測試
zabbix告警腳本部署url
三個腳本: weixin_send_template_message.sh #發送模板消息腳本 weixin_env.sh #配置文件 weixin_gen_tooken.sh #從新獲取tooken,當tooken過時時間到期或發送模板消息返回tooken過時時將會被weixin_send_template_message.sh調用日誌
weixin_send_template_message.sh需軟連接至zabbix的alterscripts目錄下,需注意三個腳本及存放tooken的文件的權限 腳本文件請見文章最後code
zabbix配置:token
zabbix中添加報警媒介類型
爲用戶添加報警媒介,將關注測試號後測試號頁面顯示出的微信號填入
建立動做,須要注意的是動做的默認消息必須爲一行,如需分行使用\n分行
最後,查看是否能夠收到報警吧。
本人文件目錄:
/data/scripts/weixin_env.sh #微信測試號的配置
#!/bin/bash APPID="xxxxxxxxxx" #測試號的appID APPSECRET="xxxxxxxxxxxxxxxxxxxx" #測試號的appsecret tmp_wx_tooken="/tmp/wx_tooken.txt" #存放tooken的文件,需注意文件的權限 wx_lock="/tmp/wx.lock" now=`date` timestamp=`date +%s` logfile="/tmp/zabbix_send_wx_message.log" #日誌文件位置 openid=$1 topic=$2 message=$3 templateid="xxxxxxxxxxxfDnzplqlgJYSyVb9Xnu8ALnLRVJ3g" #模板ID gettookenscript="/data/scripts/weixin_gen_tooken.sh" #獲取tooken的腳本路徑
/data/scripts/weixin_gen_tooken.sh #獲取tooken的腳本路徑,用於tooken過時時從新獲取tooken
#!/bin/bash envfile="/data/scripts/weixin_env.sh" #微信測試號的配置文件 source $envfile gettookenurlcontent=`curl "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET"` echo "$now get tooken:$gettookenurlcontent">>$logfile echo "$gettookenurlcontent"|grep '{"access_token":' if [ "$?" = "0" ];then echo "$now get wx tooken successful message:$gettookenurlcontent" >>$logfile new_tooken=`echo -e $gettookenurlcontent|awk -F'"' '{print $4}'` echo $new_tooken expire_in=`echo -e $gettookenurlcontent|awk -F'"' '{print $7}'|sed 's/://'|sed 's/}//'` let "new_expire=timestamp + expire_in" echo "$new_tooken $new_expire">$tmp_wx_tooken || echo "$now write tooken to $tmp_wx_tooken filed" >>$logfile line=`cat $tmp_wx_tooken` wxtooken=`echo -e $line|awk -F" " '{print $1}'` echo $wxtooken wxexpire=`echo -e $line|awk -F" " '{print $2}'` echo $wxexpire else echo "$now get wx tooken failed,message:$gettookenurlcontent" >>$logfile exit 1 fi
/data/scripts/weixin_send_template_message.sh #發送模板消息腳本
#!/bin/bash envfile="/data/scripts/weixin_env.sh" #配置文件路徑 source $envfile #檢查tooken文件或tooken是否超過過時時間 check_tooken(){ if [ -f $tmp_wx_tooken ];then line=`cat $tmp_wx_tooken` wxtooken=`echo -e $line|awk -F" " '{print $1}'` wxexpire=`echo -e $line|awk -F" " '{print $2}'` let "wxexpire=wxexpire + 10" isexpire=`echo "$timestamp<$wxexpire"|bc` if [ $isexpire = "0" ];then echo "$now tooken expired,get new tooken">>$logfile echo "$now sh $gettookenscript">>$logfile flock -xn $wx_lock -c "sh $gettookenscript" fi else flock -xn $wx_lock -c "sh $gettookenscript" fi } #發送模板消息,若是返回42001則調用腳本從新獲取tooken後再次發送消息 send_message(){ line=`cat $tmp_wx_tooken` wxtooken=`echo -e $line|awk -F" " '{print $1}'` # sendcontent=`curl -d '{"touser":"'$openid'","template_id":"'$templateid'","topcolor":"#FF0000","data":{"topic":{"value":"'$topic'","color":"#173177"},"message":{"value":"'$message'","color":"#173177"}}}' "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$wxtooken"` sendcontent=`curl -d """{\"touser\":\"$openid\",\"template_id\":\"$templateid\",\"topcolor\":\"#FF0000\",\"data\":{\"topic\":{\"value\":\"$topic\",\"color\":\"#173177\"},\"message\":{\"value\":\"$message\",\"color\":\"#173177\"}}}""" "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$wxtooken"` echo $sendcontent |grep '{"errcode":0,"errmsg":"ok"' if [ $? = "0" ];then echo "$now send message ok,message is topic:$topic,message:$message ,reponse:$sendcontent" >>$logfile exit 0 else echo $sendcontent |grep '{"errcode":42001,"errmsg":"access_token expired' if [ $? = "0" ];then flock -xn $wx_lock -c "sh $gettookenscript" if [ $? = "0" ];then line=`cat $tmp_wx_tooken` wxtooken=`echo -e $line|awk -F" " '{print $1}'` wxexpire=`echo -e $line|awk -F" " '{print $2}'` echo "$now try send message again">>$logfile sendcontent=`curl -d """{\"touser\":\"$openid\",\"template_id\":\"$templateid\",\"topcolor\":\"#FF0000\",\"data\":{\"topic\":{\"value\":\"$topic\",\"color\":\"#173177\"},\"message\":{\"value\":\"$message\",\"color\":\"#173177\"}}}""" "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$wxtooken"` if [ $? = "0" ];then echo "$now send message ok,message is topic:$topic,message:$message ,reponse:$sendcontent" >>$logfile exit 0 else echo $sendcontent |grep '{"errcode":42001,"errmsg":"access_token expired' exit 1 fi fi fi echo "$now send message failed,message is topic:$topic,message:$message ,reponse:$sendcontent" >>$logfile exit 1 fi } check_tooken send_message