Last active
May 10, 2024 19:18
-
-
Save tony-caffe/2d9df8ca53b3ec153d86e0476409bac5 to your computer and use it in GitHub Desktop.
Nagios to post alerts into a Slack channel # using the Incoming WebHooks integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script is used by Nagios to post alerts into a Slack channel | |
# using the Incoming WebHooks integration. Create the channel, botname | |
# and integration first and then add this notification script in your | |
# Nagios configuration. | |
# | |
# All variables that start with NAGIOS_ are provided by Nagios as | |
# environment variables when an notification is generated. | |
# A list of the env variables is available here: | |
# http://nagios.sourceforge.net/docs/3_0/macrolist.html | |
# | |
# My info | |
# Website: bytesunlimited.com | |
# GitHub: https://github.com/BytesUnlimited | |
# | |
#### Config Examples: | |
# | |
## Commands Definition: | |
# # 'notify-host-by-slack' command definition | |
# define command{ | |
# command_name notify-host-by-slack | |
# command_line /usr/lib64/nagios/plugins/nagios-slack-alert.sh "$HOSTNAME$" "N/A" "$HOSTSTATE$" "$HOSTOUTPUT$" "$NOTIFICATIONTYPE$" | |
# } | |
# | |
# # 'notify-service-by-slack' command definition | |
# define command{ | |
# command_name notify-service-by-slack | |
# command_line /usr/lib64/nagios/plugins/nagios-slack-alert.sh "$HOSTNAME$" "$SERVICEDESC$" "$SERVICESTATE$" "$SERVICEOUTPUT$" "$NOTIFICATIONTYPE$" | |
# | |
## Contacts Example: | |
# define contact { | |
# contact_name slack-default | |
# alias Slack Default | |
# service_notification_period 24x7 | |
# host_notification_period 24x7 | |
# service_notification_options w,u,c,r | |
# host_notification_options d,r | |
# service_notification_commands notify-service-by-slack | |
# host_notification_commands notify-host-by-slack | |
# } | |
# | |
## Contact Group Example: | |
# define contactgroup{ | |
# contactgroup_name slacknotificationsgroup | |
# alias Slack Default Notifications | |
# members slack-default | |
# } | |
# | |
## Service Example: | |
# define service{ | |
# use default-service | |
# service_description Check Example Command Description | |
# check_command check_example_command | |
# contact_groups slacknotificationsgroup | |
# host_name server-name | |
# } | |
# | |
#Modify these variables for your environment | |
MY_NAGIOS_HOSTNAME="MY-IP-OR-Domain-Name" | |
MY_NAGIOS_STATUS_URL="https://${MY_NAGIOS_HOSTNAME}/nagios/cgi-bin/status.cgi?host=${NAGIOS_HOSTNAME}" | |
SLACK_HOSTNAME="SLACK-EXTENSION.slack.com" | |
SLACK_CHANNEL="#CHANNEL-NAME" | |
SLACK_BOTNAME="Nagios Alerts" | |
WEBHOOKS_INTEGRATION_URL="https://hooks.slack.com/services/CHANGE-ME/CHANGE-ME/CHANGE-ME" | |
NAGIOS_SERVICESTATE="@3" | |
#Set the message icon based on Nagios service state | |
if [ "$NAGIOS_SERVICESTATE" = "CRITICAL" ] | |
then | |
ICON=":banging-head:" | |
COLOR="danger" | |
elif [ "$NAGIOS_SERVICESTATE" = "WARNING" ] | |
then | |
ICON=":face_with_head_bandage:" | |
COLOR="warning" | |
elif [ "$NAGIOS_SERVICESTATE" = "OK" ] | |
then | |
ICON=":excellent:" | |
COLOR="good" | |
elif [ "$NAGIOS_SERVICESTATE" = "UNKNOWN" ] | |
then | |
ICON=":question:" | |
COLOR="#00FFFF" | |
else | |
ICON=":facepalm:" | |
COLOR="#FFFFFF" | |
fi | |
#Send message to Slack using Incoming WebHooks Integration: | |
curl -X POST --data "payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_BOTNAME}\", \"icon_url\": \"https://slack.com/img/services/nagios_48.png\", \"text\": \"${ICON} HOST: ${1} SERVICE: ${2} MESSAGE: ${4} <${MY_NAGIOS_STATUS_URL}|See Nagios>\"}" $WEBHOOKS_INTEGRATION_URL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment