Skip to content

Instantly share code, notes, and snippets.

@MichaelErmer
Created July 29, 2016 08:39
Show Gist options
  • Save MichaelErmer/f89ddd644299a985d4120f220407f081 to your computer and use it in GitHub Desktop.
Save MichaelErmer/f89ddd644299a985d4120f220407f081 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'net/https'
require 'json'
slack_webhook_url=""
slack_enabled=true
def log(message)
open('/var/log/monit.log', 'a') { |f|
f.puts message
}
end
if slack_enabled
begin
uri = URI.parse(slack_webhook_url)
Net::HTTP.start(uri.host, uri.port, {use_ssl: true}) { |http|
request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' => 'application/json'})
request.body = {
:text => "[#{ENV['MONIT_HOST']}] #{ENV['MONIT_SERVICE']} - #{ENV['MONIT_DESCRIPTION']}"
}.to_json
response = http.request(request)
log("Response from Slack [#{response.code}] [#{response.body}]")
}
rescue Exception => e
log("Exception while calling Slack [#{e.message}]")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment