Webhookについて
下記の記事でも取り上げましたが、Canned-Catfood Gamingでは最近Discordを導入しました。
このDiscordの特徴として、Slackやその他のサービスと同様に、各種アプリケーションとの連携に
「Webhook」を利用できます。
Webhookの基本的な概念としては、公開されているAPIに対してHTTPリクエストを送ることにより
Discordなどのアプリケーションに特定の動作をさせる点にあります。
Discordでの利用
例えば curlを利用して対象のURLにPOSTすると:
curl -X POST --data '{ "content": "**test** post"}' https://discordapp.com/api/webhooks/{webhook.id}/{webhook.token}

この様にDiscordに投稿されます。({webhook.id}/{webhook.token}には任意の値が入ります。)
リクエストデータの書式としては、Discord Developer Documentsに詳細が書いてありますが、簡単に紹介すると
| フィールド | タイプ | 詳細 | |
|---|---|---|---|
| content | string | メッセージ内容(最大、恐らく1byteで2000字、2byteで1000字)。 | |
| username | string | webhookのユーザー名を上書きする。 | |
| avatar_url | string | webhookのアバター画像を上書きする。 | |
| tts | bool | TTSメッセージの場合Trueにセットする。 | |
| file | file contents | ファイルを送付する場合にはファイル名 | |
| embeds | array of embed objects | 埋め込みコンテンツの内容 |
等が使用できます。(詳細は https://discordapp.com/developers/docs/resources/webhook より。)
今回は、これを利用してNagiosのアラート内容をDiscordにWebhookを利用して流したいと思います。
NagiosとのWebhook連携
NagiosからSlackには、既にGISTにて公開されているPlugin(https://gist.github.com/bakorer/199dd38779440d2f7980)を利用することで
連携することができます。
Discord側でもAPIのURL末尾に”/slack”を付加することにより、Slack形式のWebhookを利用することができますが
折角なので今回はDiscord側の仕様に合わせて調整します。
前提
OSはUbuntu server 16.04LTS、Apache2.4、Nagios3の環境を想定しています。
Nagios3はUbuntuの公式レポジトリより導入したものをVirtual Host下で運用する都合上、一部設定のみ下記configで書き換えています。
DocumentRoot /usr/share/nagios3/htdocs
Alias /stylesheets /etc/nagios3/stylesheets
<Directory /etc/nagios3/stylesheets>
order deny,allow
Allow from all
</Directory>
# Alias / /usr/share/nagios3/htdocs
<Directory /usr/share/nagios3/htdocs>
order deny,allow
Allow from all
</Directory>
ScriptAlias /cgi-bin /usr/lib/cgi-bin
<Directory /usr/lib/cgi-bin/nagios3>
order deny,allow
Allow from all
</Directory>
cgiのパスは/cgi-bin、/stylesheetsは/stylesheetsになっています。(標準ではそれぞれ/nagios/cgi-bin、/nagios/stylesheetsかと思います。)
DiscordのAPIにPOSTするスクリプトの作成
Shellですが、libcurlが必須となります。
#!/bin/bash
#
MY_NAGIOS_HOSTNAME="nagios.canned-catfood.com"
MY_NAGIOS_STATUS_URL="http://${MY_NAGIOS_HOSTNAME}/cgi-bin/status.cgi?host=${NAGIOS_HOSTNAME}"
# 発行したWebhookURLのhttps://discordapp.com/api/webhooks/{webhook.id}/{webhook.token}を入れてください。
DISCORD_ID=""
DISCORD_TOKEN=""
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
curl -X POST --data "{ \"content\": \"${ICON} HOST: ${NAGIOS_HOSTNAME} SERVICE: ${NAGIOS_SERVICEDISPLAYNAME} MESSAGE: ${NAGIOS_SERVICEOUTPUT}\"}" https://discordapp.com/api/webhooks/${DISCORD_ID}/${DISCORD_TOKEN}
今回は他のNagios Pluginと同ディレクトリ(/usr/lib/nagios/plugins)に保存しました。
Commands.cfgの編集
Nagios上から実行可能にする為、commands.cfgに既定のcommandとして設定します。
define command {
command_name notify-service-by-discord
command_line /usr/lib/nagios/plugins/discord_nagios.sh
}
(最終行に追記してください。)
Contacts.cfgの編集
define contact {
contact_name discord
alias Nagios Discord
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-discord
host_notification_commands notify-host-by-email
email /dev/null
}
CONTACTSの括りに追記してください。
今回はService Notificationのみ使用する為、host_notification_commandsをnotify-host-by-emailに設定したうえで
メールの送付先を/dev/nullに設定し破棄しています。
以上で、DiscordとNagios3の連携は完了です。
設定に問題がなければ、Nagios上でServiceにAlertが発報されると、Discordの特定チャンネルにAlertが流れます。
お好みでcontent内のメッセージ内容に@hereや@channelを混ぜると、携帯で通知として受け取れると思います。