Last active
December 11, 2015 08:04
-
-
Save ohsawa0515/c3ddf5e117b69a304882 to your computer and use it in GitHub Desktop.
hubotでicingaの通知をon/offするcoffeeスクリプト
This file contains 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
# Description: | |
# Interact with the Icinga-Web API. | |
# Please set the environment variable. | |
# $ export ICINGA_URL="http://<icinga-url>" | |
# $ export ICINGA_AUTH_KEY="<icinga auth key>" | |
# | |
# Commands: | |
# hubot icinga start all <hosts> - Turn on the notification of the specified hosts of host and services | |
# hubot icinga stop all <hosts> - Turn off the notification of the specified hosts of host and services | |
# hubot icinga start host <hosts> - Turn on the notification of the specified hosts of host | |
# hubot icinga stop host <hosts> - Turn off the notification of the specified hosts of host | |
# hubot icinga start service <hosts> - Turn on the notification of the specified hosts of services | |
# hubot icinga stop service <hosts> - Turn off the notification of the specified hosts of services | |
Array::unique = -> | |
output = {} | |
output[@[key]] = @[key] for key in [0...@length] | |
value for key, value of output | |
module.exports = (robot) -> | |
robot.respond /icinga (start|stop) (host|service|all) (.*)/i, (msg) -> | |
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0' | |
mode = msg.match[1].toLowerCase() # start or stop | |
target = msg.match[2].toLowerCase() # host or service or all | |
objects = msg.match[3].split(" ") | |
switch mode | |
when "start" | |
command_type = "ENABLE" | |
human_command_type = "開始" | |
when "stop" | |
command_type = "DISABLE" | |
human_command_type = "停止" | |
else | |
msg.send "mode not found (start or stop)." | |
process.exit 1 | |
switch target | |
when "host" | |
commands = [ "#{command_type}_HOST_NOTIFICATIONS" ] | |
human_command = "ホストの通知を#{human_command_type}しました" | |
when "service" | |
commands = [ "#{command_type}_HOST_SVC_NOTIFICATIONS" ] | |
human_command = "サービスの通知を#{human_command_type}しました" | |
when "all" | |
commands = [ "#{command_type}_HOST_NOTIFICATIONS", "#{command_type}_HOST_SVC_NOTIFICATIONS" ] | |
human_command = "ホスト・サービスの通知を#{human_command_type}しました" | |
else | |
msg.send "target not found (host or service or all)." | |
process.exit 1 | |
messages = [] | |
for object in objects | |
if !object || object.trim() == '' | |
continue | |
for command in commands | |
request_target = "%5B%7B%22instance%22%3A%22default%22%2C%22host%22%3A%22#{object}%22%7D%5D" | |
request_data = "%7B%22host%22%3A%22#{object}%22%7D" | |
request = robot.http("#{process.env.ICINGA_URL}/cmd/cmd=#{command}/authkey=#{process.env.ICINGA_AUTH_KEY}/target=#{request_target}/data=#{request_data}") | |
.get() | |
request (err, res, body) -> | |
if err or res.statusCode != 200 | |
return msg.send("[#{object}] 更新に失敗しました") | |
messages.push("[#{object}] #{human_command}") | |
msg.send messages.unique().join("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment