Notifications not sent via mail


(H. Hamester) #1

Hello,

I can’t find the reason why my notification doesn’t work:

NotificationCommand:

object NotificationCommand "mail-service-notification" {
  import "plugin-notification-command"

  command = [ SysconfDir + "/icinga2/scripts/mail-service-notification.sh" ]

  env = {
        NOTIFICATIONTYPE = "$notification.type$"
        SERVICENAME = "$service.name$"
        HOSTNAME = "$host.name$"
        HOSTDISPLAYNAME = "$host.display_name$"
        HOSTADDRESS = "$address$"
        SERVICESTATE = "$service.state$"
        LONGDATETIME = "$icinga.long_date_time$"
  }
}

User:

object User "xy" {
                import "generic-user"
                display_name = "xy"
                enable_notifications = true
                groups = [ "icingaadmins", "monitoring", "test" ]
                email = "xy@xxyy.de"
                pager = "010101010"
                period = "24x7"
                states = [ OK, Up, Down, Warning, Critical ]
                types = [ Problem, Custom, Acknowledgement, Recovery ]
}

NotificationTemplate:

template Notification "mail-service-notification" {
  command = "mail-service-notification"

  states = [ OK, Warning, Critical, Unknown ]
  types = [ Problem, Acknowledgement, Recovery, Custom,
                        FlappingStart, FlappingEnd,
                        DowntimeStart, DowntimeEnd, DowntimeRemoved ]

    interval = 30s
  period = "24x7"
}

Host:

object Host             "xyx" {
        import                  "generic-host"
        address =               "000.00.2.00"
        notes =                 "Web Server NL"
        notes_url =             ""
        vars.os =               "linux"
        vars.sla =              "24x7"
        vars.backup_downtime =  "20:00-22:00"
}

Service:

apply Service "memory" {
        import "generic-service"
        check_command = "check-linux-memory"
        assign where host.address && host.vars.os == "linux"

}

Notification:

apply Notification "linux-memory" to Service {
        import "mail-service-notification"

        user = [ "xy" ]

        assign where host.vars.os == "linux" && service.name == "memory"
}

icinga2 object list -t Notification:

Object ‘08livweb!memory!linux-basics’ of type ‘Notification’:
% declared in ‘/etc/icinga2/zones.d/global/notifications/linux.conf’, lines 1:0-1:43

  • __name = “08livweb!memory!linux-basics”
  • command = “mail-service-notification”
    % = modified in ‘/etc/icinga2/zones.d/global/notificationtemplates/mail-service-notification.conf’, lines 2:3-2:39
  • command_endpoint = “”
  • host_name = “08livweb”
    % = modified in ‘/etc/icinga2/zones.d/global/notifications/linux.conf’, lines 1:0-1:43
  • interval = 30
    % = modified in ‘/etc/icinga2/zones.d/global/notificationtemplates/mail-service-notification.conf’, lines 9:5-9:18
  • name = “linux-basics”
  • package = “_etc”
    % = modified in ‘/etc/icinga2/zones.d/global/notifications/linux.conf’, lines 1:0-1:43
  • period = “24x7”
    % = modified in ‘/etc/icinga2/zones.d/global/notificationtemplates/mail-service-notification.conf’, lines 10:3-10:17
  • service_name = “memory”
    % = modified in ‘/etc/icinga2/zones.d/global/notifications/linux.conf’, lines 1:0-1:43
  • source_location
    • first_column = 0
    • first_line = 1
    • last_column = 43
    • last_line = 1
    • path = “/etc/icinga2/zones.d/global/notifications/linux.conf”
  • states = [ “OK”, “Warning”, “Critical”, “Unknown” ]
    % = modified in ‘/etc/icinga2/zones.d/global/notificationtemplates/mail-service-notification.conf’, lines 4:3-4:45
  • templates = [ “linux-basics”, “mail-service-notification” ]
    % = modified in ‘/etc/icinga2/zones.d/global/notifications/linux.conf’, lines 1:0-1:43
    % = modified in ‘/etc/icinga2/zones.d/global/notificationtemplates/mail-service-notification.conf’, lines 1:0-1:48
  • times = null
  • type = “Notification”
  • types = [ “Problem”, “Acknowledgement”, “Recovery”, “Custom”, “FlappingStart”, “FlappingEnd”, “DowntimeStart”, “DowntimeEnd”, “DowntimeRemoved” ]
    % = modified in ‘/etc/icinga2/zones.d/global/notificationtemplates/mail-service-notification.conf’, lines 5:3-7:69
  • user_groups = [ “test” ]
    % = modified in ‘/etc/icinga2/zones.d/global/notifications/linux.conf’, lines 4:2-4:25
  • users = null
  • vars = null
  • zone = “08hhba”
    % = modified in ‘/etc/icinga2/zones.d/global/notifications/linux.conf’, lines 1:0-1:43

