DACではサーバ・NW機器のリソースの監視に「Nagios」を利用してます。
監視ホストも数百台規模で増え続けており、アラート検知が非常に重要な課題となってきています。
メールの通知では、各MLを通知先に設定して、フィルタ分けして...と何かと面倒ですね。
そこで「slackへアラート内容を飛ばして、プッシュ通知で受け取る。」
アラートの通知先としてslackにて通知を受けれるよう、slack×Nagios連携してみました。
slackとは?
slackとは、2014年に正式に公開された北米発のコミュニケーションツールです。
近年では、コミュニケーションツールを選択する上で、slackを選択する企業も増えてきているようです。
機能は
- チャット
- ファイル添付
- 検索
- 画像動画共有
- コード共有
など、単なるチャットツールではなくなってきています。
また、マルチプラットフォーム「スマホ(iOS/android)」「PC(Windows/Mac)」なのも、使いやすいツールの要因といえるでしょう。
最近では、「Googleカレンダー」と連携を発表し、ますます連携面での強化を図っているようです。
当社でも、コミュニケーションツールとして、slackやHipchatを利用しております。
slackの魅力を話す上で欠かせないのは、なんといっても、「外部サービス連携」。
当社でも「Git Hub」「Redmine」「Jenkins」など様々な外部サービスと連携しております。
連携しやすい点は大きな魅力ですね。
Nagios×slack連携イメージ
Nagiosで検知したアラートをメールではなく、slackのChannelへ流します。
各プロジェクトのChannelへ流すことで、メールの仕分けや、通知先設定(宛て先追加)も不要となります。
変更前:Nagios×メール
変更後:Nagios×slack
Nagios×slack 連携手順
1.slack側からNagiosの統合設定を有効化。
2.NagiosがslackにAPI連携するためのTokenを発行。
※ここのTokenの値「hogehogehogehoge」はNagiosサーバの変数に入れる必要があるため、控えておく。
3.Nagios用Channel作成
slack側に通知先となるChannelを事前に作成しておく。
ここでは#Nagiosという名前で作成する。
4.slack連携用プログラム取得
Nagiosサーバにてslack連携用プログラム(nagios.pl)を取得する。
# wget https://raw.github.com/tinyspeck/services-examples/master/nagios.pl # cp nagios.pl /usr/local/bin/slack_nagios.pl # chmod 755 /usr/local/bin/slack_nagios.pl
5.ドメイン名とTokenを変数に設定
66,67行目のドメイン名とTokenの値を変数に設定する。
ドメイン名には、Slackチームドメイン名、Tokenには2で発行した値を設定します。
# vi /usr/local/bin/slack_nagios.pl
[shell]
!/usr/bin/perl
Copyright 2013 Tiny Speck, Inc
#
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
#
http://www.apache.org/licenses/LICENSE-2.0
#
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#
A nagios/icinga plugin for sending alerts to Slack. See more documentation on the team services page at:
https://my.slack.com/services/new/nagios
#
Requires these perl modules:
HTTP::Request
LWP::UserAgent
#
I am not a perl programmer. Beware.
#
An example Nagios config:
#
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 command {
command_name notify-service-by-slack
command_line /usr/local/bin/slack_nagios.pl -field slack_channel=#alerts
}
#
define command {
command_name notify-host-by-slack
command_line /usr/local/bin/slack_nagios.pl -field slack_channel=#ops
}
#
use warnings; use strict;
use Getopt::Long; use HTTP::Request::Common qw(POST); use HTTP::Status qw(is_client_error); use LWP::UserAgent;
#
Customizable vars. Set these to the information for your team
#
my $opt_domain = "test.slack.com"; # Slackチームドメイン名 my $opt_token = "hogehogehogehoge"; # Slackで発行したToken
#
Get command-line opts
#
my %opt_fields; GetOptions("field=s%" => \%opt_fields);
#
DO THINGS
#
my %event;
Get all Nagios variables
while ((my $k, my $v) = each %ENV) { next unless $k =~ /^(?:NAGIOS|ICINGA)_(.*)$/; $event{$1} = $v; }
Merge in passed-in variables
%event = (%event, %opt_fields);
$event{"slack_version"} = "1.1";
#
Make the request
#
my $ua = LWP::UserAgent->new; $ua->timeout(15);
my $req = POST("https://${opt_domain}/services/hooks/nagios?token=${opt_token}", \%event);
my $s = $req->as_string; print STDERR "Request:\n$s\n";
my $resp = $ua->request($req); $s = $resp->as_string; print STDERR "Response:\n$s\n";
[/shell]
6.Nagios設定ファイル変更
1.contacts.cfgへコンタクトの定義を追記する。
# vi /usr/local/nagios/etc/objects/contacts.cfg
[shell]
slack
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 } [/shell]
2.commands.cfgへコマンド定義を追記する。
# vi /usr/local/nagios/etc/objects/commands.cfg
[shell]
slack
define command { command_name notify-service-by-slack command_line /usr/local/bin/slack_nagios.pl -field slack_channel=#nagios }
define command { command_name notify-host-by-slack command_line /usr/local/bin/slack_nagios.pl -field slack_channel=#nagios } [/shell]
これだけで、設定完了です!
7.slackの通知画面
Criticalを赤色、Warningを黄色、Recoveryを緑色で分けて通知してくれるため、
一目でアラート状況を把握することができます。
Nagios×slackの連携にてslackにてアラート通知を行うことができました。
スマホのアプリでプッシュ通知を入れておくだけで、通知も問題ありません。
通知先を個別で設定する必要がないので、思ったより簡単に連携ができました。