2019-01-12 12:42:42 +02:00
<? php
/* Copyright (C) 2019 LibreNMS
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>. */
2019-01-12 12:42:42 +02:00
/**
* Alertmanager Transport
2021-09-10 20:09:53 +02:00
*
2019-01-12 12:42:42 +02:00
* @copyright 2019 LibreNMS
* @license GPL
*/
2020-09-21 14:54:51 +02:00
2019-01-12 12:42:42 +02: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-12 12:42:42 +02:00
class Alertmanager extends Transport
{
2023-05-23 09:25:17 -05:00
protected string $name = 'Alert Manager' ;
2021-10-06 07:29:47 -05:00
2023-05-23 09:25:17 -05:00
public function deliverAlert ( array $alert_data ) : bool
2019-01-12 12:42:42 +02:00
{
2023-05-23 09:25:17 -05:00
$url = $this -> config [ 'alertmanager-url' ];
$username = $this -> config [ 'alertmanager-username' ];
$password = $this -> config [ 'alertmanager-password' ];
2022-03-31 22:45:44 +02:00
2023-05-23 09:25:17 -05:00
$alertmanager_status = $alert_data [ 'state' ] == AlertState :: RECOVERED ? 'endsAt' : 'startsAt' ;
$alertmanager_msg = strip_tags ( $alert_data [ 'msg' ]);
2019-01-12 12:42:42 +02:00
$data = [[
2019-02-13 01:51:20 +02:00
$alertmanager_status => date ( 'c' ),
2023-05-23 09:25:17 -05:00
'generatorURL' => Url :: deviceUrl ( $alert_data [ 'device_id' ]),
2019-01-12 12:42:42 +02:00
'annotations' => [
2023-05-23 09:25:17 -05:00
'summary' => $alert_data [ 'name' ],
'title' => $alert_data [ 'title' ],
2019-01-12 12:42:42 +02:00
'description' => $alertmanager_msg ,
],
'labels' => [
2023-05-23 09:25:17 -05:00
'alertname' => $alert_data [ 'name' ],
'severity' => $alert_data [ 'severity' ],
'instance' => $alert_data [ 'hostname' ],
2019-01-12 12:42:42 +02:00
],
]];
2023-05-23 09:25:17 -05:00
$alertmanager_opts = $this -> parseUserOptions ( $this -> config [ 'alertmanager-options' ]);
foreach ( $alertmanager_opts as $label => $value ) {
2022-03-14 22:39:00 +01:00
// To allow dynamic values
2023-05-23 09:25:17 -05:00
if ( preg_match ( '/^extra_[A-Za-z0-9_]+$/' , $label ) && ! empty ( $alert_data [ 'faults' ][ 1 ][ $value ])) {
$data [ 0 ][ 'labels' ][ $label ] = $alert_data [ 'faults' ][ 1 ][ $value ];
2022-03-14 22:39:00 +01:00
} else {
$data [ 0 ][ 'labels' ][ $label ] = $value ;
}
2019-03-02 07:09:25 +02:00
}
2023-05-23 09:25:17 -05:00
$client = Http :: client () -> timeout ( 5 );
2021-03-27 16:01:48 +01:00
2022-03-31 22:45:44 +02:00
if ( $username != '' && $password != '' ) {
2023-05-23 09:25:17 -05:00
$client -> withBasicAuth ( $username , $password );
2022-03-31 22:45:44 +02:00
}
2021-03-27 16:01:48 +01:00
foreach ( explode ( ',' , $url ) as $am ) {
$post_url = ( $am . '/api/v2/alerts' );
2023-05-23 09:25:17 -05:00
$res = $client -> post ( $post_url , $data );
2021-03-27 16:01:48 +01:00
2023-05-23 09:25:17 -05:00
if ( $res -> successful ()) {
2021-03-27 16:01:48 +01:00
return true ;
}
}
2023-05-23 09:25:17 -05:00
throw new AlertTransportDeliveryException ( $alert_data , $res -> status (), $res -> body (), $alertmanager_msg , $data );
2019-01-12 12:42:42 +02:00
}
2023-05-23 09:25:17 -05:00
public static function configTemplate () : array
2019-01-12 12:42:42 +02:00
{
return [
'config' => [
[
2021-03-27 16:01:48 +01:00
'title' => 'Alertmanager URL(s)' ,
2019-01-12 12:42:42 +02:00
'name' => 'alertmanager-url' ,
2021-03-27 16:01:48 +01:00
'descr' => 'Alertmanager Webhook URL(s). Can contain comma-separated URLs' ,
2019-01-12 12:42:42 +02:00
'type' => 'text' ,
],
2022-03-31 22:45:44 +02:00
[
'title' => 'Alertmanager Username' ,
'name' => 'alertmanager-username' ,
'descr' => 'Alertmanager Basic Username to authenticate to Alertmanager' ,
'type' => 'text' ,
],
[
'title' => 'Alertmanager Password' ,
'name' => 'alertmanager-password' ,
'descr' => 'Alertmanager Basic Password to authenticate to Alertmanager' ,
'type' => 'password' ,
],
2019-01-12 12:42:42 +02:00
[
'title' => 'Alertmanager Options' ,
'name' => 'alertmanager-options' ,
2022-03-14 22:39:00 +01:00
'descr' => 'Alertmanager Options. You can add any fixed string value or dynamic value from alert details (label name must start with extra_ and value must exists in alert details).' ,
2019-01-12 12:42:42 +02:00
'type' => 'textarea' ,
],
],
'validation' => [
2021-03-27 16:01:48 +01:00
'alertmanager-url' => 'required|string' ,
2019-01-12 12:42:42 +02:00
],
];
}
}