Last active
December 14, 2016 13:55
-
-
Save danielhopkins/7569899 to your computer and use it in GitHub Desktop.
Really simple example of using the victorops api to create incidents
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 | |
require 'uri' | |
require 'net/http' | |
require "net/https" | |
require 'optparse' | |
url_path = "integrations/generic/20131114/alert" | |
options = {} | |
opts = OptionParser.new do |opts| | |
opts.banner = "Usage: vo -k APIKEY [options] msg" | |
opts.on('-k n', '--key APIKEY', 'Api key (required)') do |v| | |
options[:key] = v | |
end | |
options[:server] = 'alert.victorops.com' | |
opts.on('-s', '--server SERVER_NAME', 'An alternate server to send alerts to.') do |v| | |
options[:server] = v | |
end | |
options[:use_ssl] = true | |
opts.on('--[no-]ssl', 'Enable men in the middle?') do |v| | |
options[:use_ssl] = v | |
end | |
options[:msg_type] = :info | |
opts.on('--type TYPE', [:info, :warning, :critical, :acknowledgement, :recovery], 'select message type (info, warning, critical, acknowledgement, recovery)') do |v| | |
options[:msg_type] = v | |
end | |
opts.on('--entity ENTITY', 'entity id, used for incident aggregation') do |v| | |
options[:entity] = v | |
end | |
end | |
opts.parse! | |
if options[:key].nil? | |
puts 'Please specify an api-key' | |
puts opts | |
abort | |
end | |
msg = ARGV[0] || "NO MESSAGE" | |
entity_id = unless options[:entity].nil? | |
",\"entity_id\": \"#{options[:entity]}\"" | |
end | |
payload = <<-eos | |
{ | |
"message_type": "#{options[:msg_type]}", | |
"state_message": "#{msg}", | |
"originating_host": "#{`hostname`.chomp}", | |
"user": "#{`whoami`.chomp}" | |
#{entity_id} | |
} | |
eos | |
proto = options[:use_ssl] ? 'https' : 'http' | |
uri = URI("#{proto}://#{options[:server]}/#{url_path}/#{options[:key]}/everyone") | |
https = Net::HTTP.new(uri.host, uri.port) | |
https.use_ssl = options[:use_ssl] | |
request = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'}) | |
request.body = payload | |
response = https.request(request) | |
if response.code != '200' | |
puts "POST failed with #{response.body}" | |
exit 1 | |
else | |
exit 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment