NagiosでAWS障害情報(AWS Service Health Dashboard)を監視する

AWSの障害情報はAWS Service Health Dashboardにて公開されています。この情報をNagiosに連携して監視する方法です。

Nagiosプラグイン

rubyでPlugin作成します。こちらのスクリプトを改造して作っています。shebangは環境に合わせて調整してください。

GemのNokogiri は別途インストールしておいてください。

Nagios plugin to check AWS Status RSS feeds · GitHub

#!/usr/bin/env ruby
require 'nokogiri'
require 'net/http'

EXIT_OK       = 0
EXIT_WARNING  = 1
EXIT_CRITICAL = 2
EXIT_UNKNOWN  = 3

if ARGV[0].nil? || ARGV[0] == '-h'
  puts %{
USAGE:
  #{$0} url
E.g.:
  #{$0} http://status.aws.amazon.com/rss/ec2-ap-northeast-1.rss
Returns an approriate response based on the most recent status message
}
  exit EXIT_UNKNOWN
end


begin
  url = ARGV[0]

  xml_data = Net::HTTP.get_response(URI.parse(url)).body
  xml_doc  = Nokogiri::XML.parse(xml_data)
  item = xml_doc.xpath('rss/channel/item')
  if item.count == 0
    puts "No error information provided"
    exit EXIT_OK
  end

  latest_status = item.xpath("title").first.text
  puts "#{latest_status}:#{item.xpath('description').first.text}"

  case latest_status
  when /^service is operating normally/i, /Informational message/i
    exit EXIT_OK
  when /^performance issues/i
    exit EXIT_WARNING
  when /^service disruption/i
    exit EXIT_CRITICAL
  else
    exit EXIT_UNKNOWN
  end

rescue
  puts "Error fetching status: #{$!}"
  exit EXIT_UNKNOWN
end

RSSのURLを取得

引数として渡すのはRSSのURLです。このURLはAWS Service Health DashboardRSSボタンから取得できるのですが、いちいちクリックするの面倒なので、コマンドで一気に取得します。後は適当に grep かければ必要なサービスのRSSのURLを絞り込めます。

何のサービスのURLかは名前から容易に判断できると思います。

curl http://status.aws.amazon.com/ |grep rss |sed -e "s/^.*href=\"\(.*\)\">.*$/http:\/\/status.aws.amazon.com\1/g"
http://status.aws.amazon.com/rss/apigateway-us-west-2.rss
http://status.aws.amazon.com/rss/appstream-us-east-1.rss
http://status.aws.amazon.com/rss/cloudfront.rss
http://status.aws.amazon.com/rss/cloudsearch-us-west-1.rss
http://status.aws.amazon.com/rss/cloudsearch-us-east-1.rss
http://status.aws.amazon.com/rss/cloudsearch-us-west-2.rss
http://status.aws.amazon.com/rss/cloudwatch-us-west-1.rss
http://status.aws.amazon.com/rss/cloudwatch-us-east-1.rss
http://status.aws.amazon.com/rss/cloudwatch-us-west-2.rss
http://status.aws.amazon.com/rss/cloudwatchevents-us-west-1.rss
http://status.aws.amazon.com/rss/cloudwatchevents-us-east-1.rss
〜省略〜

監視設定例

設定ファイルの切り方によりますが、以下で雰囲気わかると思います。

define service{
        use                             cloud-service
        hostgroup_name                  aws-service
        service_description             Amazon EC2 Tokyo
        check_command                   check_aws_service!http://status.aws.amazon.com/rss/ec2-ap-northeast-1.rss
}
define hostgroup{
  hostgroup_name  aws-service
}

ホストが実在しないので check_dummyプラグイン使うと良いです。

define command{
        command_name    check_aws_service
        command_line    $USER1$/check_aws_service $ARG1$
        }

define command{
        command_name    check_none
        command_line    $USER1$/check_dummy 0
        }
define host{
        use                     test
        host_name               AWS
        hostgroups              aws-service
        check_command           check_none
        }

監視画面イメージ

実際に設定してみたNagiosの監視画面はこんな感じ。

f:id:yomon8:20160829222804p:plain