From f385f28f0ae2262f120da548892a85565d0c530a Mon Sep 17 00:00:00 2001 From: Robert May Date: Thu, 6 Jun 2013 13:27:08 +0000 Subject: [PATCH] Init --- .gitignore | 1 + Gemfile | 7 ++++ Gemfile.lock | 29 ++++++++++++++++ Procfile | 1 + pingdom_pi.rb | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Procfile create mode 100644 pingdom_pi.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..f34b7d5 --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source "https://rubygems.org" + +gem 'httparty' +gem 'thor' +gem 'multi_json' +gem 'rufus-scheduler' +gem 'pry' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..96f9a46 --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..94a2d99 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +ping: ruby pingdom_pi.rb diff --git a/pingdom_pi.rb b/pingdom_pi.rb new file mode 100644 index 0000000..e09ec89 --- /dev/null +++ b/pingdom_pi.rb @@ -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