# -*- coding:utf-8 -*-
# need:
# $ gem install slack-notifier
#
# $ vim /tmp/exclude_hosts.rb
# $un_watch = [
# ['server1', '10月上旬に削除します'],
# ['server2', '10月上旬に削除します']
# ]
#
# $ crontab -e
# 0 10 * * 1-5 ruby /tmp/check_mackerel_status.rb
require 'json'
require 'slack-notifier'
require '/tmp/exclude_hosts'
exclude_host = []
host_list = []
message = ''
message_exclude = ''
webhook_url = 'https://hooks.slack.com/services/xxxxxxxxxxxxxxxxxxxxxxxx'
# mackerelからデータを取得
cmd = `mkr hosts`
JSON.load(cmd).each do |t|
host_list.push(t) if t['status'] != 'working'
end
# 意図的に監視してるホスト一覧
$un_watch.each do |t|
exclude_host.push(t[0])
message_exclude += "#{t[0]}: #{t[1]}\n"
end
# 意図しないworkingステータス以外のホストを取得
host_list.each do |t|
unless exclude_host.include?(t['name'])
message += "#{t['status']} : #{t['name']}\n"
end
end
unless message.empty?
notifier = Slack::Notifier.new webhook_url, channel: '#infra-channel', username: 'botname'
notifier.ping "```\nmackerelでworking以外のサーバ一覧です\n#{message}```\n"
end