HI 大家好,去年有写过一篇Cacti如何结合Line来发告警讯息,
但是那个API毕竟不是官方的所以后来就被封了只好另找出路
,今天来介绍结合Hipchat来发告警讯息.
1.先申请hipchat帐号,申请方法非常简单这边就不赘述
http://imgur.com/Jrbjvz9
2.申请完后登入可以依照你的作业系统下载client
http://imgur.com/fPAXuxt
3.使用client登入后,先create new room
http://imgur.com/CaiuwtP
4.再来我们要产生access token,回到网页点选account settings
http://imgur.com/CZhEIGy
5.再点选API access
http://imgur.com/881OJ7d
6.赋予这个token权限,为了测试方便我先全选
http://imgur.com/n6WlFGj
7.取得token后,打开浏览器输入
https://api.hipchat.com/v2/room?auth_token=你刚取得的token
或是使用curl https://api.hipchat.com/v2/room?auth_token=你刚取得的token
这时候你就能看到你刚刚建立的room id
8.再来我们到cacti的目录(我这边是/usr/share/cacti/plugins/thold)
下vi一个hipchat.php,code如下,$room_id,$auth_token填刚刚产生的
,$data这边我说明一下,color可以改成yellow, green, red, purple, gray
任选一个默认是黄色,重点来了,在message这边一定要@在这个room里面的成员
不然hipchat ios or android app不会推送通知.
ps.这边有用到json_encode,所以要装php-pecl-json
ex:yum install php-pecl-json
记得要restart httpd or php-fpm看你用什么环境
下面的code碍于ptt的篇幅所以有断行,请在自行接回去
参考文件
https://www.hipchat.com/docs/apiv2/method/send_room_notification
<?php
$room_id = 'xxxxx';
$auth_token = 'xxxxxxxxxxxxxxxxx';
$data = array("color" => "red","notify" => "true", "message_format" => "text"
, "message" => "@连胜文 @朱立伦 $argv[1]");
$data_string = json_encode($data);
$url = "https://api.hipchat.com/v2/room/{$room_id}/notification?auth_token=
{$auth_token}";
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($c, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
echo curl_exec($c);
?>
9.再来修改thold_functions.php这只程式,找到
function thold_mail($to, $from, $subject, $message, $filename, $headers = '')
移动到function 的最尾端可以看到return ''; ,在return '';前加入
$sms = shell_exec('php /usr/share/cacti/plugins/thold/hipchat.php '
.escapeshellarg($message).'');
完成图 电脑
http://i.imgur.com/H0PESyY.png
完成图 手机
http://i.imgur.com/MWmetB4.jpg
Hipchat可以应用在很多方面,大家可以多多研究.