Files
librenms-librenms/LibreNMS/Alert/Transport/Canopsis.php
T

132 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2020-09-21 14:54:51 +02:00
namespace LibreNMS\Alert\Transport;
2018-07-21 13:34:59 -06:00
use LibreNMS\Alert\Transport;
2020-09-21 14:54:51 +02:00
use LibreNMS\Enum\AlertState;
2018-07-21 13:34:59 -06:00
class Canopsis extends Transport
{
public function deliverAlert($obj, $opts)
2018-07-21 13:34:59 -06:00
{
2020-09-21 14:54:51 +02:00
if (! empty($this->config)) {
2018-07-21 13:34:59 -06:00
$opts['host'] = $this->config['canopsis-host'];
$opts['port'] = $this->config['canopsis-port'];
$opts['user'] = $this->config['canopsis-user'];
$opts['pass'] = $this->config['canopsis-pass'];
$opts['vhost'] = $this->config['canopsis-vhost'];
}
2020-09-21 14:54:51 +02:00
2018-07-21 13:34:59 -06:00
return $this->contactCanopsis($obj, $opts);
}
public function contactCanopsis($obj, $opts)
{
// Configurations
2020-09-21 14:54:51 +02:00
$host = $opts["host"];
$port = $opts["port"];
$user = $opts["user"];
$pass = $opts["pass"];
$vhost = $opts["vhost"];
$exchange = "canopsis.events";
// Connection
$conn = new \PhpAmqpLib\Connection\AMQPConnection($host, $port, $user, $pass, $vhost);
2020-09-21 14:54:51 +02:00
$ch = $conn->channel();
// Declare exchange (if not exist)
// exchange_declare($exchange, $type, $passive=false, $durable=false, $auto_delete=true, $internal=false, $nowait=false, $arguments=null, $ticket=null)
$ch->exchange_declare($exchange, 'topic', false, true, false);
// Create Canopsis event, see: https://github.com/capensis/canopsis/wiki/Event-specification
switch ($obj['severity']) {
case "ok":
$state = 0;
break;
case "warning":
2019-02-11 16:46:09 +01:00
$state = 2;
break;
case "critical":
2019-02-11 16:46:09 +01:00
$state = 3;
break;
default:
2019-02-11 16:46:09 +01:00
$state = 0;
}
2020-09-21 14:54:51 +02:00
$msg_body = [
"timestamp" => time(),
"connector" => "librenms",
"connector_name" => "LibreNMS1",
"event_type" => "check",
"source_type" => "resource",
"component" => $obj['hostname'],
2019-02-11 16:46:09 +01:00
"resource" => $obj['name'],
"state" => $state,
"output" => $obj['msg'],
2020-09-21 14:54:51 +02:00
"display_name" => "librenms",
];
$msg_raw = json_encode($msg_body);
// Build routing key
if ($msg_body['source_type'] == "resource") {
$msg_rk = $msg_rk . "." . $msg_body['resource'];
} else {
$msg_rk = $msg_body['connector'] . "." . $msg_body['connector_name'] . "." . $msg_body['event_type'] . "." . $msg_body['source_type'] . "." . $msg_body['component'];
}
// Publish Event
2020-09-21 14:54:51 +02:00
$msg = new \PhpAmqpLib\Message\AMQPMessage($msg_raw, ['content_type' => 'application/json', 'delivery_mode' => 2]);
$ch->basic_publish($msg, $exchange, $msg_rk);
// Close connection
$ch->close();
$conn->close();
2020-09-21 14:54:51 +02:00
return true;
}
2018-07-21 13:34:59 -06:00
public static function configTemplate()
{
return [
'config' => [
[
'title' => 'Hostname',
'name' => 'canopsis-host',
'descr' => 'Canopsis Hostname',
2020-09-21 14:54:51 +02:00
'type' => 'text',
2018-07-21 13:34:59 -06:00
],
[
'title' => 'Port Number',
'name' => 'canopsis-port',
'descr' => 'Canopsis Port Number',
2020-09-21 14:54:51 +02:00
'type' => 'text',
2018-07-21 13:34:59 -06:00
],
[
'title' => 'User',
'name' => 'canopsis-user',
'descr' => 'Canopsis User',
2020-09-21 14:54:51 +02:00
'type' => 'text',
2018-07-21 13:34:59 -06:00
],
[
'title' => 'Password',
'name' => 'canopsis-pass',
'descr' => 'Canopsis Password',
2020-09-21 14:54:51 +02:00
'type' => 'text',
2018-07-21 13:34:59 -06:00
],
[
'title' => 'Vhost',
'name' => 'canopsis-vhost',
'descr' => 'Canopsis Vhost',
2020-09-21 14:54:51 +02:00
'type' => 'text',
2018-07-21 13:34:59 -06:00
],
],
'validation' => [
'canopsis-host' => 'required|string',
'canopsis-port' => 'required|numeric',
'canopsis-user' => 'required|string',
'canopsis-pass' => 'required|string',
'canopsis-vhost' => 'required|string',
2020-09-21 14:54:51 +02:00
],
2018-07-21 13:34:59 -06:00
];
}
}