Created
July 10, 2015 12:03
-
-
Save doublemarket/b21439920dcaf6353a21 to your computer and use it in GitHub Desktop.
チャットワークに通知(日本語)を投げるSensuハンドラ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Sensu Handler: chatwork | |
# | |
# This handler script is used to send notifications to Chatwork rooms. | |
# Chatworkのルームに通知を投げるハンドラです。 | |
# | |
# Input: | |
# @event - Event attributes. | |
# @event['action'] - Property to figure out the event type i.e. whether it is create or resolve. | |
# @event['check'] - Map of attributes from the check config which is calling this handler | |
# @event['client'] - Map of attributes from the client config for the clients from which this event is generated. | |
# option: json_config - By default, assumes the chatwork config parameters are in a file called "chatwork.json" with | |
# "chatwork" being the top-level key of the json. This command line option allows to specify | |
# a custom file instead of "chatwork.json" to fetch the chatwork config from. | |
# | |
# Output: | |
# Hostname and error message with admin gui url | |
require 'rubygems' if RUBY_VERSION < '1.9.0' | |
require 'sensu-handler' | |
require 'timeout' | |
require 'net/https' | |
class ChatworkNotif < Sensu::Handler | |
option :service_name, | |
description: 'Service Name', | |
short: '-s ServiceName', | |
long: '--service_name ServiceName', | |
required: false, | |
default: "" | |
option :json_config, | |
description: 'Config Name', | |
short: '-j JsonConfig', | |
long: '--json_config JsonConfig', | |
required: false | |
def event_name | |
"ホスト名 : #{@event['client']['name']}\n監視内容 : #{@event['check']['name']}" | |
end | |
def post_chatwork_message(api_key, room_id, data) | |
uri = URI("https://api.chatwork.com/v1/rooms/#{room_id}/messages") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
header = { "X-ChatWorkToken" => api_key } | |
body = "body=" + URI.encode(data) | |
res = http.post(uri, body, header) | |
puts JSON.parse(res.body) | |
end | |
def handle | |
json_config = config[:json_config] || 'chatwork' | |
admin_gui = settings[json_config]['admin_gui'] || 'http://localhost:8080/' | |
message = @event['check']['notification'] || @event['check']['output'] | |
if @event['check']['playbook'] | |
message << " Playbook: #{@event['check']['playbook']}" | |
end | |
message << "\n監視サーバ : #{admin_gui}" | |
if @event['action'].eql?('resolve') | |
data = "[info][title]#{config[:service_name]} 解決[/title]#{event_name}\nエラーメッセージ : #{message}[/info]" | |
else | |
data = "[info][title]#{config[:service_name]} 障害発生[/title]#{event_name}\nエラーメッセージ : #{message}[/info]" | |
end | |
post_chatwork_message(settings[json_config]['api_key'], | |
settings[json_config]['room_id'], | |
data) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment