2020年9月18日

line notify for cacti 1.x 20200921更新版

1. vim thold_functions.php 找到「function thold_mail」這個 Function,在最後的「return '';」上方加入以下程式碼:
2.把 your_line_token 改成自己使用的 token
/* LINE訊息告警 */
thold_debug("LINE alert-----------------");
$ttime=date("Y/m/d h:i:sa");
$content = trim("$subject")."\n"."\n".$ttime;
if ($data != '') {
    foreach ($data as $val) {
        $ffile = fopen("/tmp/qq.png","w+");
        fwrite($ffile,$val);
        fclose($ffile);
        $linefields = array('message' => $content,
                            'imageFile' => curl_file_create('/tmp/qq.png'));
        line_notify($linefields);
        unlink('/tmp/qq.png');
    }
}else{
    $linefields = array('message' => $content);
    line_notify($linefields);
}
thold_debug("LINE alert-----------------");
/* LINE訊息告警 */


3.一樣在「function thold_mail」這個 Function裡面找到
$attachments[] = array(
    'attachment' => rrdtool_function_graph($val['local_graph_id'], '', $graph_data_array, ''),
    'filename' => 'graph_' . $val['local_graph_id'] . '.png',
    'mime_type' => 'image/png',
    'local_graph_id' => $val['local_graph_id'],
    'local_data_id' => $val['local_data_id'],
    'inline' => 'inline'
);

下面一行加上
/* LINE訊息告警 */
$data1 = @rrdtool_function_graph($val['local_graph_id'], '', $graph_data_array);
array_push($data, $data1);
/* LINE訊息告警 */


4.在function thold_mail」這個 Function,在最後的「return '';」下方加入以下程式碼
/* LINE訊息告警 */
function line_notify($linefields){
    $ch = curl_init();
    $lineurl="https://notify-api.line.me/api/notify" ;
    $linehead=array("Content-Type: multipart/form-data",
                        "Authorization: Bearer your_line_token");
    curl_setopt($ch, CURLOPT_URL, $lineurl);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $linehead);
    curl_setopt($ch, CURLOPT_NOBODY, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $linefields);
    curl_exec($ch);
    curl_close($ch);
}
/* LINE訊息告警 */


0 意見:

張貼留言