Skip to content

Instantly share code, notes, and snippets.

@tsettle
Created June 13, 2021 02:35
Show Gist options
  • Save tsettle/9f78bb6e3376634a17331b3c938ef500 to your computer and use it in GitHub Desktop.
Save tsettle/9f78bb6e3376634a17331b3c938ef500 to your computer and use it in GitHub Desktop.
Bash script to send notifications from Nagios to MS Teams
#!/bin/bash
#
# It is what it is. Might be some better ways to do some things, but
# I never claimed to be a proficient at shell scripts.
#
# Enjoy.
#
NAGIOS_URL="undefined"
WEBHOOK_URL="undefined"
usage() { echo "Usage: $0 -w <WEBHOOK_URL> -h <NAGIOS_URL>" 1>&2; exit 1; }
while getopts ":h:w:t" o; do
case "${o}" in
w)
WEBHOOK_URL=${OPTARG}
;;
h)
NAGIOS_URL=${OPTARG}
;;
t)
TEST_MODE=1
;;
*)
usage
;;
esac
done
# Declare some colors to use.
declare -A THEME
THEME[ACK]="0275d8"
THEME[OK]="5cb85c"
THEME[WARNING]="f0ad4e"
THEME[UNKNOWN]="f7f7f7"
THEME[CRITICAL]="d9534f"
THEME[UP]="5cb85c"
THEME[DOWN]="d9534f"
THEME[UNREACHABLE]="f7f7f7"
# Move some informatino around in case this is a host alert
if [ -z ${NAGIOS_SERVICESTATE} ]; then
NAGIOS_SERVICEDESC="Host Status"
NAGIOS_SERVICESTATE="${NAGIOS_HOSTSTATE}"
NAGIOS_SERVICEOUTPUT="${NAGIOS_HOSTOUTPUT}"
NAGIOS_SERVICEINFOURL="${NAGIOS_HOSTINFOURL}"
fi
# Don't know enough to even comment on this, but it works.
if [ -z ${NAGIOS_SERVICEINFOURL} ]; then
NAGIOS_SERVICEINFOURL="${NAGIOS_URL}/nagios/"
fi
# I don't need this, but you might...
if [ -z ${NAGIOS_HOSTALIAS} ]; then
NAGIOS_HOSTALIAS="${NAGIOS_HOSTNAME}"
fi
# The whole reason I wrote this, was to handle Ack notices. The
# perl script I was using didn't do ACks for some reason.
echo "TYPE: ${NAGIOS_NOTIFICATIONTYPE}"
if [ ${NAGIOS_NOTIFICATIONTYPE} == "ACKNOWLEDGEMENT" ]; then
COLOR=${THEME[ACK]}
NAGIOS_NOTIFICATIONTYPE="ACK"
else
COLOR=${THEME[${NAGIOS_SERVICESTATE}]}
NAGIOS_NOTIFICATIONTYPE=${NAGIOS_SERVICESTATE}
fi
TITLE="${NAGIOS_NOTIFICATIONTYPE} - ${NAGIOS_HOSTALIAS} / ${NAGIOS_SERVICEDESC}"
TEXT="${NAGIOS_SERVICESTATE} - ${NAGIOS_SERVICEOUTPUT}"
FACTS="{\"name\":\"Host Address\",\"value\":\"${NAGIOS_HOSTADDRESS}\"}"
# Add some more facts...
#
# FACTS="${FACTS},{\"name\":\"Host Address\",\"value\":\"${NAGIOS_HOSTADDRESS}\"}"
#
SECTIONS="\"sections\":[{\"facts\":[${FACTS}]}]"
# Create some buttons. You can only have 4 I think.
#
# I'm using Apache's Redirect directive so that I can make URI's that point
# to things that MS doesn't like.
#
# Redirect RedirectMatch "^/ssh/(.+)" "ssh://$1"
# RedirectMatch "^/https/(.+)" "https://$1"
#
BUTTONS="{\"@type\":\"OpenUri\",\"name\":\"View in Nagios\",\"targets\":[{\"os\":\"default\",\"uri\":\"${NAGIOS_SERVICEINFOURL}\"}]}"
BUTTONS="${BUTTONS},{\"@type\":\"OpenUri\",\"name\":\"Host WebUI\",\"targets\":[{\"os\":\"default\",\"uri\":\"${NAGIOS_URL}/https/${NAGIOS_HOSTADDRESS}\"}]}"
BUTTONS="${BUTTONS},{\"@type\":\"OpenUri\",\"name\":\"Host SSH\",\"targets\":[{\"os\":\"default\",\"uri\":\"${NAGIOS_URL}/ssh/${NAGIOS_HOSTADDRESS}\"}]}"
ACTIONS="\"potentialAction\":[${BUTTONS}]"
# Convert formating.
MESSAGE=$( echo "${TEXT}" | sed 's/"/\"/g' | sed "s/'/\'/g" )
# Create the JSON blob
JSON="{\"Summary\":\"${TITLE}\",\"title\":\"${TITLE}\",\"text\":\"${MESSAGE}\",\"themeColor\":\"${COLOR}\",${SECTIONS},${ACTIONS}}"
# Post to Microsoft Teams.
echo "${JSON}" >> /tmp/json.tmp.$(date +%Y%m%d-%H%M%S)
if [ ${TEST_MODE} ]; then
echo ${JSON}
else
curl -H "Content-Type: application/json" -d "${JSON}" "${WEBHOOK_URL}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment