mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
* Add no_proxy and other proxy related settings Set user agent on all http client requests Unify http client usage * Style fixes * Remove useless use statements * Correct variable, good job phpstan * Add tests fix https_proxy bug add tcp:// to the config settings format * style and lint fixes * Remove guzzle from the direct dependencies * Use built in Laravel testing functionality * update baseline
146 lines
4.6 KiB
PHP
146 lines
4.6 KiB
PHP
<?php
|
|
/**
|
|
* Discord.php
|
|
*
|
|
* LibreNMS Discord API Tranport
|
|
*
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
*
|
|
* @link https://www.librenms.org
|
|
*
|
|
* @copyright 2018 Ryan Finney
|
|
* @author https://github.com/theherodied/
|
|
*
|
|
* @contributer f0o, sdef2
|
|
* Thanks to F0o <[email protected]> for creating the Slack transport which is the majority of this code.
|
|
* Thanks to sdef2 for figuring out the differences needed to make Discord work.
|
|
*/
|
|
|
|
namespace LibreNMS\Alert\Transport;
|
|
|
|
use LibreNMS\Alert\Transport;
|
|
use LibreNMS\Exceptions\AlertTransportDeliveryException;
|
|
use LibreNMS\Util\Http;
|
|
|
|
class Discord extends Transport
|
|
{
|
|
public const ALERT_FIELDS_TO_DISCORD_FIELDS = [
|
|
'timestamp' => 'Timestamp',
|
|
'severity' => 'Severity',
|
|
'hostname' => 'Hostname',
|
|
'name' => 'Rule Name',
|
|
'rule' => 'Rule',
|
|
];
|
|
|
|
public function deliverAlert(array $alert_data): bool
|
|
{
|
|
$added_fields = $this->parseUserOptions($this->config['options']);
|
|
|
|
$discord_title = '#' . $alert_data['uid'] . ' ' . $alert_data['title'];
|
|
$discord_msg = $alert_data['msg'];
|
|
$color = hexdec(preg_replace('/[^\dA-Fa-f]/', '', self::getColorForState($alert_data['state'])));
|
|
|
|
// Special handling for the elapsed text in the footer if the elapsed is not set.
|
|
$footer_text = $alert_data['elapsed'] ? 'alert took ' . $alert_data['elapsed'] : '';
|
|
|
|
$data = [
|
|
'embeds' => [
|
|
[
|
|
'title' => $discord_title,
|
|
'color' => $color,
|
|
'description' => $discord_msg,
|
|
'fields' => $this->createDiscordFields($alert_data),
|
|
'footer' => [
|
|
'text' => $footer_text,
|
|
],
|
|
],
|
|
],
|
|
];
|
|
if (! empty($added_fields)) {
|
|
$data = array_merge($data, $added_fields);
|
|
}
|
|
|
|
$data = $this->embedGraphs($data);
|
|
|
|
// remove all remaining HTML tags
|
|
$data['embeds'][0]['description'] = strip_tags($data['embeds'][0]['description']);
|
|
|
|
$res = Http::client()->post($this->config['url'], $data);
|
|
|
|
if ($res->successful()) {
|
|
return true;
|
|
}
|
|
|
|
throw new AlertTransportDeliveryException($alert_data, $res->status(), $res->body(), $discord_msg, $data);
|
|
}
|
|
|
|
private function embedGraphs(array $data): array
|
|
{
|
|
$count = 1;
|
|
$data['embeds'][0]['description'] = preg_replace_callback('#<img class="librenms-graph" src="(.*?)" />#', function ($match) use (&$data, &$count) {
|
|
$data['embeds'][] = [
|
|
'image' => [
|
|
'url' => $match[1],
|
|
],
|
|
];
|
|
|
|
return '[Image ' . ($count++) . ']';
|
|
}, $data['embeds'][0]['description']);
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function createDiscordFields(array $alert_data): array
|
|
{
|
|
$result = [];
|
|
|
|
foreach (self::ALERT_FIELDS_TO_DISCORD_FIELDS as $objKey => $discordKey) {
|
|
// Skip over keys that do not exist so Discord does not give us a 400.
|
|
if (! $alert_data[$objKey]) {
|
|
continue;
|
|
}
|
|
|
|
$result[] = [
|
|
'name' => $discordKey,
|
|
'value' => $alert_data[$objKey],
|
|
];
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function configTemplate(): array
|
|
{
|
|
return [
|
|
'config' => [
|
|
[
|
|
'title' => 'Discord URL',
|
|
'name' => 'url',
|
|
'descr' => 'Discord URL',
|
|
'type' => 'text',
|
|
],
|
|
[
|
|
'title' => 'Options',
|
|
'name' => 'options',
|
|
'descr' => 'Enter the config options (format: option=value separated by new lines)',
|
|
'type' => 'textarea',
|
|
],
|
|
],
|
|
'validation' => [
|
|
'url' => 'required|url',
|
|
],
|
|
];
|
|
}
|
|
}
|