Skip to content

Instantly share code, notes, and snippets.

@danielhopkins
Last active December 14, 2016 13:55
Show Gist options
  • Select an option

  • Save danielhopkins/7569899 to your computer and use it in GitHub Desktop.

Select an option

Save danielhopkins/7569899 to your computer and use it in GitHub Desktop.
Really simple example of using the victorops api to create incidents
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'uri'
require 'net/http'
# Create a .vorc in your homedirectory.
# The format of the file is json and has keys
# {
# "api_key": "xxxx-xxxx-xxxx-xxxx",
# "use_ssl": true
# }
config_file = "#{ENV['HOME']}/.vorc"
url_path = "integrations/generic/20131114/alert"
unless File.exists?(config_file)
puts "No config file, please create a .vorc in your home directory"
exit 1
end
config = begin
JSON.parse(IO.read(config_file))
rescue
puts "vorc needs to contain json."
exit 1
end
api_key = if config.key?('api_key')
config['api_key']
else
puts 'Please a key "api_key" in your .vorc'
exit 1
end
server = if config.key?('server')
config['server']
else
'alert.victorops.com'
end
use_ssl = if config.key?('use_ssl')
config['use_ssl']
else
true
end
msg = ARGV[0]
payload = {
:message_type => 'INFO',
:state_message => msg,
:originating_host => `hostname`.chomp,
:user => `whoami`.chomp
}
proto = use_ssl ? 'https' : 'http'
uri = URI("#{proto}://#{server}/#{url_path}/#{config['api_key']}/everyone")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = use_ssl
request = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
request.body = JSON.generate(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