Slack Integration for Nagios
Posted: Fri Jul 07, 2017 3:45 pm
So I am following Matthew's instruction on how to integrate Nagios with slack.
http://matthewcmcmillan.blogspot.com/20 ... -with.html
So I followed the instructions...
#!/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
#
# More info on Slack
# Website: https://slack.com/
# Twitter: @slackhq, @slackapi
#
# My info
# Website: http://matthewcmcmillan.blogspot.com/
# Twitter: @matthewmcmillan
#Modify these variables for your environment
MY_NAGIOS_HOSTNAME="nagios.yourdomain.com"
SLACK_HOSTNAME="[TEAMNAME].slack.com"
SLACK_TOKEN="[SLACKKEY]"
SLACK_CHANNEL="#nagios-alertyourslacks"
SLACK_BOTNAME="nagios"
#Set the message icon based on Nagios service state
if [ "$NAGIOS_SERVICESTATE" = "CRITICAL" ]
then
ICON=""
elif [ "$NAGIOS_SERVICESTATE" = "WARNING" ]
then
ICON=""
elif [ "$NAGIOS_SERVICESTATE" = "OK" ]
then
ICON=""
elif [ "$NAGIOS_SERVICESTATE" = "UNKNOWN" ]
then
ICON=""
else
ICON=""
fi
#Send message to Slack
curl -X POST --data "payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"text\": \"${ICON} HOST: ${NAGIOS_HOSTNAME} SERVICE: ${NAGIOS_SERVICEDISPLAYNAME} MESSAGE: ${NAGIOS_SERVICEOUTPUT} <https://${MY_NAGIOS_HOSTNAME}/cgi-bin/nagios3/status.cgi?host=${NAGIOS_HOSTNAME}|See Nagios>\"}" https://${SLACK_HOSTNAME}/services/hooks/incoming-webhook?token=${SLACK_TOKEN}
My commands.cfg
# 'notify-service-by-slack' command definition
define command {
command_name notify-service-by-slack
command_line /usr/local/bin/slack_nagios.sh > /tmp/slack.log 2>&1
}
# 'notify-host-by-slack' command definition
define command {
command_name notify-host-by-slack
command_line /usr/local/bin/slack_nagios.sh > /tmp/slack.log 2>&1
}
My contacts.cfg:
define contact {
contact_name slack
alias Slack
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
}
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin,slack
}
define contactgroup{
contactgroup_name admins-page
alias Nagios Administrators
members nagiosadmin,slack
}
This is what my Notifications look like on Nagios:
paloalto MGT Port Link Status CRITICAL 07-07-2017 10:36:11 slack notify-service-by-slack CRITICAL - Plugin timed out while executing system call
paloalto MGT Port Link Status CRITICAL 07-07-2017 10:36:11 nagiosadmin notify-service-by-email CRITICAL - Plugin timed out while executing system call
paloalto PING CRITICAL 07-07-2017 10:34:49 slack notify-service-by-slack CRITICAL - Host Unreachable (172.16.0.6)
paloalto PING CRITICAL 07-07-2017 10:34:49 nagiosadmin notify-service-by-email CRITICAL - Host Unreachable (172.16.0.6)
I received the email notification, but didn't get any for slack. Any ideas?
http://matthewcmcmillan.blogspot.com/20 ... -with.html
So I followed the instructions...
#!/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
#
# More info on Slack
# Website: https://slack.com/
# Twitter: @slackhq, @slackapi
#
# My info
# Website: http://matthewcmcmillan.blogspot.com/
# Twitter: @matthewmcmillan
#Modify these variables for your environment
MY_NAGIOS_HOSTNAME="nagios.yourdomain.com"
SLACK_HOSTNAME="[TEAMNAME].slack.com"
SLACK_TOKEN="[SLACKKEY]"
SLACK_CHANNEL="#nagios-alertyourslacks"
SLACK_BOTNAME="nagios"
#Set the message icon based on Nagios service state
if [ "$NAGIOS_SERVICESTATE" = "CRITICAL" ]
then
ICON=""
elif [ "$NAGIOS_SERVICESTATE" = "WARNING" ]
then
ICON=""
elif [ "$NAGIOS_SERVICESTATE" = "OK" ]
then
ICON=""
elif [ "$NAGIOS_SERVICESTATE" = "UNKNOWN" ]
then
ICON=""
else
ICON=""
fi
#Send message to Slack
curl -X POST --data "payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"text\": \"${ICON} HOST: ${NAGIOS_HOSTNAME} SERVICE: ${NAGIOS_SERVICEDISPLAYNAME} MESSAGE: ${NAGIOS_SERVICEOUTPUT} <https://${MY_NAGIOS_HOSTNAME}/cgi-bin/nagios3/status.cgi?host=${NAGIOS_HOSTNAME}|See Nagios>\"}" https://${SLACK_HOSTNAME}/services/hooks/incoming-webhook?token=${SLACK_TOKEN}
My commands.cfg
# 'notify-service-by-slack' command definition
define command {
command_name notify-service-by-slack
command_line /usr/local/bin/slack_nagios.sh > /tmp/slack.log 2>&1
}
# 'notify-host-by-slack' command definition
define command {
command_name notify-host-by-slack
command_line /usr/local/bin/slack_nagios.sh > /tmp/slack.log 2>&1
}
My contacts.cfg:
define contact {
contact_name slack
alias Slack
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
}
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin,slack
}
define contactgroup{
contactgroup_name admins-page
alias Nagios Administrators
members nagiosadmin,slack
}
This is what my Notifications look like on Nagios:
paloalto MGT Port Link Status CRITICAL 07-07-2017 10:36:11 slack notify-service-by-slack CRITICAL - Plugin timed out while executing system call
paloalto MGT Port Link Status CRITICAL 07-07-2017 10:36:11 nagiosadmin notify-service-by-email CRITICAL - Plugin timed out while executing system call
paloalto PING CRITICAL 07-07-2017 10:34:49 slack notify-service-by-slack CRITICAL - Host Unreachable (172.16.0.6)
paloalto PING CRITICAL 07-07-2017 10:34:49 nagiosadmin notify-service-by-email CRITICAL - Host Unreachable (172.16.0.6)
I received the email notification, but didn't get any for slack. Any ideas?