A direct command “mail xy@xyx.de” work fine.


#2

Could you send the answer of the command : icinga2 object list but for the hosts this time.

there is a host variable called vars.notification.mail that might help you. It has to be ‘true’.


#3

Your NotificationCommand “mail-service-notification” is old style using environment variables. Are you sure you still use the old style mail-service-notification.sh script as well? I think with 2.7 or 2.8 it changed to using variables/arguments instead of environment variables. If you have upgraded mail-service-notification.sh to new style and the configuration is still old style it probably won’t work…


(H. Hamester) #4

Yes, I upgraded the command and scipts - WORK!

Nice! Thank you so much!


(H. Hamester) #5

I’m really confused! When I force the notification in Icinga Web 2, I get a (costum-)mail.

If a host change to down, I get no mails.

User:

object User "yx" {
                import "generic-user"
                display_name = "yx"
                enable_notifications = true
                groups = [ "icingaadmins", "monitoring", "test" ]
                email = "yx@X.de"
                pager = "49179999999"
                period = "24x7"
                states = [ OK, Up, Down, Warning, Critical ]
                types = [ Problem, Custom, Acknowledgement, Recovery ]
}

Command:

object NotificationCommand "mail-host-notification" {
  command = [ SysconfDir + "/icinga2/scripts/mail-host-notification.sh" ]

  env = {
    NOTIFICATIONTYPE = "$notification.type$"
    HOSTDISPLAYNAME = "$host.display_name$"
    HOSTNAME = "$host.name$"
    HOSTADDRESS = "$address$"
    HOSTSTATE = "$host.state$"
    LONGDATETIME = "$icinga.long_date_time$"
    HOSTOUTPUT = "$host.output$"
    NOTIFICATIONAUTHORNAME = "$notification.author$"
    NOTIFICATIONCOMMENT = "$notification.comment$"
    USEREMAIL = "$user.email$"
  }
}

Template:

template Notification "mail-host-notification" {
  command = "mail-host-notification"

  states = [ Up, Down ]
  types = [ Problem, Acknowledgement, Recovery, Custom,
                        FlappingStart, FlappingEnd,
                        DowntimeStart, DowntimeEnd, DowntimeRemoved ]
  period = "24x7"
}

Notification:

apply Notification "mail-icingaadmin" to Host {
  import "mail-host-notification"

  user_groups = host.vars.notification.mail.groups

  assign where host.vars.notification.mail
}

Host:

object Host             "router-primaer" {
        import          "router"
        address =       "192.168.00.XXX"
        notes =         "WAN"
        vars.ints["GigabitEthernet0/1"] = { descr = "wan" }
        vars.ints["GigabitEthernet0/2"] = { descr = "lan" }
        vars.ints["GigabitEthernet0/3"] = { descr = "interlink" }
        vars.sla =              "24x7"
       vars.notification["mail"] = {
               groups = [ "test" ]
            }

Object 'router-primaer!mail-icingaadmin' of type 'Notification':
  % declared in '/etc/icinga2/zones.d/global/notifications/wan.conf', lines 1:0-1:44
  * __name = "router-primaer!mail-icingaadmin"
  * command = "mail-host-notification"
    % = modified in '/etc/icinga2/zones.d/global/notificationtemplates/mail-host-notification.conf', lines 2:3-2:36
  * command_endpoint = ""
  * host_name = "08wan-versatel-hhba-primaer"
    % = modified in '/etc/icinga2/zones.d/global/notifications/wan.conf', lines 1:0-1:44
  * interval = 1800
  * name = "mail-icingaadmin"
  * package = "_etc"
    % = modified in '/etc/icinga2/zones.d/global/notifications/wan.conf', lines 1:0-1:44
  * period = "24x7"
    % = modified in '/etc/icinga2/zones.d/global/notificationtemplates/mail-host-notification.conf', lines 8:3-8:17
  * service_name = ""
  * source_location
    * first_column = 0
    * first_line = 1
    * last_column = 44
    * last_line = 1
    * path = "/etc/icinga2/zones.d/global/notifications/wan.conf"
  * states = [ "Up", "Down" ]
    % = modified in '/etc/icinga2/zones.d/global/notificationtemplates/mail-host-notification.conf', lines 4:3-4:23
  * templates = [ "mail-icingaadmin", "mail-host-notification" ]
    % = modified in '/etc/icinga2/zones.d/global/notifications/wan.conf', lines 1:0-1:44
    % = modified in '/etc/icinga2/zones.d/global/notificationtemplates/mail-host-notification.conf', lines 1:0-1:45
  * times = null
  * type = "Notification"
  * types = [ "Problem", "Acknowledgement", "Recovery", "Custom", "FlappingStart", "FlappingEnd", "DowntimeStart", "DowntimeEnd", "DowntimeRemoved" ]
    % = modified in '/etc/icinga2/zones.d/global/notificationtemplates/mail-host-notification.conf', lines 5:3-7:69
  * user_groups = [ "test" ]
    % = modified in '/etc/icinga2/zones.d/global/notifications/wan.conf', lines 4:3-4:50
  * users = null
  * vars = null
  * zone = "master"
    % = modified in '/etc/icinga2/zones.d/global/notifications/wan.conf', lines 1:0-1:44

I’ve tried so many ways to fix it, but it doesn’t work :triumph:


(H. Hamester) #6

Hello,

my notifications working now.

How and where can I set additional information like host.notes or notes_url for mails?


#7

Add definitions for the attributes to the env array and refer to them in the notification script.


(H. Hamester) #8

Okay for example:

object NotificationCommand "mail-host-notification" {
  command = [ SysconfDir + "/icinga2/scripts/mail-host-notification.sh" ]

  env = {
    NOTIFICATIONTYPE = "$notification.type$"
    HOSTDISPLAYNAME = "$host.display_name$"
    HOSTNAME = "$host.name$"
    HOSTADDRESS = "$address$"
    HOSTSTATE = "$host.state$"
    LONGDATETIME = "$icinga.long_date_time$"
    HOSTOUTPUT = "$host.output$"
    NOTIFICATIONAUTHORNAME = "$notification.author$"
    NOTIFICATIONCOMMENT = "$notification.comment$"
    USEREMAIL = "$user.email$"
    NOTES = "$host.notes$"  // for example 
  }
}


object Host             "wan-test" {
        import          "router-"
        address =       "192.888.88.888"
        notes =         "WAN TEST"
        vars.ints["GigabitEthernet0/1"] = { descr = "wan" }
        vars.ints["GigabitEthernet0/2"] = { descr = "lan" }
        vars.ints["GigabitEthernet0/3"] = { descr = "interlink" }
            vars.notification["mail"] = {
        groups = [ "test" ]
        }

apply Notification "wan-versatel" to Host {
  import "host-24x7"

  user_groups = host.vars.notification.mail.groups
  users = host.vars.notification.mail.users
  vars.notes = host.notes
  assign where host.vars.os == "wan" && host.vars.notification.mail
}

Where is my mistake?


#9

Please explain what happens. You’re the only one in front of your system.


(H. Hamester) #10

Unbenannt

I’ve no note information from host in the mail.
I’m looking for a row like “Notes: WAN TEST”


#11

Please the notification script.


(H. Hamester) #12
#!/usr/bin/env bash
#
# Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)

PROG="`basename $0`"
ICINGA2HOST="`hostname`"
MAILBIN="mail"

if [ -z "`which $MAILBIN`" ] ; then
  echo "$MAILBIN not found in \$PATH. Consider installing it."
  exit 1
fi

## Function helpers
Usage() {
cat << EOF
Required parameters:
  -d LONGDATETIME (\$icinga.long_date_time\$)
  -e SERVICENAME (\$service.name\$)
  -l HOSTNAME (\$host.name\$)
  -n HOSTDISPLAYNAME (\$host.display_name\$)
  -o SERVICEOUTPUT (\$service.output\$)
  -r USEREMAIL (\$user.email\$)
  -s SERVICESTATE (\$service.state\$)
  -t NOTIFICATIONTYPE (\$notification.type\$)
  -u SERVICEDISPLAYNAME (\$service.display_name\$)
Optional parameters:
  -4 HOSTADDRESS (\$address\$)
  -6 HOSTADDRESS6 (\$address6\$)
  -b NOTIFICATIONAUTHORNAME (\$notification.author\$)
  -c NOTIFICATIONCOMMENT (\$notification.comment\$)
  -i ICINGAWEB2URL (\$notification_icingaweb2url\$, Default: unset)
  -f MAILFROM (\$notification_mailfrom\$, requires GNU mailutils (Debian/Ubuntu) or mailx (RHEL/SUSE))
  -v (\$notification_sendtosyslog\$, Default: false)
EOF
}

Help() {
  Usage;
  exit 0;
}

Error() {
  if [ "$1" ]; then
    echo $1
  fi
  Usage;
  exit 1;
}

## Main
while getopts 4:6:b:c:d:e:f:hi:l:n:o:r:s:t:u:v: opt
do
  case "$opt" in
    4) HOSTADDRESS=$OPTARG ;;
    6) HOSTADDRESS6=$OPTARG ;;
    b) NOTIFICATIONAUTHORNAME=$OPTARG ;;
    c) NOTIFICATIONCOMMENT=$OPTARG ;;
    d) LONGDATETIME=$OPTARG ;; # required
    e) SERVICENAME=$OPTARG ;; # required
    f) MAILFROM=$OPTARG ;;
    h) Usage ;;
    i) ICINGAWEB2URL=$OPTARG ;;
    l) HOSTNAME=$OPTARG ;; # required
    n) HOSTDISPLAYNAME=$OPTARG ;; # required
    o) SERVICEOUTPUT=$OPTARG ;; # required
    r) USEREMAIL=$OPTARG ;; # required
    s) SERVICESTATE=$OPTARG ;; # required
    t) NOTIFICATIONTYPE=$OPTARG ;; # required
    u) SERVICEDISPLAYNAME=$OPTARG ;; # required
    v) VERBOSE=$OPTARG ;;
   \?) echo "ERROR: Invalid option -$OPTARG" >&2
       Usage ;;
    :) echo "Missing option argument for -$OPTARG" >&2
       Usage ;;
    *) echo "Unimplemented option: -$OPTARG" >&2
       Usage ;;
  esac
