Skip to content

Instantly share code, notes, and snippets.

@cabecada
Created September 22, 2016 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cabecada/ad262f20095ba7190de2b7f0e85073f4 to your computer and use it in GitHub Desktop.
Save cabecada/ad262f20095ba7190de2b7f0e85073f4 to your computer and use it in GitHub Desktop.
stash_to_silenced_migrate
#!/opt/sensu/embedded/bin/ruby
require 'net/http'
require 'json'
module Sensu
class Api
attr_reader :server, :port
def initialize(server='localhost', port=4567)
@server = server
@port = port
@http = Net::HTTP.new(@server, @port)
# @http.set_debug_output($stdout)
end
def headers
{'api-proxy' => 'true', 'Content-Type' => 'application/json'}
end
def http
@http ||= Net::HTTP.new(@server, @port)
# @http.set_debug_output($stdout)
@http
end
def make_request(action, path, headers, content=false)
if action == "get"
req = Net::HTTP::Get.new(URI.encode(path), headers)
elsif action == "post"
req = Net::HTTP::Post.new(URI.encode(path), headers)
req.body = content.to_json
elsif action == "delete"
req = Net::HTTP::Delete.new(URI.encode(path), headers)
else
raise 'Invalid Request'
end
begin
http.request(req)
rescue Timeout::Error
puts 'HTTP request has timed out.'
exit 1
rescue StandardError => e
puts 'An HTTP error occurred'
puts e
exit 1
end
end
def get_request(path)
make_request('get', path, headers)
end
def post_request(path, content)
make_request('post', path, headers, content)
end
def delete_request(path)
make_request('delete', path, headers)
end
def parse_response(res)
JSON.parse(res.body)
end
def stashes
return @stashes if @stashes
res = get_request('/stashes')
if res.code == '200'
@stashes ||= parse_response(res)
else
puts "Failed with #{res.code}, #{res.body}"
exit 1
end
end
def silenced
res = get_request('/silenced')
if res.code == '200'
parse_response(res)
else
puts "Failed with #{res.code}, #{res.body}"
exit 1
end
end
def delete_all_stashes
stashes.each do |stash|
delete_stash(stash['path'])
end
end
def delete_stash(path)
path = '/' + path unless path.start_with?('/')
res = delete_request('/stashes' + path)
if res.code == '204'
puts "The stash at #{path} was successfully deleted."
else
puts "stash at #{path} could not be deleted. Failed with #{res.code}, #{res.body}"
end
end
def create_silenced(content)
res = post_request('/silenced', content)
silenced_id = content.fetch(:id)
if res.code == '201'
puts "The silenced entry created for #{silenced_id}"
else
puts "silence entry for #{silenced_id} could not be created. Failed with #{res.code}, #{res.body}"
end
end
def stash_to_silenced
stashes.each do |stash|
_, client, check = stash['path'].split('/')
content = {}
content.merge!(:subscription => "client:#{client}")
content.merge!(:check => check) if !check.nil?
content.merge!(:expire => stash['content'].fetch('expire', 3600)) if stash['content'].key?('expire')
content.merge!(:reason => "testing")
content.merge!(:creator => "vjain")
content.merge!(:expire_on_resolve => false)
subscription = content.fetch(:subscription, "*")
check = content.fetch(:check, "*")
silenced_id = "#{subscription}:#{check}"
content.merge!(:id => silenced_id)
create_silenced(content)
end
end
def run
stash_to_silenced
delete_all_stashes
end
end
end
def stash_to_silence_migrate
Sensu::Api.new.run
end
stash_to_silence_migrate
@cabecada
Copy link
Author

    # silenced flag is set to true only for clients with subscriptions:, hence silenced is true for puppet_agent_service but not for test991
    #172.16.5.3:6379> hgetall "events:test" 
    #1) "test991"
    #2) "{\"client\":{\"name\":\"test\",\"address\":\"unknown\",\"subscriptions\":[],\"keepalives\":false,\"version\":\"0.26.1\"},\"check\":{\"name\":\"test991\",\"command\":\"whoami\",\"status\":2,\"output\":\"nok\",\"source\":\"test\",\"pid\":\"1234\",\"timeout\":10,\"duration\":10,\"subscription\":[\"client:test\"],\"executed\":1474573131,\"issued\":1474573131,\"type\":\"standard\",\"origin\":\"ubuntu1404-5.vagrant.local\",\"history\":[\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"0\",\"2\",\"0\",\"2\",\"0\",\"2\"],\"total_state_change\":0},\"occurrences\":1,\"occurrences_watermark\":1,\"action\":\"create\",\"timestamp\":1474573131,\"id\":\"0bd20705-34a0-4b22-b747-7b464c60d079\",\"last_ok\":1474573131,\"last_state_change\":1474573131,\"silenced\":false,\"silenced_by\":[]}"
    #        
    #1) "puppet_agent_service"
    #2) "{\"client\":{\"name\":\"ubuntu1404-2.vagrant.local\",\"address\":\"172.16.5.2\",\"subscriptions\":[\"client:ubuntu1404-2.vagrant.local\"],\"redact\":[\"password\",\"access_key\",\"secret_key\"],\"socket\":{\"bind\":\"127.0.0.1\",\"port\":3030},\"safe_mode\":true,\"rabbitmq\":{\"password\":\"REDACTED\"},\"role\":\"roles::sensu::rabbitmq\",\"component_id\":\"sensu_rabbitmq\",\"environment\":\"vagrant\",\"region\":\"vagrant\",\"owner\":\"ops_infrastructure\",\"notification_group\":\"ops_infrastructure\",\"system_metrics\":\"http://dashboard-qa.otenv.com/dashboard/db/system-metrics-sensu_rabbitmq-vagrant-vagrant\",\"keepalive\":{\"team\":\"ops_infrastructure\",\"realert_every\":\"-1\"},\"version\":\"0.26.1\",\"timestamp\":1474573632},\"check\":{\"command\":\"check-process.rb -p 'puppet agent'\",\"dependencies\":[],\"handlers\":[\"default\"],\"interval\":600,\"runbook\":\"https://wiki.otcorp.opentable.com/\",\"team\":\"dcops\",\"alert_after\":1200,\"realert_every\":\"-1\",\"page\":false,\"ticket\":false,\"standalone\":true,\"timeout\":600,\"name\":\"puppet_agent_service\",\"issued\":1474573648,\"executed\":1474573648,\"duration\":0.1,\"output\":\"CheckProcess CRITICAL: Found 0 matching processes; cmd /puppet agent/\\n\",\"status\":2,\"type\":\"standard\",\"history\":[\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\"],\"total_state_change\":0},\"occurrences\":37,\"occurrences_watermark\":37,\"action\":\"create\",\"timestamp\":1474573648,\"id\":\"73935d8b-83db-4077-a810-095571dda31a\",\"last_state_change\":1474544760,\"last_ok\":1474544760,\"silenced\":true,\"silenced_by\":[\"client:ubuntu1404-2.vagrant.local:puppet_agent_service\"]}"

@cabecada
Copy link
Author

creating silence entry for stash at silence/test/test992
silenced entry created for client:test:test992
creating silence entry for stash at silence/test/test991
silenced entry created for client:test:test991
stash at /silence/test/test992 was successfully deleted.
stash at /silence/test/test991 was successfully deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment