diff --git a/LibreNMS/Alert/Transport/Discord.php b/LibreNMS/Alert/Transport/Discord.php index 6689df5340..76a2535a6a 100644 --- a/LibreNMS/Alert/Transport/Discord.php +++ b/LibreNMS/Alert/Transport/Discord.php @@ -33,6 +33,14 @@ use LibreNMS\Alert\Transport; class Discord extends Transport { + const ALERT_FIELDS_TO_DISCORD_FIELDS = [ + 'timestamp' => 'Timestamp', + 'severity' => 'Severity', + 'hostname' => 'Hostname', + 'name' => 'Rule Name', + 'rule' => 'Rule' + ]; + public function deliverAlert($obj, $opts) { $discord_opts = [ @@ -47,9 +55,25 @@ class Discord extends Transport { $host = $discord_opts['url']; $curl = curl_init(); + $discord_title = '#' . $obj['uid'] . ' ' . $obj['title']; $discord_msg = strip_tags($obj['msg']); + $color = self::getColorForState($obj['state']); + + // Special handling for the elapsed text in the footer if the elapsed is not set. + $footer_text = $obj['elapsed'] ? 'alert took ' . $obj['elapsed'] : ''; + $data = [ - 'content' => "". $obj['title'] ."\n" . $discord_msg + 'embeds' => [ + [ + 'title' => $discord_title, + 'color' => hexdec($color), + 'description' => $discord_msg, + 'fields' => $this->createDiscordFields($obj, $discord_opts), + 'footer' => [ + 'text' => $footer_text + ] + ] + ] ]; if (!empty($discord_opts['options'])) { $data = array_merge($data, $discord_opts['options']); @@ -74,6 +98,25 @@ class Discord extends Transport return true; } + public function createDiscordFields($obj, $discord_opts) + { + $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 (!$obj[$objKey]) { + continue; + } + + array_push($result, [ + 'name' => $discordKey, + 'value' => $obj[$objKey], + ]); + } + + return $result; + } + public static function configTemplate() { return [