Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert May committed Jun 6, 2013
0 parents commit f385f28
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source "https://rubygems.org"

gem 'httparty'
gem 'thor'
gem 'multi_json'
gem 'rufus-scheduler'
gem 'pry'
29 changes: 29 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.0.9)
httparty (0.11.0)
multi_json (~> 1.0)
multi_xml (>= 0.5.2)
method_source (0.8.1)
multi_json (1.7.6)
multi_xml (0.5.4)
pry (0.9.12.2)
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.4)
rufus-scheduler (2.0.19)
tzinfo (>= 0.3.23)
slop (3.4.5)
thor (0.18.1)
tzinfo (1.0.0)

PLATFORMS
ruby

DEPENDENCIES
httparty
multi_json
pry
rufus-scheduler
thor
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ping: ruby pingdom_pi.rb
95 changes: 95 additions & 0 deletions pingdom_pi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require 'rubygems'
require 'bundler/setup'
require 'httparty'
require 'multi_json'
require 'rufus-scheduler'
require 'pry'

SCHEDULER = Rufus::Scheduler.start_new

module PingdomPi
class Monitor
def initialize(auth = {}, options = {})
@client = Client.new(auth)
@options = options
end

def checks
p "Fetching checks"
checks = @client.checks
if !checks.empty?
handle_checks(checks)
else
p "No data"
end
end

def handle_checks(checks)
p "Handling check data"
status = checks.map { |c| c[:status] }
response_times = checks.map { |c| c[:lastresponsetime] }

if status.include? "down"
notify @options[:response_time].last[:colour]
else
highest_response_time = response_times.map(&:to_i).sort.last
response = @options[:response_time].select { |rt| rt[:range].include? highest_response_time }.last
notify(response[:colour])
end
end

def notify(colour = "000")
p "Notifying #{colour}"
path = File.expand_path(@options[:file])
File.open(path, "w") { |f| f.write colour }
p "Colour written to #{path}"
end
end

class Client
include HTTParty
base_uri 'https://api.pingdom.com/api/2.0'
headers "Content-Type" => "application/json"

def initialize(auth = {})
self.class.headers "App-Key" => auth.delete(:api_key)
@auth = auth
end

def checks(opts = {})
opts.merge!({ basic_auth: @auth })
response = self.class.get("/checks", opts)
if response.success?
parse_response(response.body)[:checks]
else
response
end
end

private
def parse_response(body)
MultiJson.load(body, symbolize_keys: true)
end
end
end

monitor = PingdomPi::Monitor.new({
username: ENV['PINGDOM_USERNAME'],
password: ENV['PINGDOM_PASSWORD'],
api_key: ENV['PINGDOM_API_KEY']
}, {
response_time: [
{ range: 1...700, colour: "010" },
{ range: 700...1500, colour: "011" },
{ range: 1500...5000, colour: "220" },
{ range: 5000..100000, colour: "200" }
],
file: "/dev/ledborg"
})

SCHEDULER.every '1m' do
p "Performing check"
monitor.checks
end

SCHEDULER.join

0 comments on commit f385f28

Please sign in to comment.