From 9a4955d2e2de5759f17a715e40ad0e1dbc199d57 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Mon, 3 Sep 2012 17:34:22 +0200 Subject: [PATCH] Added notify_{host,service}_by_email.sh. These scripts are similar to, but a bit more flexible than, the original notify-*-by-email commands shipped with Nagios. --- notify_host_by_email.sh | 155 +++++++++++++++++++++++++++++++++++ notify_service_by_email.sh | 161 +++++++++++++++++++++++++++++++++++++ 2 files changed, 316 insertions(+) create mode 100755 notify_host_by_email.sh create mode 100755 notify_service_by_email.sh diff --git a/notify_host_by_email.sh b/notify_host_by_email.sh new file mode 100755 index 0000000..c3601bd --- /dev/null +++ b/notify_host_by_email.sh @@ -0,0 +1,155 @@ +#! /bin/bash +# +# Nagios host notification script. +# +# Copyright (C) 2012 Sebastian 'tokkee' Harl +# and teamix GmbH, Nuernberg, Germany +# +# This file is part of "teamix Monitoring Plugins" +# URL: http://oss.teamix.org/projects/monitoringplugins/ +# +# This file is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation, either version 2 of the License, +# or (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this file. If not, see . + +# +# Sample command line: +# notify_host_by_email.sh -T '$NOTIFICATIONTYPE$' -H '$HOSTNAME$' \ +# -a '$HOSTALIAS$' -A '$HOSTADDRESS$' \ +# -s '$HOSTSTATE$' \ +# -o '$HOSTOUTPUT$' -O '$LONGHOSTOUTPUT$' \ +# -P '$HOSTPERFDATA$' -D '$LONGDATETIME$' \ +# -E '$CONTACTEMAIL$' +# + +NOTIFICATIONTYPE="UNKNOWN" + +HOSTNAME="unknown host" +HOSTALIAS="" +HOSTADDRESS="" +HOSTSTATE="UNKNOWN" +HOSTOUTPUT="" +LONGHOSTOUTPUT="" +HOSTPERFDATA="" + +LONGDATETIME=`date` + +CONTACTEMAIL="" + +GETOPT=$( getopt -o T:H:a:A:s:o:O:P:D:E: \ + --long type,hostname,alias,address,state,output,longoutput,perfdata,datetime,email \ + -n 'notify_host_by_email' -- "$@" ) + +if test $? -ne 0; then + echo 'Failed to parse command line options!' >&2 + exit 1 +fi + +eval set -- "$GETOPT" + +while true; do + case "$1" in + -T|--type) + NOTIFICATIONTYPE="$2" + shift 2 + ;; + -H|--hostname) + HOSTNAME="$2" + shift 2 + ;; + -a|--alias) + HOSTALIAS="$2" + shift 2 + ;; + -A|--address) + HOSTADDRESS="$2" + shift 2 + ;; + -s|--state) + HOSTSTATE="$2" + shift 2 + ;; + -o|--output) + HOSTOUTPUT="$2" + shift 2 + ;; + -O|--longoutput) + LONGHOSTOUTPUT="$2" + shift 2 + ;; + -P|--perfdata) + HOSTPERFDATA="$2" + shift 2 + ;; + -D|--datetime) + LONGDATETIME="$2" + shift 2 + ;; + -E|--email) + CONTACTEMAIL="$2" + shift 2 + ;; + --) + shift + break + ;; + esac +done + +if test -z "$CONTACTEMAIL"; then + echo 'Missing contact email!' >&2 + exit 1 +fi + +EMAIL=$( mktemp -t ) +trap "rm -f $EMAIL" EXIT + +function email_append() { + /usr/bin/printf "%b" "$@" "\n" >> $EMAIL +} + +cat < $EMAIL +***** Nagios ***** + +Notification Type: $NOTIFICATIONTYPE + +Host: $HOSTNAME ($HOSTALIAS) +EOF + +if test -n "$HOSTADDRESS"; then + email_append "Address: $HOSTADDRESS" +fi + +email_append "State: $HOSTSTATE" +if test -n "$HOSTOUTPUT"; then + email_append "Info: $HOSTOUTPUT" +fi + +email_append "" +email_append "Date/Time: $LONGDATETIME" + +if test -n "$LONGHOSTOUTPUT"; then + email_append "" + email_append "Additional Info:" + email_append "" + email_append "$LONGHOSTOUTPUT" +fi + +if test -n "$HOSTPERFDATA"; then + email_append "" + email_append "Performance Data:" + email_append "" + email_append "$HOSTPERFDATA" +fi + +cat $EMAIL | /usr/bin/mail -s "** $NOTIFICATIONTYPE Host Alert: $HOSTNAME is $HOSTSTATE **" $CONTACTEMAIL + diff --git a/notify_service_by_email.sh b/notify_service_by_email.sh new file mode 100755 index 0000000..ca2447f --- /dev/null +++ b/notify_service_by_email.sh @@ -0,0 +1,161 @@ +#! /bin/bash +# +# Nagios service notification script. +# +# Copyright (C) 2012 Sebastian 'tokkee' Harl +# and teamix GmbH, Nuernberg, Germany +# +# This file is part of "teamix Monitoring Plugins" +# URL: http://oss.teamix.org/projects/monitoringplugins/ +# +# This file is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation, either version 2 of the License, +# or (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this file. If not, see . + +# +# Sample command line: +# notify_service_by_email.sh -T '$NOTIFICATIONTYPE$' -H '$HOSTNAME$' \ +# -a '$HOSTALIAS$' -A '$HOSTADDRESS$' \ +# -S '$SERVICEDESC$' -s '$SERVICESTATE$' \ +# -o '$SERVICEOUTPUT$' -O '$LONGSERVICEOUTPUT$' \ +# -P '$SERVICEPERFDATA$' -D '$LONGDATETIME$' \ +# -E '$CONTACTEMAIL$' +# + +NOTIFICATIONTYPE="UNKNOWN" + +HOSTNAME="unknown host" +HOSTALIAS="" +HOSTADDRESS="" +SERVICEDESC="unknown service" +SERVICESTATE="UNKNOWN" +SERVICEOUTPUT="" +LONGSERVICEOUTPUT="" +SERVICEPERFDATA="" + +LONGDATETIME=`date` + +CONTACTEMAIL="" + +GETOPT=$( getopt -o T:H:a:A:S:s:o:O:P:D:E: \ + --long type,hostname,alias,address,service,state,output,longoutput,perfdata,datetime,email \ + -n 'notify_host_by_email' -- "$@" ) + +if test $? -ne 0; then + echo 'Failed to parse command line options!' >&2 + exit 1 +fi + +eval set -- "$GETOPT" + +while true; do + case "$1" in + -T|--type) + NOTIFICATIONTYPE="$2" + shift 2 + ;; + -H|--hostname) + HOSTNAME="$2" + shift 2 + ;; + -a|--alias) + HOSTALIAS="$2" + shift 2 + ;; + -A|--address) + HOSTADDRESS="$2" + shift 2 + ;; + -S|--service) + SERVICEDESC="$2" + shift 2 + ;; + -s|--state) + SERVICESTATE="$2" + shift 2 + ;; + -o|--output) + SERVICEOUTPUT="$2" + shift 2 + ;; + -O|--longoutput) + LONGSERVICEOUTPUT="$2" + shift 2 + ;; + -P|--perfdata) + SERVICEPERFDATA="$2" + shift 2 + ;; + -D|--datetime) + LONGDATETIME="$2" + shift 2 + ;; + -E|--email) + CONTACTEMAIL="$2" + shift 2 + ;; + --) + shift + break + ;; + esac +done + +if test -z "$CONTACTEMAIL"; then + echo 'Missing contact email!' >&2 + exit 1 +fi + +EMAIL=$( mktemp -t ) +trap "rm -f $EMAIL" EXIT + +function email_append() { + /usr/bin/printf "%b" "$@" "\n" >> $EMAIL +} + +cat < $EMAIL +***** Nagios ***** + +Notification Type: $NOTIFICATIONTYPE + +Service: $SERVICEDESC +Host: $HOSTNAME ($HOSTALIAS) +EOF + +if test -n "$HOSTADDRESS"; then + email_append "Address: $HOSTADDRESS" +fi + +email_append "State: $SERVICESTATE" +if test -n "$SERVICEOUTPUT"; then + email_append "Info: $SERVICEOUTPUT" +fi + +email_append "" +email_append "Date/Time: $LONGDATETIME" + +if test -n "$LONGSERVICEOUTPUT"; then + email_append "" + email_append "Additional Info:" + email_append "" + email_append "$LONGSERVICEOUTPUT" +fi + +if test -n "$SERVICEPERFDATA"; then + email_append "" + email_append "Performance Data:" + email_append "" + email_append "$SERVICEPERFDATA" +fi + +cat $EMAIL | /usr/bin/mail -s "** $NOTIFICATIONTYPE Service Alert: $HOSTNAME/$SERVICEDESC is $SERVICESTATE **" $CONTACTEMAIL +