Add notification command parameters to the default mail notification script

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>
This commit is contained in:
Marianne M. Spiller 2017-04-05 12:29:23 +02:00 committed by Michael Friedrich
parent 0d380d0a4f
commit 82a5c5a624
5 changed files with 354 additions and 52 deletions

View File

@ -3,36 +3,124 @@
object NotificationCommand "mail-host-notification" { object NotificationCommand "mail-host-notification" {
command = [ SysconfDir + "/icinga2/scripts/mail-host-notification.sh" ] command = [ SysconfDir + "/icinga2/scripts/mail-host-notification.sh" ]
env = { arguments += {
NOTIFICATIONTYPE = "$notification.type$" "-4" = {
HOSTALIAS = "$host.display_name$" required = true
HOSTADDRESS = "$address$" value = "$notification_address$"
HOSTSTATE = "$host.state$" }
LONGDATETIME = "$icinga.long_date_time$" "-6" = "$notification_address6$"
HOSTOUTPUT = "$host.output$" "-b" = "$notification_author$"
NOTIFICATIONAUTHORNAME = "$notification.author$" "-c" = "$notification_comment$"
NOTIFICATIONCOMMENT = "$notification.comment$" "-d" = {
HOSTDISPLAYNAME = "$host.display_name$" required = true
USEREMAIL = "$user.email$" 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" { object NotificationCommand "mail-service-notification" {
command = [ SysconfDir + "/icinga2/scripts/mail-service-notification.sh" ] command = [ SysconfDir + "/icinga2/scripts/mail-service-notification.sh" ]
env = { arguments += {
NOTIFICATIONTYPE = "$notification.type$" "-4" = {
SERVICEDESC = "$service.name$" required = true
HOSTALIAS = "$host.display_name$" value = "$notification_address$"
HOSTADDRESS = "$address$" }
SERVICESTATE = "$service.state$" "-6" = "$notification_address6$"
LONGDATETIME = "$icinga.long_date_time$" "-b" = "$notification_author$"
SERVICEOUTPUT = "$service.output$" "-c" = "$notification_comment$"
NOTIFICATIONAUTHORNAME = "$notification.author$" "-d" = {
NOTIFICATIONCOMMENT = "$notification.comment$" required = true
HOSTDISPLAYNAME = "$host.display_name$" value = "$notification_date$"
SERVICEDISPLAYNAME = "$service.display_name$" }
USEREMAIL = "$user.email$" "-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$"
} }
} }

View File

@ -10,18 +10,20 @@
apply Notification "mail-icingaadmin" to Host { apply Notification "mail-icingaadmin" to Host {
import "mail-host-notification" import "mail-host-notification"
user_groups = host.vars.notification.mail.groups user_groups = host.vars.notification.mail.groups
users = host.vars.notification.mail.users users = host.vars.notification.mail.users
interval = 2h
vars.notification_logtosyslog = true
assign where host.vars.notification.mail assign where host.vars.notification.mail
} }
apply Notification "mail-icingaadmin" to Service { apply Notification "mail-icingaadmin" to Service {
import "mail-service-notification" import "mail-service-notification"
user_groups = host.vars.notification.mail.groups user_groups = host.vars.notification.mail.groups
users = host.vars.notification.mail.users users = host.vars.notification.mail.users
interval = 2h
vars.notification_logtosyslog = true
assign where host.vars.notification.mail assign where host.vars.notification.mail
} }

View File

@ -51,6 +51,12 @@ template Notification "mail-host-notification" {
FlappingStart, FlappingEnd, FlappingStart, FlappingEnd,
DowntimeStart, DowntimeEnd, DowntimeRemoved ] 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" period = "24x7"
} }
@ -62,11 +68,16 @@ template Notification "mail-host-notification" {
template Notification "mail-service-notification" { template Notification "mail-service-notification" {
command = "mail-service-notification" command = "mail-service-notification"
states = [ OK, Warning, Critical, Unknown ] states = [ Critical, OK, Unknown, Warning ]
types = [ Problem, Acknowledgement, Recovery, Custom, types = [ Problem, Acknowledgement, Recovery, Custom,
FlappingStart, FlappingEnd, FlappingStart, FlappingEnd,
DowntimeStart, DowntimeEnd, DowntimeRemoved ] 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" period = "24x7"
} }

View File

@ -1,20 +1,119 @@
#!/bin/sh #!/bin/sh
template=`cat <<TEMPLATE
***** Icinga *****
Notification Type: $NOTIFICATIONTYPE PROG="`basename $0`"
HOSTNAME="`hostname`"
MAILBIN="mail"
Host: $HOSTALIAS if [ -z "`which $MAILBIN`" ] ; then
Address: $HOSTADDRESS echo "$MAILBIN not in \$PATH. Consider installing it."
State: $HOSTSTATE 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 And these are optional:
TEMPLATE -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

View File

@ -1,21 +1,123 @@
#!/bin/sh #!/bin/sh
template=`cat <<TEMPLATE
***** Icinga *****
Notification Type: $NOTIFICATIONTYPE PROG="`basename $0`"
HOSTNAME="`hostname`"
MAILBIN="mail"
Service: $SERVICEDESC if [ -z "`which $MAILBIN`" ] ; then
Host: $HOSTALIAS echo "$MAILBIN not in \$PATH. Consider installing it."
Address: $HOSTADDRESS exit 1
State: $SERVICESTATE 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 And these are optional:
TEMPLATE -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