首先申请企业号获取对应应用中的CropID和Secret以及应用ID
这部分后面再进行添加
也可参考
http://wuhf2015.blog.51cto.com/8213008/1688614
中的内容进行设置
注:以前的教程中发现不能触发主机宕机报警,所以进行修改。目前已能实现主机宕机和thold的图文报警。并已解决乱码问题
1、将cacti中thold的报警内容导出至文本文件进行保存
进入cacti主目录
vi plugins/thold/thold_functions.php
查找$text。在如下位置插入后面的的语句
$msg_wx = strip_tags(str_replace('<br>', "\n", $message)); //删除message中的html标签,并将<br>替换为换行符\n $msg_wx = trim($msg_wx); //整理msg_wx字符串 $msg_wx = iconv( "GB2312//IGNORE", "UTF-8", $msg_wx); //转换编码为utf-8,防止乱码 $sub_wx = iconv( "GB2312//IGNORE", "UTF-8", $subject); //转换编码为utf-8,防止乱码 $file_title = '/tmp/title.txt'; //要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个 $file_message = '/tmp/message.txt'; //要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个 if($f = file_put_contents($file_title, $sub_wx)) //将subject参数的值保存到file_title中 if($f = file_put_contents($file_message, $msg_wx)) //将msg_wx参数的值保存到file_message中 shell_exec("/etc/wechat.sh"); //运行wechat.sh文件,进行微信报警操作。 //By:Fenei QQ:407603129 转载请注明出处 http://babyfenei.blog.51cto.com/443861/1728604
插入位置如图所示
2、创建微信发送脚本
vi /etc/wechat.sh chmod 750 /etc/wechat.sh
输入如下内容
#微信企业号发送消息脚本 #By:Fenei QQ:407603129 #2016年4月19日 #转载请注明出处 #http://babyfenei.blog.51cto.com/443861/1728604 #!/bin/bash CropID='企业号ID' Secret='企业号密钥' 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 #Appid 填写企业号中建立的报警APP的ID local UserID=用户ID #此处填写报警接收用户,全部报警可留空 local PartyID=1 local TagID=2 Ent=$'\n' Date=$(date '+%Y年%m月%d日 %H:%M:%S\n\n') #应cactifans群内要求,添加Cacti微信报警日期参数 Tit=$(cat /tmp/title.txt) #读取/tmp/title文件中内容到变量Tit Msg=$Date$Tit$Ent$(cat /tmp/message.txt|sed 's/%//g') #拼接msg主体文件,包含日期,主题,报警内容.并删除报警内容中的'%'号. Url=$(grep "http" /tmp/message.txt|sed 's/URL: //g') #获取message.txt中的url行内容 Pic_tmp=$(grep "http" /tmp/message.txt|sed 's/URL: //g'|sed 's/\/graph.php/\/graph_p_w_picpath.php/g') if [ ! -n "$Pic_tmp" ] ;then Pic="" else Pic=$Pic_tmp$'&graph_height=130&graph_width=500&t='$Date fi #根据url行内容修改为添加Pic参数 #另感谢cactifans群 深圳-动动 提供的方法,在此修改图片大小,防止图像显示不全,并判断图片文件是否存在。防止宕机出现图片报警 printf '{\n' printf '\t"touser": "'"$User"\"",\n" printf '\t"toparty": "'"$PartyID"\"",\n" printf '\t"totag": "'"$TagID"\"",\n" printf '\t"msgtype": "news",\n' printf '\t"agentid": "'" $AppID "\"",\n" printf '\t"news": {\n' printf '\t"articles": [\n' printf '{\n' printf '\t\t"title": "'"$Tit"\","\n" printf '\t\t"description": "'"$Msg"\","\n" printf '\t\t"url": "'"$Url"\","\n" printf '\t\t"picurl": "'"$Pic"\","\n" printf '\t}\n' printf '\t]\n' printf '\t}\n' printf '}\n' } /usr/bin/curl --data-ascii "$(body )" $PURL
微信脚本创建完成以后,请运行 /etc/wechat.sh 看下企业号是否能收到空白消息。
3 修改graph_p_w_picpath.php文件,取消graph_p_w_picpath.php的认证。
否则微信无法正常显示图片
#include("./include/auth.php"); include_once("./lib/rrd.php"); include("./include/global.php");
4 有朋友反映说到这步以后微信没有报警。说明一下,我这个方法是通过命令抓取邮件报警的标题和内容进行报警的。所以需要在创建阀值的时候添加报警邮箱,并配置好邮件服务器。在测试阀值触发以后邮箱是否能收到报警邮件。如果没有报警邮件的话。微信报警也是不生效的。
以下是微信报警截图
转载于:https://blog.51cto.com/babyfenei/1728604