LibreNMS 預設是支援常見的 Line Notify,但不支援 Jandi 的.
所以試寫一個 Jandi 的 alert-transports
Demo 如下所示.
PS:以下程式的串接寫下僅適用於 librenms-22.12.x 以下的版本.
而且是抄改自 Linenotify.php 原碼,如下所示
[root@Oracle9 Transport]# pwd
/opt/librenms/LibreNMS/Alert/Transport
[root@Oracle9 Transport]# cat Linenotify.php
<?php
/**
* LINE Notify Transport
*/
namespace LibreNMS\Alert\Transport;
use LibreNMS\Alert\Transport;
use LibreNMS\Util\Proxy;
class Linenotify extends Transport
{
protected $name = 'LINE Notify';
public function deliverAlert($obj, $opts)
{
$opts['line-notify-access-token'] = $this->config['line-notify-access-token'];
return $this->contactLinenotify($obj, $opts);
}
private function contactLinenotify($obj, $opts)
{
$lineUrl = 'https://notify-api.line.me/api/notify';
$lineHead = ['Authorization: Bearer ' . $opts['line-notify-access-token']];
$lineFields = ['message' => $obj['msg']];
$curl = curl_init();
Proxy::applyToCurl($curl);
curl_setopt($curl, CURLOPT_URL, $lineUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, $lineHead);
curl_setopt($curl, CURLOPT_NOBODY, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $lineFields);
curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($code != 200) {
return 'HTTP Status code ' . $code;
}
return true;
}
public static function configTemplate()
{
return [
'config' => [
[
'title' => 'Token',
'name' => 'line-notify-access-token',
'descr' => 'LINE Notify Token',
'type' => 'text',
],
],
'validation' => [
'line-notify-access-token' => 'required|string',
],
];
}
}
[root@Oracle9 Transport]#
抄改成 Jandi 的程式如下所示
[root@Oracle9 Transport]# pwd
/opt/librenms/LibreNMS/Alert/Transport
[root@Oracle9 Transport]# cat Jandi.php
<?php
/**
* Jandi webhook Transport
*/
namespace LibreNMS\Alert\Transport;
use LibreNMS\Alert\Transport;
use LibreNMS\Util\Proxy;
class Jandi extends Transport
{
protected $name = 'Jandi';
public function deliverAlert($obj, $opts)
{
$opts['jandi-webhook-access-token'] = $this->config['jandi-webhook-access-token'];
$opts['jandi-webhook-access-rec'] = $this->config['jandi-webhook-access-rec'];
return $this->contactLineNotify($obj, $opts);
}
private function contactLinenotify($obj, $opts)
{
$webhooktoken=$opts['jandi-webhook-access-token'];
$webhookid=$opts['jandi-webhook-access-rec'];
$lineUrl = "https://wh.jandi.com/connect-api/webhook/$webhooktoken/$webhookid";
$lineHead = ['Accept: application/vnd.tosslab.jandi-v2+json','Content-Type: application/json'];
// $lineFields = [body' => body , 'connectColor' => '#FAC11B' , 'connectInfo' => [ 'title' => title , 'description' => $obj['msg'] ] ];
$jmsg=$obj['msg'];
$nowtime=date('Y-m-d H:i:s');
$lineFields = <<<DATA
{
"body" : "$nowtime",
"connectColor" : "#FAC11B",
"connectInfo" : [
{ "title" : "AlertMessage",
"description" : "$jmsg"
}
]
}
DATA;
// $lineFields = ["body" => body];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lineUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, $lineHead);
curl_setopt($curl, CURLOPT_NOBODY, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $lineFields);
curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($code != 200) {
return 'HTTP Status code ' . $code;
}
return true;
}
public static function configTemplate()
{
return [
'config' => [
[
'title' => 'Jandi WebHook Token',
'name' => 'jandi-webhook-access-token',
'descr' => 'Jandi WEBHOOK Token',
'type' => 'text',
],
[
'title' => 'Jandi WebHook Recipient',
'name' => 'jandi-webhook-access-rec',
'descr' => 'Jandi WEBHOOK Recipient',
'type' => 'text',
],
],
'validation' => [
'jandi-webhook-access-token' => 'required|string',
'jandi-webhook-access-rec' => 'required|string',
],
];
}
}
[root@Oracle9 Transport]#
DEMO
沒有留言:
張貼留言