原理
由于 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(‘
Could not open!
” . $val[‘file’], $message);
}
}
}
########找到相关程式码位置,新增如下程式码__start#####################
$msg_wx = strip_tags(str_replace(‘
‘, “n”, $message)); //删除 message 中的 html 标签, 并将
替换为换行符 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(‘
‘, “n”, $message);
$message = str_replace(‘
‘, “n”, $message);
$message = str_replace(‘‘, “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 “[“: