Adding Information to Notification-EMails

Hey Guys,

I’m looking for a reference on how to add Information to the standard-email notifications for services and hosts that are being send by Icinga.

So far, I found a shellscript that generates HTML-Emails ( I know, I know… :expressionless: ) and a modification to insert graphite/grafana visualizations to the notifications.

That’s all fine and awesome but specifically I’m searching for a way to add Information like “Notes” and “Notes URL” to the Mail.

Does anyone of you has any hints on how to achieve this?

Greets and Regards
voddlt

Hi,

Nwc_health Line wrapping notifications don´t work might inspire you in terms of such a script.

There’s a few steps involved in order to pass details to a script:

  • Ensure that the notification script reads parameters from the CLI or via environment variable - you can test that standalone on your terminal.
  script.sh --notes="...." --notes-url="..."

Depending on the script language you’re using, you’ll need to implement and extend getopts for this.

  • Extend the NotificationCommand and add additional arguments with using the previously added script parameters. Use placeholder runtime macros, you’ll need them in the actual notification object later.
  "--notes" = {
     description = "Host/Service notes"
     value = "$notification_notes$"
  }
  "--notes-url" = {
     description "Host/Service notes_url"
     value = "$notification_notes_url$"
  }
  • Adopt the notification (apply rule) config, add the required custom attributes.
apply Notification "..." to Service {
  ...

  if (service.notes != "") {
    vars.notification_notes = service.notes
  } else if (host.notes != "") {
     vars.notification_nodes = host.notes //fallback
  }
  if (service.notes_url != "") {
    vars.notification_notes_url = service.notes_url
  } else if (host.notes_url != "") {
    vars.notification_notes_url = host.notes_url //fallback
  }

  ...
}

apply Notification "..." to Host {
  ...
  if (host.notes != "") {
    vars.notification_notes = host.notes
  }

  if (host.notes_url != "") {
    vars.notification_notes_url = host.notes_url
  }

  ...

}

Cheers,
Michael

Hi Michael and thank you for your help :heart:

Basically I’m using the standard-bash-script for notifications that comes with the Icinga installation and what works (as in: compiles an EMail from CLI with all given information) is:
./mail-service-notification_notes.sh -d "2019-04-23" -e "testservice" -l "testhost" -n "testus0r" -o "all cool" -r "testus0r@domain.org" -s "OK" -t "custom" -u "testservice" -a "here goes the notes and with -b I send the notes-URL" -b "https://www.google.de"

What gives me a headache is how I actually fill -a and -b with the Information from Icinga.

So far, I have the following command:

 object NotificationCommand "notes_service_alarm_by_email" {
     import "plugin-notification-command"
     command = [
         "/etc/icinga2/scripts/mail-service-notification_notes.sh"
     ]
     arguments += {
         "-a" = "$service.notes$"
         "-b" = "$service.notes_url$"
         "-d" = {
             repeat_key = false
             required = true
             value = "$icinga.long_date_time$"
         }
         "-e" = {
             repeat_key = false
             required = true
             value = "$service.name$"
         }
         "-l" = {
             repeat_key = false
             required = true
             value = "$host.name$"
         }
         "-n" = {
             repeat_key = false
             required = true
             value = "$host.display_name$"
         }
         "-o" = {
             repeat_key = false
             required = true
             value = "$service.output$"
         }
         "-r" = {
             repeat_key = false
             required = true
             value = "$user.email$"
         }
         "-s" = {
             repeat_key = false
             required = true
             value = "$service.state$"
         }
         "-t" = {
             repeat_key = false
             required = true
             value = "$notification.type$"
         }
         "-u" = {
             repeat_key = false
             required = true
             value = "$service.display_name$"
         }
     }
 }

Does that look right to you?

It looks ok, but I would only set these parameters if the host/service objects actually have them. That is why I have done the extra step with the parameter names inside the notification, only setting them if those attributes actually exist.

apply Notification "..." to Service {
  ...

  if (service.notes != "") {
    vars.notification_notes = service.notes
  } else if (host.notes != "") {
     vars.notification_nodes = host.notes //fallback
  }
  if (service.notes_url != "") {
    vars.notification_notes_url = service.notes_url
  } else if (host.notes_url != "") {
    vars.notification_notes_url = host.notes_url //fallback
  }

  ...
}

In case you’re using the Director, you cannot use this method, and yours is more simple. It only needs proper error handling inside the script, e.g. what to show when notes_url is empty (hide the row for instance).

Cheers,
Michael

Thanks man, worked liked a charm :smile:

I am considering to write a little how-to now :smirk:

I appreciate your help!

1 Like

Go for it - sharing the knowledge is key for future adventures :slight_smile: Just hop onto #howto and explore how others structure theirs, with markdown formatting including headings, etc.