Skip to content

Instantly share code, notes, and snippets.

@robertlabrie
Created July 14, 2016 14:41
Show Gist options
  • Save robertlabrie/d9939a78079afefff403cf7323c98ab3 to your computer and use it in GitHub Desktop.
Save robertlabrie/d9939a78079afefff403cf7323c98ab3 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'uri'
require 'json'
# get the current status DB
begin
data = File.read('/tmp/sensudata')
data = JSON.parse(data)
rescue
data = {}
end
# what time is it?
time = Time.now.to_i
# get the list of checks from sensu
uri = URI('http://sensu:4567/checks')
res = Net::HTTP.get_response(uri)
checks = JSON.parse(res.body)
# now enumerate the checks
checks.each do |c|
name = c['name']
interval = c['interval']
last = data[name].to_i
puts "#{name} --> #{time} - #{last} = #{time - last} >= #{interval}?\n"
if time - last >= interval
puts "I should check #{name}"
data[name] = time # set the data for this check to now
#### ATROCITY ALERT ###
# Even though I have a post URI, and even though Net:HTTP can parse it
# as demonstrated by get_response, I still have to instantitate an
# http object with the host and port? Really? Thanks Ruby.
# php4life -- REL
req = Net::HTTP::Post.new('http://sensu:4567/request', initheader = {'Content-Type' =>'application/json'})
req.body = {check: name}.to_json
res = Net::HTTP.start('sensu', 4567) do |http|
http.request(req)
end
end
end
File.write('/tmp/sensudata', JSON.generate(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment