LoginSignup
1
1

More than 5 years have passed since last update.

mackerelでworking以外のステータスのホストをslackに通知させる

Last updated at Posted at 2016-09-28

概要

  • mackerelでworking以外のステータスになってるやつを見逃すことあるよね
  • なのでmkrで定期的にslackに通知させましょう
  • でも意図的にstandbyにするやつは除外ルールに書いちゃいます

内容

①必要なgemのインストール

$ gem install slack-notifier

②slackに通知させるプログラム

$ vim /tmp/check_mackerel_status.rb
# -*- 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

③除外するホストを格納する部分

$ vim /tmp/exclude_hosts.rb
# mackerel上のホスト名と理由を書いてます
$un_watch = [
  ['server1', '10月上旬に削除します'],
  ['server2', '10月上旬に削除します']
]

④あとはcronで動かしてね!

$ crontab -e
0 10 * * 1-5 ruby /tmp/check_mackerel_status.rb
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1