Skip to content

Instantly share code, notes, and snippets.

@mattfitzgerald
Created August 24, 2011 03:41
Show Gist options
  • Save mattfitzgerald/1167255 to your computer and use it in GitHub Desktop.
Save mattfitzgerald/1167255 to your computer and use it in GitHub Desktop.
def calculate_downtime_minutes
check_id = 387085
user = "***"
pass = "***"
key = "***"
today = DateTime.now.to_date
last_sunday = today - today.wday
start_unix_time = (last_sunday - 6).to_time.to_i
end_unix_time = last_sunday.to_time.to_i
uri = URI.parse("https://api.pingdom.com/api/2.0/summary.average/#{check_id}?includeuptime=true&from=#{start_unix_time}&to=#{end_unix_time}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri, initheader = {"App-Key" => key})
request.basic_auth(user, pass)
response = http.request(request)
response_hash = ActiveSupport::JSON.decode(response.body)
downtime_seconds = response_hash["summary"]["status"]["totaldown"]
self.downtime_minutes = downtime_seconds/60.0
self.save
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment