cacti实现微信告警功能
背景
近来受朋友所托,为他实现cacti的微信告警功能,但是在印象之中,cacti的thold插件貌似只有邮件告警功能吧?于是花了点时间在网络上搜索了相关信息,发现cacti的微信告警的信息着实很少,唯一看似可用的文章尝试了下却无法成功,在那篇文章的基础上,我修改了一下微信脚本的代码,测试成功了。故在此整理一下,供各位参看。
网上参考的原文章:http://www.itnpc.com/news/web/145117534132561.html
原理
由于cacti的thold插件本身并没有微信告警功能,只有邮件功能,所以本文章的微信告警功能其实是有取巧的性质。原理就是在thold插件触发邮件告警的时候,同时运行微信发送消息的脚本,利用微信公众号的api发送告警内容。
配置(cacti目录为/usr/local/cacti)
1:安装cacti的thold与settings插件。
thold依赖setting插件,先安装settings插件:
插件下载地址:http://docs.cacti.net/plugin:settings
1
2
|
# cp settings-v0.71-1.tgz /usr/local/cacti/plugins # tar -zxvf settings-v0.71-1.tgz |
安装thold插件:
插件下载地址:http://docs.cacti.net/plugin:thold
1
2
|
# cp thold-v0.5.0.tgz /usr/local/cacti/plugins # tar -zxvf thold-v0.5.0.tgz |
使插件生效:
1
2
3
4
5
6
7
8
9
10
11
12
|
# vi /usr/local/cacti/include/config.php $database_type = "mysql" ;
$database_default = "cacti" ;
$database_hostname = "localhost" ;
$database_username = "cacti" ;
$database_password = "cacti123456" ;
$database_port = "3306" ;
$database_ssl = false ;
#添加如下信息 $plugins[] = 'settings' ;
$plugins[] = 'thold' ;
|
进入cacti,在console--Configuration--Plugin Management中开启插件即可。
2:配置微信告警触发与微信告警脚本
在thold插件程序中,触发邮件告警的部分,添加运行微信告警的代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# vi /usr/local/cacti/plugins/thold/thold_functions.php $message = str_replace( '<GRAPH>' , "<br><img src='" . $val[' file '] . "' ><br>Could not open !<br>" . $val[ 'file' ], $message);
}
}
}
########找到相关代码位置,添加如下代码__start#####################
$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文件,进行微信报警操作。
#################end#################################
$text = array( 'text' => '' , 'html' => '' );
if ($filename == '' ) {
$message = str_replace( '<br>' , "\n" , $message);
$message = str_replace( '<BR>' , "\n" , $message);
$message = str_replace( '</BR>' , "\n" , $message);
$text[ 'text' ] = strip_tags($message);
|
触发微信消息脚本:(提前申请微信企业号,并配置好相关信息,可参考博文http://icenycmh.blog.51cto.com/4077647/1909527)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# vi /etc/wechat.sh #微信企业号的CropID CropID= 'xxxxxxxxxxxxxxx'
#企业号中发送告警的应用 Secret= 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
GURL= "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$( /usr/bin/curl -s -G $GURL | awk -F "[\":,]" '{print $15}' )
PURL= "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
function body() {
local int AppID=1
#Appid 填写企业号中建立的报警APP的ID local UserID= "@all"
#此处填写报警接收用户,全部报警可留空 local PartyID= "@all"
local TagID= "@all"
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_image.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": "' "$UserID "\"" ,\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'
} curl -l -H "Content-type: application/json" -X POST -d "$(body )" $PURL
|
测试一下微信脚本是否可用:
1
2
3
4
|
#在/tmp/title.txt和/tmp/message.txt中随便填写一些内容,运行微信脚本: # echo 123 > /tmp/title.txt # echo 456 > /tmp/message.txt # sh /etc/wechat.sh |
如果收到微信信息,则微信脚本可用,内容如下:
3:配置cacti邮件告警,并配置主机宕机状态告警:(不配置邮件应该也可以,只是可以更直观的发现触发邮件告警的时候,同时触发了微信告警)
配置邮件相关信息与DNS:
模拟主机宕机,查看告警:
5:cacti流量监控告警配置
创建一个告警模板,规定网口进入流量超过30M就触发告警。(具体流量监控配置不在这里阐述)
把这个模板应用到某个主机端口上,如果流畅超过30M,则触发告警,告警如下:
可以看到,不止能收到文本告警,就连当下接口的流量图都可以收到,说明这个微信告警的功能还是挺强大滴~~~
注意:
如果流量告警中无法显示流量图,可以做如下修改:
1
2
3
4
5
|
#取消cacti的图像验证配置 # vi /usr/local/cacti/graph_image.php #include("./include/auth.php"); #添加注释 include_once( "./lib/rrd.php" );
include( "./include/global.php" ); #添加这一行
|