看板 MIS 關於我們 聯絡資訊
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可以應用在很多方面,大家可以多多研究. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.218.17.180 ※ 文章網址: https://www.ptt.cc/bbs/MIS/M.1458223411.A.A9E.html ※ 編輯: xxoo1122 (49.218.17.180), 03/17/2016 22:05:07
mick72: 酷喔,目前還沒用到,先給你一個讚! 03/17 22:15
Forgotsome: 讚!我老闆現在一直凹我做簡訊通知又不想花錢 這招好! 03/17 22:42
chang0206: 水啦! 03/18 09:19
leaderliu: 好猛喔 而且分享這個超級有心 推推推 03/18 10:31
bojack: 有分享有推,很用心又實用的教學 ! 03/18 14:44
lusaka: 感謝大大的分享 03/20 06:03
jashking: 嘗試中,但是我是B 沒有thold目錄 也沒找到Thold_functi 03/21 18:46
jashking: on這個PHP檔案@@ 03/21 18:46
jashking: cacti 0.8.8b...數字鍵盤沒開 03/21 18:47
xxoo1122: oh,你應該沒裝thold 這個外掛 03/21 23:55
xxoo1122: http://goo.gl/5Ekja4 03/21 23:56
jashking: 哦 哈哈 感謝!! 我以為CACTI內建自動了..XD 非常謝謝 03/22 01:28