2019-01-25 15:37:27 -06:00
|
|
|
<?php
|
|
|
|
|
/*Copyright (c) 2019 GitStoph <https://github.com/GitStoph>
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
|
* option) any later version. Please see LICENSE.txt at the top level of
|
|
|
|
|
* the source code distribution for details. */
|
2023-05-23 09:25:17 -05:00
|
|
|
|
2019-01-25 15:37:27 -06:00
|
|
|
/**
|
|
|
|
|
* API Transport
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2019-01-25 15:37:27 -06:00
|
|
|
* @author GitStoph <https://github.com/GitStoph>
|
|
|
|
|
* @copyright 2019 GitStoph
|
|
|
|
|
* @license GPL
|
|
|
|
|
*/
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2019-01-25 15:37:27 -06:00
|
|
|
namespace LibreNMS\Alert\Transport;
|
|
|
|
|
|
|
|
|
|
use LibreNMS\Alert\Transport;
|
2020-05-24 04:14:36 +02:00
|
|
|
use LibreNMS\Enum\AlertState;
|
2023-05-23 09:25:17 -05:00
|
|
|
use LibreNMS\Exceptions\AlertTransportDeliveryException;
|
|
|
|
|
use LibreNMS\Util\Http;
|
|
|
|
|
use LibreNMS\Util\Url;
|
2019-01-25 15:37:27 -06:00
|
|
|
|
|
|
|
|
class Alerta extends Transport
|
|
|
|
|
{
|
2023-05-23 09:25:17 -05:00
|
|
|
public function deliverAlert(array $alert_data): bool
|
2019-01-25 15:37:27 -06:00
|
|
|
{
|
2023-05-23 09:25:17 -05:00
|
|
|
$severity = ($alert_data['state'] == AlertState::RECOVERED ? $this->config['recoverstate'] : $this->config['alertstate']);
|
2019-01-25 15:37:27 -06:00
|
|
|
$data = [
|
2023-05-23 09:25:17 -05:00
|
|
|
'resource' => $alert_data['display'],
|
|
|
|
|
'event' => $alert_data['name'],
|
|
|
|
|
'environment' => $this->config['environment'],
|
2019-01-25 15:37:27 -06:00
|
|
|
'severity' => $severity,
|
2023-05-23 09:25:17 -05:00
|
|
|
'service' => [$alert_data['title']],
|
|
|
|
|
'group' => $alert_data['name'],
|
|
|
|
|
'value' => $alert_data['state'],
|
|
|
|
|
'text' => strip_tags($alert_data['msg']),
|
|
|
|
|
'tags' => [$alert_data['title']],
|
2019-01-25 15:37:27 -06:00
|
|
|
'attributes' => [
|
2023-05-23 09:25:17 -05:00
|
|
|
'sysName' => $alert_data['sysName'],
|
|
|
|
|
'sysDescr' => $alert_data['sysDescr'],
|
|
|
|
|
'os' => $alert_data['os'],
|
|
|
|
|
'type' => $alert_data['type'],
|
|
|
|
|
'ip' => $alert_data['ip'],
|
|
|
|
|
'uptime' => $alert_data['uptime_long'],
|
|
|
|
|
'moreInfo' => '<a href=' . Url::deviceUrl($alert_data['device_id']) . '>' . $alert_data['display'] . '</a>',
|
2019-01-25 15:37:27 -06:00
|
|
|
],
|
2023-05-23 09:25:17 -05:00
|
|
|
'origin' => $alert_data['rule'],
|
|
|
|
|
'type' => $alert_data['title'],
|
2019-01-25 15:37:27 -06:00
|
|
|
];
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
$res = Http::client()
|
|
|
|
|
->withHeaders([
|
|
|
|
|
'Authorization' => 'Key ' . $this->config['apikey'],
|
|
|
|
|
])
|
|
|
|
|
->post($this->config['alerta-url'], $data);
|
|
|
|
|
|
|
|
|
|
if ($res->successful()) {
|
|
|
|
|
return true;
|
2019-01-25 15:37:27 -06:00
|
|
|
}
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
throw new AlertTransportDeliveryException($alert_data, $res->status(), $res->body(), $data['text'], $data);
|
2019-01-25 15:37:27 -06:00
|
|
|
}
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2023-05-23 09:25:17 -05:00
|
|
|
public static function configTemplate(): array
|
2019-01-25 15:37:27 -06:00
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'config' => [
|
|
|
|
|
[
|
|
|
|
|
'title' => 'API Endpoint',
|
|
|
|
|
'name' => 'alerta-url',
|
|
|
|
|
'descr' => 'Alerta API URL',
|
|
|
|
|
'type' => 'text',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'title' => 'Environment',
|
|
|
|
|
'name' => 'environment',
|
|
|
|
|
'descr' => 'An allowed environment from your alertad.conf.',
|
|
|
|
|
'type' => 'text',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'title' => 'Api Key',
|
|
|
|
|
'name' => 'apikey',
|
|
|
|
|
'descr' => 'Your alerta api key with minimally write:alert permissions.',
|
2023-09-01 20:07:39 +00:00
|
|
|
'type' => 'password',
|
2019-01-25 15:37:27 -06:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'title' => 'Alert State',
|
|
|
|
|
'name' => 'alertstate',
|
|
|
|
|
'descr' => 'What severity you want Alerta to reflect when rule matches.',
|
|
|
|
|
'type' => 'text',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'title' => 'Recover State',
|
|
|
|
|
'name' => 'recoverstate',
|
|
|
|
|
'descr' => 'What severity you want Alerta to reflect when rule unmatches/recovers.',
|
|
|
|
|
'type' => 'text',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'validation' => [
|
|
|
|
|
'alerta-url' => 'required|url',
|
|
|
|
|
'apikey' => 'required|string',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|