[心得] Cacti+Hipchat告警

楼主: xxoo1122 (一个连IE6都能相容的男人)   2016-03-17 22:03:21
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可以应用在很多方面,大家可以多多研究.
作者: mick72 (Mick)   2016-03-17 22:15:00
酷喔,目前还没用到,先给你一个赞!
作者: Forgotsome (遗忘)   2016-03-17 22:42:00
赞!我老板现在一直凹我做短信通知又不想花钱 这招好!
作者: chang0206 (Eric Chang)   2016-03-18 09:19:00
水啦!
作者: leaderliu (雨儿潸然落下)   2016-03-18 10:31:00
好猛喔 而且分享这个超级有心 推推推
作者: bojack (Bojack)   2016-03-18 14:44:00
有分享有推,很用心又实用的教学 !
作者: lusaka (gary.lusifa)   2016-03-20 06:03:00
感谢大大的分享
作者: jashking (拉拉拉)   2016-03-21 18:46:00
尝试中,但是我是B 没有thold目录 也没找到Thold_function这个PHP档案@@cacti 0.8.8b...数字键盘没开
楼主: xxoo1122 (一个连IE6都能相容的男人)   2016-03-21 23:55:00
oh,你应该没装thold 这个外挂http://goo.gl/5Ekja4
作者: jashking (拉拉拉)   2016-03-22 01:28:00
哦 哈哈 感谢!! 我以为CACTI内建自动了..XD 非常谢谢

Links booklink

Contact Us: admin [ a t ] ucptt.com