From 9036fd53b4d05b9518fc7c82428dc33dad3f96ec Mon Sep 17 00:00:00 2001
From: geg347 <37312097+geg347@users.noreply.github.com>
Date: Mon, 14 Mar 2022 22:39:00 +0100
Subject: [PATCH] Added extra dynamic values alerttransport alertmanager
(#13828)
* Add extra dynamic values to Alert transport for Alertmanager
* Add description to explain how extra dynamic values for Alertmanager works in the WebUI
* Add _ in dynamic label name + fix continuous-itegration error
* Fix continuous-itegration error part 2
* Updating AlertManager docs with explanation of how dynamic variables works
---
LibreNMS/Alert/Transport/Alertmanager.php | 9 +++++++--
doc/Alerting/Transports.md | 11 ++++++++++-
2 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/LibreNMS/Alert/Transport/Alertmanager.php b/LibreNMS/Alert/Transport/Alertmanager.php
index abd0241bb6..8e06a35430 100644
--- a/LibreNMS/Alert/Transport/Alertmanager.php
+++ b/LibreNMS/Alert/Transport/Alertmanager.php
@@ -66,7 +66,12 @@ class Alertmanager extends Transport
$url = $api['url'];
unset($api['url']);
foreach ($api as $label => $value) {
- $data[0]['labels'][$label] = $value;
+ // To allow dynamic values
+ if ((preg_match('/^extra_[A-Za-z0-9_]+$/', $label)) && (! empty($obj['faults'][1][$value]))) {
+ $data[0]['labels'][$label] = $obj['faults'][1][$value];
+ } else {
+ $data[0]['labels'][$label] = $value;
+ }
}
return $this->postAlerts($url, $data);
@@ -131,7 +136,7 @@ class Alertmanager extends Transport
[
'title' => 'Alertmanager Options',
'name' => 'alertmanager-options',
- 'descr' => 'Alertmanager Options',
+ '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).',
'type' => 'textarea',
],
],
diff --git a/doc/Alerting/Transports.md b/doc/Alerting/Transports.md
index f0637fd575..d7ba461ef2 100644
--- a/doc/Alerting/Transports.md
+++ b/doc/Alerting/Transports.md
@@ -63,6 +63,15 @@ It is possible to configure as many label values as required in
Alertmanager Options section. Every label and its value should be
entered as a new line.
+Labels can be a fixed string or a dynamic variable from the alert.
+To set a dynamic variable your label must start with extra_ then
+complete with the name of your label (only characters, figures and
+underscore are allowed here). The value must be the name of
+the variable you want to get (you can see all the variables in
+Alerts->Notifications by clicking on the Details icon of your alert when it is pending).
+If the variable's name does not match with an existing value the
+label's value will be the string you provided just as it was a fixed string.
+
Multiple Alertmanager URLs (comma separated) are supported. Each
URL will be tried and the search will stop at the first success.
@@ -73,7 +82,7 @@ URL will be tried and the search will stop at the first success.
| Config | Example |
| ------ | ------- |
| Alertmanager URL(s) | http://alertmanager1.example.com,http://alertmanager2.example.com |
-| Alertmanager Options: | source=librenms
customlabel=value |
+| Alertmanager Options: | source=librenms
customlabel=value
extra_dynamic_value=variable_name |
## API