Last active
June 14, 2016 13:57
-
-
Save efrenfuentes/7611bfe318f624ed926483b1c33dcdb9 to your computer and use it in GitHub Desktop.
Send Nagios notifications to slack
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
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'uri' | |
require 'json' | |
WEB_HOOK = 'https://hooks.slack.com/services/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' | |
def arguments_to_field | |
fields = {} | |
ARGV.each do |arg| | |
field = arg.split('=') | |
fields[field[0]] = field[1] | |
end | |
fields | |
end | |
def message | |
message = '' | |
arguments_to_field.each do |key, value| | |
message += " #{key}: #{value}" | |
end | |
message.strip | |
end | |
def slack_send | |
uri = URI.parse(WEB_HOOK) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
request = Net::HTTP::Post.new(uri.request_uri) | |
payload = { channel: '#sysadmin', username: 'Nagios', text: message } | |
request.body = payload.to_json | |
response = http.request(request) | |
end | |
slack_send |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment