]> granicus.if.org Git - icinga2/commitdiff
Add notification command parameters to the default mail notification script
authorMarianne M. Spiller <github@spiller.me>
Wed, 5 Apr 2017 10:29:23 +0000 (12:29 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Tue, 9 May 2017 15:53:52 +0000 (17:53 +0200)
Replace the previous notification scheme - now using getops in place of ENV
variables, giving the ability to integrate with the Icinga Director.

refs #5170

Signed-off-by: Michael Friedrich <michael.friedrich@icinga.com>
etc/icinga2/conf.d/commands.conf
etc/icinga2/conf.d/notifications.conf
etc/icinga2/conf.d/templates.conf
etc/icinga2/scripts/mail-host-notification.sh
etc/icinga2/scripts/mail-service-notification.sh

index e97b087afeb89767d225ebfdcfdfbe4766d87b93..d40fb84e8c887cc961bb996c96d74a0a3303c037 100644 (file)
 object NotificationCommand "mail-host-notification" {
   command = [ SysconfDir + "/icinga2/scripts/mail-host-notification.sh" ]
 
-  env = {
-    NOTIFICATIONTYPE = "$notification.type$"
-    HOSTALIAS = "$host.display_name$"
-    HOSTADDRESS = "$address$"
-    HOSTSTATE = "$host.state$"
-    LONGDATETIME = "$icinga.long_date_time$"
-    HOSTOUTPUT = "$host.output$"
-    NOTIFICATIONAUTHORNAME = "$notification.author$"
-    NOTIFICATIONCOMMENT = "$notification.comment$"
-    HOSTDISPLAYNAME = "$host.display_name$"
-    USEREMAIL = "$user.email$"
+  arguments += {
+    "-4" = {
+      required = true
+      value = "$notification_address$"
+    }
+    "-6" = "$notification_address6$"
+    "-b" = "$notification_author$"
+    "-c" = "$notification_comment$"
+    "-d" = {
+      required = true
+      value = "$notification_date$"
+    }
+    "-f" = "$notification_from$"
+    "-i" = "$notification_icingaweb2url$"
+    "-l" = {
+      required = true
+      value = "$notification_hostname$"
+    }
+    "-n" = {
+      required = true
+      value = "$notification_hostdisplayname$"
+    }
+    "-o" = {
+      required = true
+      value = "$notification_hostoutput$"
+    }
+    "-r" = {
+      required = true
+      value = "$notification_useremail$"
+    }
+    "-s" = {
+      required = true
+      value = "$notification_hoststate$"
+    }
+    "-t" = {
+      required = true
+      value = "$notification_type$"
+    }
+    "-v" = "$notification_logtosyslog$"
+  }
+
+  vars += {
+    notification_address = "$address$"
+    notification_address6 = "$address6$"
+    notification_author = "$notification.author$"
+    notification_comment = "$notification.comment$"
+    notification_type = "$notification.type$"
+    notification_date = "$icinga.long_date_time$"
+    notification_hostname = "$host.name$"
+    notification_hostdisplayname = "$host.display_name$"
+    notification_hostoutput = "$host.output$"
+    notification_hoststate = "$host.state$"
+    notification_useremail = "$user.email$"
   }
 }
 
 object NotificationCommand "mail-service-notification" {
   command = [ SysconfDir + "/icinga2/scripts/mail-service-notification.sh" ]
 
-  env = {
-    NOTIFICATIONTYPE = "$notification.type$"
-    SERVICEDESC = "$service.name$"
-    HOSTALIAS = "$host.display_name$"
-    HOSTADDRESS = "$address$"
-    SERVICESTATE = "$service.state$"
-    LONGDATETIME = "$icinga.long_date_time$"
-    SERVICEOUTPUT = "$service.output$"
-    NOTIFICATIONAUTHORNAME = "$notification.author$"
-    NOTIFICATIONCOMMENT = "$notification.comment$"
-    HOSTDISPLAYNAME = "$host.display_name$"
-    SERVICEDISPLAYNAME = "$service.display_name$"
-    USEREMAIL = "$user.email$"
+  arguments += {
+    "-4" = {
+      required = true
+      value = "$notification_address$"
+        }
+    "-6" = "$notification_address6$"
+    "-b" = "$notification_author$"
+    "-c" = "$notification_comment$"
+    "-d" = {
+      required = true
+      value = "$notification_date$"
+    }
+    "-e" = {
+      required = true
+      value = "$notification_servicename$"
+    }
+    "-f" = "$notification_from$"
+    "-i" = "$notification_icingaweb2url$"
+    "-l" = {
+      required = true
+      value = "$notification_hostname$"
+    }
+    "-n" = {
+      required = true
+      value = "$notification_hostdisplayname$"
+    }
+    "-o" = {
+      required = true
+      value = "$notification_serviceoutput$"
+    }
+    "-r" = {
+      required = true
+      value = "$notification_useremail$"
+    }
+    "-s" = {
+      required = true
+      value = "$notification_servicestate$"
+    }
+    "-t" = "$notification_type$"
+    "-u" = {
+      required = true
+      value = "$notification_servicedisplayname$"
+    }
+    "-v" = "$notification_logtosyslog$"
   }
-}
 
+  vars += {
+    notification_address = "$address$"
+    notification_address6 = "$address6$"
+    notification_author = "$notification.author$"
+    notification_comment = "$notification.comment$"
+    notification_type = "$notification.type$"
+    notification_date = "$icinga.long_date_time$"
+    notification_hostname = "$host.name$"
+    notification_hostdisplayname = "$host.display_name$"
+    notification_servicename = "$service.name$"
+    notification_serviceoutput = "$service.output$"
+    notification_servicestate = "$service.state$"
+    notification_useremail = "$user.email$"
+    notification_servicedisplayname = "$service.display_name$"
+  }
+}
index 73f0f5f66334a74e55107bacc7f2fd5d48cda3fa..62057b9374e4c624f5e8e82ce1f2b4277f884711 100644 (file)
 
 apply Notification "mail-icingaadmin" to Host {
   import "mail-host-notification"
-
   user_groups = host.vars.notification.mail.groups
   users = host.vars.notification.mail.users
+  interval = 2h
 
+  vars.notification_logtosyslog = true
   assign where host.vars.notification.mail
 }
 
 apply Notification "mail-icingaadmin" to Service {
   import "mail-service-notification"
-
   user_groups = host.vars.notification.mail.groups
   users = host.vars.notification.mail.users
+  interval = 2h
 
+  vars.notification_logtosyslog = true
   assign where host.vars.notification.mail
 }
index 3588f7fcfffbb4109fdda1ef5a879e5dc805b4d5..1a8ea8328d9b10ee87b4fa792f78c6d157803865 100644 (file)
@@ -51,6 +51,12 @@ template Notification "mail-host-notification" {
             FlappingStart, FlappingEnd,
             DowntimeStart, DowntimeEnd, DowntimeRemoved ]
 
+  vars += {
+    // notification_icingaweb2url = "https://www.example.com/icingaweb2"
+    // notification_from = "Icinga 2 Host Monitoring <icinga@example.com>"
+    notification_logtosyslog = false
+  }
+
   period = "24x7"
 }
 
@@ -62,11 +68,16 @@ template Notification "mail-host-notification" {
 template Notification "mail-service-notification" {
   command = "mail-service-notification"
 
-  states = [ OK, Warning, Critical, Unknown ]
+  states = [ Critical, OK, Unknown, Warning ]
   types = [ Problem, Acknowledgement, Recovery, Custom,
             FlappingStart, FlappingEnd,
             DowntimeStart, DowntimeEnd, DowntimeRemoved ]
 
+  vars += {
+    // notification_icingaweb2url = "https://www.example.com/icingaweb2"
+    // notification_from = "Icinga 2 Service Monitoring <icinga@example.com>"
+    notification_logtosyslog = false
+  }
+
   period = "24x7"
 }
-
index e1ac7166486af5992519b0ce19052459a43706aa..b67b3db6b1a3b6293f50dbd235a4754c68a495ab 100755 (executable)
 #!/bin/sh
-template=`cat <<TEMPLATE
-***** Icinga  *****
 
-Notification Type: $NOTIFICATIONTYPE
+PROG="`basename $0`"
+HOSTNAME="`hostname`"
+MAILBIN="mail"
 
-Host: $HOSTALIAS
-Address: $HOSTADDRESS
-State: $HOSTSTATE
+if [ -z "`which $MAILBIN`" ] ; then
+  echo "$MAILBIN not in \$PATH. Consider installing it."
+  exit 1
+fi
 
-Date/Time: $LONGDATETIME
+Usage() {
+cat << EOF
 
-Additional Info: $HOSTOUTPUT
+The following are mandatory:
+  -4 HOSTADDRESS (\$address$)
+  -6 HOSTADDRESS6 (\$address6$)
+  -d LONGDATETIME (\$icinga.long_date_time$)
+  -l HOSTALIAS (\$host.name$)
+  -n HOSTDISPLAYNAME (\$host.display_name$)
+  -o HOSTOUTPUT (\$host.output$)
+  -r USEREMAIL (\$user.email$)
+  -s HOSTSTATE (\$host.state$)
+  -t NOTIFICATIONTYPE (\$notification.type$)
 
-Comment: [$NOTIFICATIONAUTHORNAME] $NOTIFICATIONCOMMENT
-TEMPLATE
+And these are optional:
+  -b NOTIFICATIONAUTHORNAME (\$notification.author$)
+  -c NOTIFICATIONCOMMENT (\$notification.comment$)
+  -i ICINGAWEB2URL (\$notification_icingaweb2url$, Default: unset)
+  -f MAILFROM (\$notification_mailfrom$, requires GNU mailutils)
+  -v (\$notification_sendtosyslog$, Default: false)
+
+EOF
+
+exit 1;
+}
+
+while getopts 4:6::b:c:d:f:hi:l:n:o:r:s:t:v: opt
+do
+  case "$opt" in
+    4) HOSTADDRESS=$OPTARG ;;
+    6) HOSTADDRESS6=$OPTARG ;;
+    b) NOTIFICATIONAUTHORNAME=$OPTARG ;;
+    c) NOTIFICATIONCOMMENT=$OPTARG ;;
+    d) LONGDATETIME=$OPTARG ;;
+    f) MAILFROM=$OPTARG ;;
+    h) Usage ;;
+    i) ICINGAWEB2URL=$OPTARG ;;
+    l) HOSTALIAS=$OPTARG ;;
+    n) HOSTDISPLAYNAME=$OPTARG ;;
+    o) HOSTOUTPUT=$OPTARG ;;
+    r) USEREMAIL=$OPTARG ;;
+    s) HOSTSTATE=$OPTARG ;;
+    t) NOTIFICATIONTYPE=$OPTARG ;;
+    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))
+
+## Build the message's subject
+SUBJECT="[$NOTIFICATIONTYPE] Host $HOSTDISPLAYNAME is $HOSTSTATE!"
+
+## Build the notification message
+NOTIFICATION_MESSAGE=`cat << EOF
+***** Icinga 2 Host Monitoring on $HOSTNAME *****
+
+==> $HOSTDISPLAYNAME ($HOSTALIAS) is $HOSTSTATE! <==
+
+Info?    $HOSTOUTPUT
+
+When?    $LONGDATETIME
+Host?    $HOSTALIAS (aka "$HOSTDISPLAYNAME")
+IPv4?   $HOSTADDRESS
+EOF
 `
 
-/usr/bin/printf "%b" "$template" | mail -s "$NOTIFICATIONTYPE - $HOSTDISPLAYNAME is $HOSTSTATE" $USEREMAIL
+## Is this host IPv6 capable? Put its address into the message.
+if [ -n "$HOSTADDRESS6" ] ; then
+  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
+IPv6?   $HOSTADDRESS6"
+fi
+
+## Are there any comments? Put them into the message.
+if [ -n "$NOTIFICATIONCOMMENT" ] ; then
+  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
+
+Comment by $NOTIFICATIONAUTHORNAME:
+  $NOTIFICATIONCOMMENT"
+fi
+
+## Are we using Icinga Web 2? Put the URL into the message.
+if [ -n "$ICINGAWEB2URL" ] ; then
+  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
+
+Get live status:
+  $ICINGAWEB2URL/monitoring/host/show?host=$HOSTALIAS"
+fi
+
+## Are we verbose? Then put a message to syslog.
+if [ "$VERBOSE" == "true" ] ; then
+  logger "$PROG sends $SUBJECT => $USEREMAIL"
+fi
 
+## And finally: send the message using $MAILBIN command.
+## Do we have an explicit sender? Then we'll use it.
+if [ -n "$MAILFROM" ] ; then
+  /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" \
+  | $MAILBIN -a "From: $MAILFROM" -s "$SUBJECT" $USEREMAIL
+else
+  /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" \
+  | $MAILBIN -s "$SUBJECT" $USEREMAIL
+fi
index 8574036b306acf6c20294bd976322c28a249de84..5b490527e638158e667dbde333be553c76eafe9b 100755 (executable)
 #!/bin/sh
-template=`cat <<TEMPLATE
-***** Icinga  *****
 
-Notification Type: $NOTIFICATIONTYPE
+PROG="`basename $0`"
+HOSTNAME="`hostname`"
+MAILBIN="mail"
 
-Service: $SERVICEDESC
-Host: $HOSTALIAS
-Address: $HOSTADDRESS
-State: $SERVICESTATE
+if [ -z "`which $MAILBIN`" ] ; then
+  echo "$MAILBIN not in \$PATH. Consider installing it."
+  exit 1
+fi
 
-Date/Time: $LONGDATETIME
+Usage() {
+cat << EOF
 
-Additional Info: $SERVICEOUTPUT
+The following are mandatory:
+  -4 HOSTADDRESS (\$address$)
+  -6 HOSTADDRESS (\$address6$)
+  -d LONGDATETIME (\$icinga.long_date_time$)
+  -e SERVICENAME (\$service.name$)
+  -l HOSTALIAS (\$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$)
 
-Comment: [$NOTIFICATIONAUTHORNAME] $NOTIFICATIONCOMMENT
-TEMPLATE
+And these are optional:
+  -b NOTIFICATIONAUTHORNAME (\$notification.author$)
+  -c NOTIFICATIONCOMMENT (\$notification.comment$)
+  -i ICINGAWEB2URL (\$notification_icingaweb2url$, Default: unset)
+  -f MAILFROM (\$notification_mailfrom$, requires GNU mailutils)
+e -v (\$notification_sendtosyslog$, Default: false)
+
+EOF
+exit 1;
+}
+
+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 ;;
+    e) SERVICENAME=$OPTARG ;;
+    f) MAILFROM=$OPTARG ;;
+    h) Usage ;;
+    i) ICINGAWEB2URL=$OPTARG ;;
+    l) HOSTALIAS=$OPTARG ;;
+    n) HOSTDISPLAYNAME=$OPTARG ;;
+    o) SERVICEOUTPUT=$OPTARG ;;
+    r) USEREMAIL=$OPTARG ;;
+    s) SERVICESTATE=$OPTARG ;;
+    t) NOTIFICATIONTYPE=$OPTARG ;;
+    u) SERVICEDISPLAYNAME=$OPTARG ;;
+    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))
+
+## Build the message's subject
+SUBJECT="[$NOTIFICATIONTYPE] $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!"
+
+## Build the notification message
+NOTIFICATION_MESSAGE=`cat << EOF
+***** Icinga 2 Service Monitoring on $HOSTNAME *****
+
+==> $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE! <==
+
+Info?    $SERVICEOUTPUT
+
+When?    $LONGDATETIME
+Service? $SERVICENAME (aka "$SERVICEDISPLAYNAME")
+Host?    $HOSTALIAS (aka "$HOSTDISPLAYNAME")
+IPv4?   $HOSTADDRESS
+EOF
 `
 
