zabbix通过微信模板消息发送报警信息给指定的用户
效果如下:
环境:
可发送模板消息的认证号或测试号(无认证号,目前使用测试号) zabbix 2.4.7
微信测试号设置: 登陆测试号,获得appID及appsecret
关注测试号,获得图中右侧用户的微信号
增加消息模板,记录消息模板id 模板内容需填写{{topic.DATA}} {{message.DATA}}
zabbix告警脚本部署
三个脚本: 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的文件的权限 脚本文件请见文章最后
zabbix配置:
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
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