From 73b7636c9ff8178578e1bc51d9dc6c0239348a5e Mon Sep 17 00:00:00 2001
From: Raphael Dannecker <49727759+raphael247@users.noreply.github.com>
Date: Thu, 20 Aug 2020 12:04:17 +0200
Subject: [PATCH] Matrix alert transport (#12018)
* Added Matrix as alert-channel (Feature request #4927)
* formatting
* Dokumentation of matrix-transport
* Matrix Server-URL
* Formatting
---
LibreNMS/Alert/Transport/Matrix.php | 114 ++++++++++++++++++++++++++++
doc/Alerting/Transports.md | 18 +++++
2 files changed, 132 insertions(+)
create mode 100644 LibreNMS/Alert/Transport/Matrix.php
diff --git a/LibreNMS/Alert/Transport/Matrix.php b/LibreNMS/Alert/Transport/Matrix.php
new file mode 100644
index 0000000000..f6b2c220c3
--- /dev/null
+++ b/LibreNMS/Alert/Transport/Matrix.php
@@ -0,0 +1,114 @@
+
+ * 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 . */
+
+/**
+ * Matrix Transport
+ * @author Raphael Dannecker (github.com/raphael247)
+ * @copyright 2020 , LibreNMS
+ * @license GPL
+ * @package LibreNMS
+ * @subpackage Alerts
+ */
+
+namespace LibreNMS\Alert\Transport;
+
+use LibreNMS\Alert\Transport;
+use LibreNMS\Enum\AlertState;
+use GuzzleHttp\Client;
+
+class Matrix extends Transport
+{
+ public function deliverAlert($obj, $opts)
+ {
+ $server = $this->config['matrix-server'];
+ $room = $this->config['matrix-room'];
+ $authtoken = $this->config['matrix-authtoken'];
+ $message = $this->config['matrix-message'];
+ return $this->contactMatrix($obj, $server, $room, $authtoken, $message);
+ }
+
+ private function contactMatrix($obj, $server, $room, $authtoken, $message)
+ {
+ $request_opts = [];
+ $request_heads = [];
+
+ $server = preg_replace('/\/$/', '', $server);
+ $host = $server."/_matrix/client/r0/rooms/".urlencode($room)."/send/m.room.message/".$obj['uid'];
+
+ $request_heads['Authorization'] = "Bearer $authtoken";
+ $request_heads['Content-Type'] = "application/json";
+ $request_heads['Accept'] = "application/json";
+
+ foreach ($obj as $p_key => $p_val) {
+ $message = str_replace("{{ $" . $p_key . ' }}', $p_val, $message);
+ }
+
+ $body = array('body'=>$message, 'msgtype'=>'m.text');
+
+ $client = new \GuzzleHttp\Client();
+ $request_opts['proxy'] = get_guzzle_proxy();
+ $request_opts['headers'] = $request_heads;
+ $request_opts['body'] = json_encode($body);
+ $res = $client->request('PUT', $host, $request_opts);
+
+ $code = $res->getStatusCode();
+ if ($code != 200) {
+ var_dump("Matrix '$host' returned Error");
+ var_dump("Params:");
+ var_dump("Response headers:");
+ var_dump($res->getHeaders());
+ var_dump("Return: ".$res->getReasonPhrase());
+ return 'HTTP Status code '.$code;
+ }
+ return true;
+ }
+
+ public static function configTemplate()
+ {
+ return [
+ 'config' => [
+ [
+ 'title' => 'Matrix-Server URL',
+ 'name' => 'matrix-server',
+ 'descr' => 'Matrix server URL up to the matrix api-part (for example: https://matrix.example.com/)',
+ 'type' => 'text',
+ ],
+ [
+ 'title' => 'Room',
+ 'name' => 'matrix-room',
+ 'descr' => 'Enter the room',
+ 'type' => 'text',
+ ],
+ [
+ 'title' => 'Auth_token',
+ 'name' => 'matrix-authtoken',
+ 'descr' => 'Enter the auth_token',
+ 'type' => 'text',
+ ],
+ [
+ 'title' => 'Message',
+ 'name' => 'matrix-message',
+ 'descr' => 'Enter the message',
+ 'type' => 'textarea',
+ ]
+ ],
+ 'validation' => [
+ 'matrix-server' => 'required',
+ 'matrix-room' => 'required',
+ 'matrix-authtoken' => 'required'
+ ]
+ ];
+ }
+}
diff --git a/doc/Alerting/Transports.md b/doc/Alerting/Transports.md
index 9ed98bac85..9bc13ca781 100644
--- a/doc/Alerting/Transports.md
+++ b/doc/Alerting/Transports.md
@@ -354,6 +354,24 @@ As a small reminder, here is it's configuration directives including defaults:
| ------ | ------- |
| Email | me@example.com |
+## Matrix
+
+For using the Matrix transports, you have to create a room on the Matrix-server.
+The provided Auth_token belongs to an user, which is member of this room.
+The Message, sent to the matrix-room can be built from the variables defined in
+[Template-Syntax](Templates.md#syntax) but without the 'alert->' prefix.
+See API-Transport. The variable ``` $msg ``` is contains the result of the Alert template.
+The Matrix-Server URL is cutted before the beginning of the ``_matrix/client/r0/...`` API-part.
+
+**Example:**
+
+| Config | Example |
+| ------ | ------- |
+| Matrix-Server URL | |
+| Room | !ajPbbPalmVbNuQoBDK:example.com |
+| Auth_token: | MDAyYmxvY2F0aW9uI...z1DCn6lz_uOhtW3XRICg |
+| Message: | Alert: {{ $msg }} https://librenms.example.com |
+
## Microsoft Teams
LibreNMS can send alerts to Microsoft Teams [Incoming Webhooks](https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook) which are