-/usr/bin/printf "%b" "$template" | mail -s "$NOTIFICATIONTYPE - $HOSTDISPLAYNAME - $SERVICEDISPLAYNAME is $SERVICESTATE" $USEREMAIL
+## Is this host IPv6 capable? Put its address into the message.
+if [ -n "$HOSTADDRESS6" ] ; then
+  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
+IPv6?    $HOSTADDRESS6"
+fi
+
+## Are there any comments? Put them into the message.
+if [ -n "$NOTIFICATIONCOMMENT" ] ; then
+  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
+
+Comment by $NOTIFICATIONAUTHORNAME:
+  $NOTIFICATIONCOMMENT"
+fi
+
+## Are we using Icinga Web 2? Put the URL into the message.
+if [ -n "$ICINGAWEB2URL" ] ; then
+  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
+
+Get live status:
+  $ICINGAWEB2URL/monitoring/service/show?host=$HOSTALIAS&service=$SERVICENAME"
+fi
+
+## Are we verbose? Then put a message to syslog.
+if [ "$VERBOSE" == "true" ] ; then
+  logger "$PROG sends $SUBJECT => $USEREMAIL"
+fi
 
+## And finally: send the message using $MAILBIN command.
+## Do we have an explicit sender? Then we'll use it.
+if [ -n "$MAILFROM" ] ; then
+  /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" \
+  | $MAILBIN -a "From: $MAILFROM" -s "$SUBJECT" $USEREMAIL
+else
+  /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" \
+  | $MAILBIN -s "$SUBJECT" $USEREMAIL
+fi