mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Make the Discord transport more formatted for Discord. (#11461)
* Make the Discord transport more formatted for Discord. * Generate the fields using a function for cleaner code. * Handle cases where the elapsed time does not exist in the object.
This commit is contained in:
@@ -33,6 +33,14 @@ use LibreNMS\Alert\Transport;
|
|||||||
|
|
||||||
class Discord extends 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)
|
public function deliverAlert($obj, $opts)
|
||||||
{
|
{
|
||||||
$discord_opts = [
|
$discord_opts = [
|
||||||
@@ -47,9 +55,25 @@ class Discord extends Transport
|
|||||||
{
|
{
|
||||||
$host = $discord_opts['url'];
|
$host = $discord_opts['url'];
|
||||||
$curl = curl_init();
|
$curl = curl_init();
|
||||||
|
$discord_title = '#' . $obj['uid'] . ' ' . $obj['title'];
|
||||||
$discord_msg = strip_tags($obj['msg']);
|
$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 = [
|
$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'])) {
|
if (!empty($discord_opts['options'])) {
|
||||||
$data = array_merge($data, $discord_opts['options']);
|
$data = array_merge($data, $discord_opts['options']);
|
||||||
@@ -74,6 +98,25 @@ class Discord extends Transport
|
|||||||
return true;
|
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()
|
public static function configTemplate()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
Reference in New Issue
Block a user