1#!/usr/bin/env bash 2# Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ 3# Except of function urlencode which is Copyright (C) by Brian White (brian@aljex.com) used under MIT license 4 5PROG="`basename $0`" 6ICINGA2HOST="`hostname`" 7MAILBIN="mail" 8 9if [ -z "`which $MAILBIN`" ] ; then 10 echo "$MAILBIN not found in \$PATH. Consider installing it." 11 exit 1 12fi 13 14## Function helpers 15Usage() { 16cat << EOF 17 18Required parameters: 19 -d LONGDATETIME (\$icinga.long_date_time\$) 20 -l HOSTNAME (\$host.name\$) 21 -n HOSTDISPLAYNAME (\$host.display_name\$) 22 -o HOSTOUTPUT (\$host.output\$) 23 -r USEREMAIL (\$user.email\$) 24 -s HOSTSTATE (\$host.state\$) 25 -t NOTIFICATIONTYPE (\$notification.type\$) 26 27Optional parameters: 28 -4 HOSTADDRESS (\$address\$) 29 -6 HOSTADDRESS6 (\$address6\$) 30 -b NOTIFICATIONAUTHORNAME (\$notification.author\$) 31 -c NOTIFICATIONCOMMENT (\$notification.comment\$) 32 -i ICINGAWEB2URL (\$notification_icingaweb2url\$, Default: unset) 33 -f MAILFROM (\$notification_mailfrom\$, requires GNU mailutils (Debian/Ubuntu) or mailx (RHEL/SUSE)) 34 -v (\$notification_sendtosyslog\$, Default: false) 35 36EOF 37} 38 39Help() { 40 Usage; 41 exit 0; 42} 43 44Error() { 45 if [ "$1" ]; then 46 echo $1 47 fi 48 Usage; 49 exit 1; 50} 51 52urlencode() { 53 local LANG=C i=0 c e s="$1" 54 55 while [ $i -lt ${#1} ]; do 56 [ "$i" -eq 0 ] || s="${s#?}" 57 c=${s%"${s#?}"} 58 [ -z "${c#[[:alnum:].~_-]}" ] || c=$(printf '%%%02X' "'$c") 59 e="${e}${c}" 60 i=$((i + 1)) 61 done 62 echo "$e" 63} 64 65## Main 66while getopts 4:6::b:c:d:f:hi:l:n:o:r:s:t:v: opt 67do 68 case "$opt" in 69 4) HOSTADDRESS=$OPTARG ;; 70 6) HOSTADDRESS6=$OPTARG ;; 71 b) NOTIFICATIONAUTHORNAME=$OPTARG ;; 72 c) NOTIFICATIONCOMMENT=$OPTARG ;; 73 d) LONGDATETIME=$OPTARG ;; # required 74 f) MAILFROM=$OPTARG ;; 75 h) Help ;; 76 i) ICINGAWEB2URL=$OPTARG ;; 77 l) HOSTNAME=$OPTARG ;; # required 78 n) HOSTDISPLAYNAME=$OPTARG ;; # required 79 o) HOSTOUTPUT=$OPTARG ;; # required 80 r) USEREMAIL=$OPTARG ;; # required 81 s) HOSTSTATE=$OPTARG ;; # required 82 t) NOTIFICATIONTYPE=$OPTARG ;; # required 83 v) VERBOSE=$OPTARG ;; 84 \?) echo "ERROR: Invalid option -$OPTARG" >&2 85 Error ;; 86 :) echo "Missing option argument for -$OPTARG" >&2 87 Error ;; 88 *) echo "Unimplemented option: -$OPTARG" >&2 89 Error ;; 90 esac 91done 92 93shift $((OPTIND - 1)) 94 95## Keep formatting in sync with mail-service-notification.sh 96for P in LONGDATETIME HOSTNAME HOSTDISPLAYNAME HOSTOUTPUT HOSTSTATE USEREMAIL NOTIFICATIONTYPE ; do 97 eval "PAR=\$${P}" 98 99 if [ ! "$PAR" ] ; then 100 Error "Required parameter '$P' is missing." 101 fi 102done 103 104## Build the message's subject 105SUBJECT="[$NOTIFICATIONTYPE] Host $HOSTDISPLAYNAME is $HOSTSTATE!" 106 107## Build the notification message 108NOTIFICATION_MESSAGE=`cat << EOF 109***** Host Monitoring on $ICINGA2HOST ***** 110 111$HOSTDISPLAYNAME is $HOSTSTATE! 112 113Info: $HOSTOUTPUT 114 115When: $LONGDATETIME 116Host: $HOSTNAME 117EOF 118` 119 120## Check whether IPv4 was specified. 121if [ -n "$HOSTADDRESS" ] ; then 122 NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE 123IPv4: $HOSTADDRESS" 124fi 125 126## Check whether IPv6 was specified. 127if [ -n "$HOSTADDRESS6" ] ; then 128 NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE 129IPv6: $HOSTADDRESS6" 130fi 131 132## Check whether author and comment was specified. 133if [ -n "$NOTIFICATIONCOMMENT" ] ; then 134 NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE 135 136Comment by $NOTIFICATIONAUTHORNAME: 137 $NOTIFICATIONCOMMENT" 138fi 139 140## Check whether Icinga Web 2 URL was specified. 141if [ -n "$ICINGAWEB2URL" ] ; then 142 NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE 143 144$ICINGAWEB2URL/monitoring/host/show?host=$(urlencode "$HOSTNAME")" 145fi 146 147## Check whether verbose mode was enabled and log to syslog. 148if [ "$VERBOSE" = "true" ] ; then 149 logger "$PROG sends $SUBJECT => $USEREMAIL" 150fi 151 152## Send the mail using the $MAILBIN command. 153## If an explicit sender was specified, try to set it. 154if [ -n "$MAILFROM" ] ; then 155 156 ## Modify this for your own needs! 157 158 ## Debian/Ubuntu use mailutils which requires `-a` to append the header 159 if [ -f /etc/debian_version ]; then 160 /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" | $MAILBIN -a "From: $MAILFROM" -s "$SUBJECT" $USEREMAIL 161 ## Other distributions (RHEL/SUSE/etc.) prefer mailx which sets a sender address with `-r` 162 else 163 /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" | $MAILBIN -r "$MAILFROM" -s "$SUBJECT" $USEREMAIL 164 fi 165 166else 167 /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" \ 168 | $MAILBIN -s "$SUBJECT" $USEREMAIL 169fi 170