Added Alertmanager transport (#9637)

* Added Alertmanager transport

* Fixed as per comments

* Update Alertmanager.php

* Update Alertmanager.php
This commit is contained in:
angryp
2019-01-12 12:42:42 +02:00
committed by Neil Lathwood
parent c99c97acf3
commit 0c188c5995
2 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,106 @@
<?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
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/**
* Alertmanager Transport
* @copyright 2019 LibreNMS
* @license GPL
* @package LibreNMS
* @subpackage Alerts
*/
namespace LibreNMS\Alert\Transport;
use LibreNMS\Alert\Transport;
use LibreNMS\Config;
class Alertmanager extends Transport
{
public function deliverAlert($obj, $opts)
{
$alertmanager_opts = [];
$alertmanager_opts['url'] = $this->config['alertmanager-url'];
foreach (explode(PHP_EOL, $this->config['alertmanager-options']) as $option) {
list($k,$v) = explode('=', $option);
$alertmanager_opts[$k] = $v;
}
return $this->contactAlertmanager($obj, $alertmanager_opts);
}
public static function contactAlertmanager($obj, $api)
{
if ($obj['state'] == 0) {
$alertmanager_status = 'resolved';
} else {
$alertmanager_status = 'firing';
}
$gen_url = (Config::get('base_url') . 'device/device=' . $obj['device_id']);
$host = ($api['url'] . '/api/v1/alerts');
$curl = curl_init();
$alertmanager_msg = strip_tags($obj['msg']);
$data = [[
'status' => $alertmanager_status,
'generatorURL' => $gen_url,
'annotations' => [
'summary' => $obj['name'],
'title' => $obj['title'],
'description' => $alertmanager_msg,
],
'labels' => [
'alertname' => $obj['name'],
'severity' => $obj['severity'],
'instance' => $obj['hostname'],
'source' => $api['source'],
],
]];
$alert_message = json_encode($data);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
set_curl_proxy($curl);
curl_setopt($curl, CURLOPT_URL, $host);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $alert_message);
$ret = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code != 200) {
return 'HTTP Status code ' . $code;
}
return true;
}
public static function configTemplate()
{
return [
'config' => [
[
'title' => 'Alertmanager URL',
'name' => 'alertmanager-url',
'descr' => 'Alertmanager Webhook URL',
'type' => 'text',
],
[
'title' => 'Alertmanager Options',
'name' => 'alertmanager-options',
'descr' => 'Alertmanager Options',
'type' => 'textarea',
]
],
'validation' => [
'alertmanager-url' => 'required|url',
]
];
}
}

View File

@@ -24,6 +24,24 @@ To include users that have `Global-Read`, `Administrator` or `Normal-User` permi
## Using a Proxy?
[Proxy Configuration](../Support/Configuration.md#proxy-support)
## Alertmanager
Alertmanager is an alert handling software, initially developed for alert processing sent by Prometheus.
It has built-in functionality for deduplicating, grouping and routing alerts based on configurable criteria.
LibreNMS uses alert grouping by alert rule, which can produce an array of alerts of similar content for an array of hosts, whereas Alertmanager can group them by alert meta, ideally producing one single notice in case an issue occurs.
The one and only possible parameter to be passed is `source` - this is required to distinguish LibreNMS alerts from alerts coming from different sources.
[Alertmanager Docs](https://prometheus.io/docs/alerting/alertmanager/)
**Example:**
| Config | Example |
| ------ | ------- |
| Alertmanager URL | http://alertmanager.example.com |
| Alertmanager Options: | source=librenms |
## API
API transports definitions are a bit more complex than the E-Mail configuration.