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.

Revisions

  1. danielhopkins revised this gist Nov 21, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion vo.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    #!/usr/bin/env ruby

    require 'rubygems'
    require 'uri'
    require 'net/http'
    require "net/https"
  2. danielhopkins revised this gist Nov 21, 2013. 1 changed file with 13 additions and 10 deletions.
    23 changes: 13 additions & 10 deletions vo.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    #!/usr/bin/env ruby

    require 'rubygems'
    require 'json'
    require 'uri'
    require 'net/http'
    require "net/https"
    @@ -46,16 +45,20 @@

    msg = ARGV[0] || "NO MESSAGE"

    payload = {
    :message_type => options[:msg_type],
    :state_message => msg,
    :originating_host => `hostname`.chomp,
    :user => `whoami`.chomp
    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

    unless options[:entity].nil?
    payload[:entity_id] = options[:entity]
    end

    proto = options[:use_ssl] ? 'https' : 'http'

    @@ -64,7 +67,7 @@
    https.use_ssl = options[:use_ssl]

    request = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
    request.body = JSON.generate(payload)
    request.body = payload
    response = https.request(request)

    if response.code != '200'
  3. danielhopkins revised this gist Nov 20, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions vo.rb
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    require 'json'
    require 'uri'
    require 'net/http'
    require "net/https"
    require 'optparse'

    url_path = "integrations/generic/20131114/alert"
  4. danielhopkins revised this gist Nov 20, 2013. 1 changed file with 38 additions and 37 deletions.
    75 changes: 38 additions & 37 deletions vo.rb
    Original file line number Diff line number Diff line change
    @@ -4,62 +4,63 @@
    require 'json'
    require 'uri'
    require 'net/http'
    require 'optparse'

    # 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
    # }
    url_path = "integrations/generic/20131114/alert"

    config_file = "#{ENV['HOME']}/.vorc"
    url_path = "integrations/generic/20131114/alert"
    options = {}
    opts = OptionParser.new do |opts|
    opts.banner = "Usage: vo -k APIKEY [options] msg"

    unless File.exists?(config_file)
    puts "No config file, please create a .vorc in your home directory"
    exit 1
    end
    opts.on('-k n', '--key APIKEY', 'Api key (required)') do |v|
    options[:key] = v
    end

    config = begin
    JSON.parse(IO.read(config_file))
    rescue
    puts "vorc needs to contain json."
    exit 1
    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

    api_key = if config.key?('api_key')
    config['api_key']
    else
    puts 'Please a key "api_key" in your .vorc'
    exit 1
    end
    options[:use_ssl] = true
    opts.on('--[no-]ssl', 'Enable men in the middle?') do |v|
    options[:use_ssl] = v
    end

    server = if config.key?('server')
    config['server']
    else
    'alert.victorops.com'
    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

    use_ssl = if config.key?('use_ssl')
    config['use_ssl']
    else
    true
    opts.parse!
    if options[:key].nil?
    puts 'Please specify an api-key'
    puts opts
    abort
    end

    msg = ARGV[0]
    msg = ARGV[0] || "NO MESSAGE"

    payload = {
    :message_type => 'INFO',
    :message_type => options[:msg_type],
    :state_message => msg,
    :originating_host => `hostname`.chomp,
    :user => `whoami`.chomp
    }

    proto = use_ssl ? 'https' : 'http'
    unless options[:entity].nil?
    payload[:entity_id] = options[:entity]
    end

    proto = options[:use_ssl] ? 'https' : 'http'

    uri = URI("#{proto}://#{server}/#{url_path}/#{config['api_key']}/everyone")
    uri = URI("#{proto}://#{options[:server]}/#{url_path}/#{options[:key]}/everyone")
    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = use_ssl
    https.use_ssl = options[:use_ssl]

    request = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
    request.body = JSON.generate(payload)
  5. danielhopkins created this gist Nov 20, 2013.
    73 changes: 73 additions & 0 deletions vo.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    #!/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