done

shift $((OPTIND - 1))

## Check required parameters (TODO: better error message)
## Keep formatting in sync with mail-host-notification.sh
if [ ! "$LONGDATETIME" ] \
|| [ ! "$HOSTNAME" ] || [ ! "$HOSTDISPLAYNAME" ] \
|| [ ! "$SERVICENAME" ] || [ ! "$SERVICEDISPLAYNAME" ] \
|| [ ! "$SERVICEOUTPUT" ] || [ ! "$SERVICESTATE" ] \
|| [ ! "$USEREMAIL" ] || [ ! "$NOTIFICATIONTYPE" ]; then
  Error "Requirement parameters are missing."
fi

## Build the message's subject
SUBJECT="[$NOTIFICATIONTYPE] $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!"

## Build the notification message
NOTIFICATION_MESSAGE=`cat << EOF
***** Service Monitoring on $ICINGA2HOST *****
$SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!
Info:    $SERVICEOUTPUT
When:    $LONGDATETIME
Service: $SERVICENAME
Host:    $HOSTNAME
EOF
`

## Check whether IPv4 was specified.
if [ -n "$HOSTADDRESS" ] ; then
  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
IPv4:    $HOSTADDRESS"
fi

## Check whether IPv6 was specified.
if [ -n "$HOSTADDRESS6" ] ; then
  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
IPv6:    $HOSTADDRESS6"
fi

## Check whether author and comment was specified.
if [ -n "$NOTIFICATIONCOMMENT" ] ; then
  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
Comment by $NOTIFICATIONAUTHORNAME:
  $NOTIFICATIONCOMMENT"
fi

## Check whether Icinga Web 2 URL was specified.
if [ -n "$ICINGAWEB2URL" ] ; then
  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
$ICINGAWEB2URL/monitoring/service/show?host=$HOSTNAME&service=$SERVICENAME"
fi

## Check whether verbose mode was enabled and log to syslog.
if [ "$VERBOSE" == "true" ] ; then
  logger "$PROG sends $SUBJECT => $USEREMAIL"
fi

## Send the mail using the $MAILBIN command.
## If an explicit sender was specified, try to set it.
if [ -n "$MAILFROM" ] ; then

  ## Modify this for your own needs!

  ## Debian/Ubuntu use mailutils which requires `-a` to append the header
  if [ -f /etc/debian_version ]; then
    /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" | $MAILBIN -a "From: $MAILFROM" -s "$SUBJECT" $USEREMAIL
  ## Other distributions (RHEL/SUSE/etc.) prefer mailx which sets a sender address with `-r`
  else
    /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" | $MAILBIN -r "$MAILFROM" -s "$SUBJECT" $USEREMAIL
  fi

else
  /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" \
  | $MAILBIN -s "$SUBJECT" $USEREMAIL
fi

(H. Hamester) #13

I think I have to set a costum parameter in the script like

-n NOTES ($host.notes)

???


#14

There’s no trace of NOTES in the script so how do you expect the information should be transmitted?


#15
Check whether
...
fi

shows how to add information.


(H. Hamester) #16

I’m not really familiar with scripts… :frowning:

Can you give me an example for notes_url, please?


(H. Hamester) #17

I defined the parameter in the script and it works!
Thank you!


#18

One way might be like the following

## Check whether host notes was specified.
if [ -n "$NOTES" ] ; then
  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
Notes:	 $NOTES"
fi

(H. Hamester) #19

I set it like this:

PROG="`basename $0`"
ICINGA2HOST="`hostname`"
MAILBIN="mail"

if [ -z "`which $MAILBIN`" ] ; then
  echo "$MAILBIN not found in \$PATH. Consider installing it."
  exit 1
fi

## Function helpers
Usage() {
cat << EOF

Required parameters:
  -d LONGDATETIME (\$icinga.long_date_time\$)
  -l HOSTNAME (\$host.name\$)
  -n HOSTDISPLAYNAME (\$host.display_name\$)
  -o HOSTOUTPUT (\$host.output\$)
  -r USEREMAIL (\$user.email\$)
  -s HOSTSTATE (\$host.state\$)
  -t NOTIFICATIONTYPE (\$notification.type\$)

Optional parameters:
  -4 HOSTADDRESS (\$address\$)
  -6 HOSTADDRESS6 (\$address6\$)
  -b NOTIFICATIONAUTHORNAME (\$notification.author\$)
  -c NOTIFICATIONCOMMENT (\$notification.comment\$)
  -i ICINGAWEB2URL (\$notification_icingaweb2url\$, Default: unset)
  -f MAILFROM (\$notification_mailfrom\$, requires GNU mailutils (Debian/Ubuntu) or mailx (RHEL/SUSE))
  -v (\$notification_sendtosyslog\$, Default: false)
  -w NOTES (\$notes\$)
  -x URL (\$notes_url\$)
EOF
}

Help() {
  Usage;
  exit 0;
}

Error() {
  if [ "$1" ]; then
    echo $1
  fi
  Usage;
  exit 1;
}

## Main
while getopts 4:6:b:c:d:f:hi:l:n:o:r:s:t:v:w:x: opt
do
  case "$opt" in
    4) HOSTADDRESS=$OPTARG ;;
    6) HOSTADDRESS6=$OPTARG ;;
    b) NOTIFICATIONAUTHORNAME=$OPTARG ;;
    c) NOTIFICATIONCOMMENT=$OPTARG ;;
    d) LONGDATETIME=$OPTARG ;; # required
    f) MAILFROM=$OPTARG ;;
    h) Help ;;
    i) ICINGAWEB2URL=$OPTARG ;;
    l) HOSTNAME=$OPTARG ;; # required
    n) HOSTDISPLAYNAME=$OPTARG ;; # required
    o) HOSTOUTPUT=$OPTARG ;; # required
    r) USEREMAIL=$OPTARG ;; # required
    s) HOSTSTATE=$OPTARG ;; # required
    t) NOTIFICATIONTYPE=$OPTARG ;; # required
    v) VERBOSE=$OPTARG ;;
    w) NOTES=$OPTARG ;;
    x) URL=$OPTARG ;;
   \?) echo "ERROR: Invalid option -$OPTARG" >&2
       Error ;;
    :) echo "Missing option argument for -$OPTARG" >&2
       Error ;;
    *) echo "Unimplemented option: -$OPTARG" >&2
       Error ;;
  esac
done

shift $((OPTIND - 1))

## Check required parameters (TODO: better error message)
## Keep formatting in sync with mail-service-notification.sh
if [ ! "$LONGDATETIME" ] \
|| [ ! "$HOSTNAME" ] || [ ! "$HOSTDISPLAYNAME" ] \
|| [ ! "$HOSTOUTPUT" ] || [ ! "$HOSTSTATE" ] \
|| [ ! "$USEREMAIL" ] || [ ! "$NOTIFICATIONTYPE" ]; then
  Error "Requirement parameters are missing."
fi

## Build the message's subject
SUBJECT="PROBLEM - $HOSTDISPLAYNAME is $HOSTSTATE!"

## Build the notification message
NOTIFICATION_MESSAGE=`cat << EOF
Info:    $HOSTOUTPUT
When:    $LONGDATETIME
Host:    $HOSTNAME
Notes:   $NOTES
URL:     $URL
EOF
`

#20

I’d say that the line starts with "NOTES: " having no value in many cases.