一、申請好Slack的帳號,在Slack的網站裡面點選進入APP&Custom Integrations ->Build->Make a Custom Integration 新增 Incoming WebHooks,新增想要送的channels,申請完可以取得Webhook URL
二、先在Cacti裡面用Curl指令測試上面產生的Webhook URL來發送訊息到#random頻道做測試
curl -X POST --data-urlencode 'payload={"channel": "#random", "username": "Cacti-Slack-LAB", "text": "This is posted to #alert and comes from a bot named webhookbot.", "icon_emoji": ":bear:"}' https://hooks.slack.com/services/A3DBDQ74Z/A9C2K15GD/cf23G7uCET2Y2FLK9ABiY52X
發送成功的話,在#random頻道裡會收到相關通知訊息
三、Slack是用json的格式來發訊,需要先在Cacti裝一下php-pecl-json
[root@Localhost ~]#yum install -y php-pecl-json
裝完要restart httpd服務
四、在Cacti的tholdhold安裝目錄下建立一支php程式名叫slack.php
[root@Localhost ~]#vi /var/www/html/cacti/plugins/thold/slack.php
PHP程式內容如下,下面紅色的地方可自訂發訊人的名稱與圖示
$data = array("username" => "Cacti-Slack-LAB","icon_emoji" => ":cactus:" ,"text" => "$argv[1]");
$data_string = json_encode($data);
$url = "https://hooks.slack.com/services/A3DBDQ74Z/A9C2K15GD/cf23G7uCET2Y2FLK9ABiY52X";
$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);
五、接著調整修改thold_functions.php這隻程式,找到大約在2836行的附近找到
function thold_mail($to, $from, $subject, $message, $filename, $headers = '')
然後移動到function 的最尾端可以看到return ''; 接著在return '';前加入
$sms = shell_exec('php /usr/share/cacti/plugins/thold/slack.php ' .escapeshellarg($message).'');
Web通知效果
手機APP通知效果
選All new messages 跟Notify Me on Mobile As quicky as possible
最後用下面這張圖來代表工程師的無奈與心酸XDD~~
你好!!我參考您的這篇文章, 對我幫助很大!!很謝謝你, 另外有一個問題想跟你請教, 我的slack alert message, 只會出現,"A warning has been issued that requires your attention.: 168.95.1.1-HinetDNS (168.95.1.1) URL : Link to Graph in Cacti Message: WARNING: 168.95.1.1-HinetDNS - Advanced Ping [avg] went above threshold of 5 with 10.5823", 看起來是只有把"NOTE:" 那段顯示出來, 請問我該如何修改?謝謝
回覆刪除想請問一下,在轉發 alert 的時候,它會帶一堆資訊,包括提示字串, device, url, message 跟圖檔,如果我只要讓它轉發 Message,其他的資訊都刪儲,該改甚麼地方呢?
回覆刪除將thold_functions.php第2847行裡面的 escapeshellarg($message)改成escapeshellarg($subject)即可
回覆刪除參考https://bit.ly/2pzAt7c
你好,我改成功了,謝謝.
回覆刪除另外,你那個網址是失效的.
Thanks, can I ask how to include the graph into the slack as well?
回覆刪除如果想要把slack的發送頻道依照告警內容切開發送的話,可以參考後續更新的文章教學
回覆刪除http://tanzih.blogspot.com/2021/01/cactislack.html