Nagios から Slack channel へ通知する設定方法

Nagios からのアラート通知を Slack の Incoming WebHooks を利用して Channel へ書き込む設定をしました。

slack

Slack には Nagios の Integration が用意されているので、普通はこれ使えば良さそうです。

しかし、今回は Lite プランに抑えたいという理由で、色々とインテグレーションを使ってると 5 external integrations の上限に引っかかってしまうのを避けるために Incoming WebHooks を使って自前で実装しました。Incoming WebHooks の Webhook URL を発行するのは上限ないみたいなので重宝しています。

早速、シェルスクリプトを書こうと思ったんですが、ググったら Gist にコード公開している人が居たので fork してちょっと書き換えて使うことにしました。

下記が、gist に公開しているシェルスクリプトの内容です。

#!/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
 
#Modify these variables for your environment
MY_NAGIOS_HOSTNAME="nagios.yourdomain.com"
MY_NAGIOS_STATUS_URL="http://${MY_NAGIOS_HOSTNAME}/nagios/cgi-bin/status.cgi?host=${NAGIOS_HOSTNAME}"
SLACK_HOSTNAME="yourslack.slack.com"
SLACK_TOKEN="xyxyxyourslackkey"
SLACK_CHANNEL="#alert"
SLACK_BOTNAME="Nagios"
 
 
#Set the message icon based on Nagios service state
if [ "$NAGIOS_SERVICESTATE" = "CRITICAL" ]
then
    ICON=":exclamation:"
    COLOR="danger"
elif [ "$NAGIOS_SERVICESTATE" = "WARNING" ]
then
    ICON=":warning:"
    COLOR="warning"
elif [ "$NAGIOS_SERVICESTATE" = "OK" ]
then
    ICON=":white_check_mark:"
    COLOR="good"
elif [ "$NAGIOS_SERVICESTATE" = "UNKNOWN" ]
then
    ICON=":question:"
    COLOR="#00FFFF"
else
    ICON=":white_medium_square:"
    COLOR="#FFFFFF"
fi
 
#Send message to Slack
curl -X POST --data "payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_BOTNAME}\", \"icon_url\": \"https://slack.com/img/services/nagios_48.png\", \"text\": \"${ICON} HOST: ${NAGIOS_HOSTNAME}   SERVICE: ${NAGIOS_SERVICEDISPLAYNAME}     MESSAGE: ${NAGIOS_SERVICEOUTPUT} <${MY_NAGIOS_STATUS_URL}|See Nagios>\"}" https://${SLACK_HOSTNAME}/services/hooks/incoming-webhook?token=${SLACK_TOKEN}

これを Nagios を動かしているサーバーの plugins 以下とかに設置して、実行できるようにします。

chmod +x /usr/lib64/nagios/plugins/slack_nagios.sh

そして、Nagios commands に slack_nagios.sh を実行する定義を追加します。

vim /etc/nagios/objects/commands.cfg
 
define command {
    command_name    notify-service-by-slack
    command_line    /usr/lib64/nagios/plugins/slack_nagios.sh
}

contacts にも Slack を追加します。

vim /etc/nagios/objects/contacts.cfg
 
define contact {
  contact_name slack
  use generic-contact
  service_notification_commands   notify-service-by-slack
  email   /dev/null
}

あとは Slack の #alert channel に通知したいサービスに登録してあげて、設定確認と reload すれば完了です。

service nginx configtest
service nagios reload

Nagios のアラートを Slack へテスト通知してみた結果は下記のような感じです。

slack-nagios

Slack はチャンネル毎にアプリで通知を受け取るか設定できるので、これで夜中に Nagios からアラートきても気付けて、即対応できて幸せですね!