Created
April 12, 2013 18:17
-
-
Save eldavido/5374007 to your computer and use it in GitHub Desktop.
Connect Pingdom status checks to nagios. Written in Ruby w/httparty and optparse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'httparty' | |
require 'optparse' | |
require 'ostruct' | |
# That was simple | |
class PingdomAPI | |
include HTTParty | |
base_uri 'https://api.pingdom.com/' | |
attr_accessor :pingdom_user, :pingdom_password | |
def initialize(pingdom_user, pingdom_password) | |
@pingdom_user = pingdom_user | |
@pingdom_password = pingdom_password | |
@app_key = 'fkgcdl519t5uwllxmusmcyyskevs75z2' | |
@auth = { :username => @pingdom_user, :password => @pingdom_password } | |
end | |
def get_status | |
self.class.get('/api/2.0/checks', :basic_auth => @auth) | |
end | |
end | |
# options should include pingdom user, pingdom password, and what to check | |
class PingdomAPICheckParser | |
def self.parse(args) | |
options = OpenStruct.new | |
options.check_id = nil | |
options.pingdom_user = nil | |
options.pingdom_password = nil | |
optparse = OptionParser.new do |opts| | |
opts.banner = "Usage: check_pingdom.rb [options]" | |
opts.separator "" | |
opts.separator "Specific options:" | |
opts.on("-i", "--pingdom-check-id ID", 'Numerical id of pingdom check') do |arg| | |
options.check_id = arg | |
end | |
opts.on("-u", "--pingdom-username [email protected]", "Pingdom username (email address)") do |arg| | |
options.pingdom_user = arg | |
end | |
opts.on("-p", "--pingdom-password password", 'Pingdom password') do |arg| | |
options.pingdom_password = arg | |
end | |
end | |
optparse.parse!(args) | |
options | |
end | |
end | |
options = PingdomAPICheckParser.parse(ARGV) | |
begin | |
pa = PingdomAPI.new(options.pingdom_user, options.pingdom_password) | |
r = pa.get_status | |
if r.success? | |
if(r.parsed_response["checks"] && r.parsed_response["checks"].is_a?(Array)) | |
filtered_checks = r.parsed_response["checks"].select { |check| check["id"].to_s == options.check_id } | |
if(filtered_checks.length > 0) | |
check = filtered_checks.first | |
if check["status"] == 'up' | |
puts "OK: Pingdom reports check #{options.check_id} in state UP" | |
exit 0 | |
else | |
puts "CRITICAL: Pingdom reports check #{options.check_id} in state DOWN" | |
exit 2 | |
end | |
else | |
puts "CRITICAL: Could not find check with id #{options.check_id}" | |
exit 2 | |
end | |
else | |
puts 'CRITICAL: Invalid response from Pingdom API ("checks" field not an array)' | |
exit 2 | |
end | |
else | |
puts "CRITICAL: Pingdom API returned error" | |
exit 2 | |
end | |
rescue SocketError | |
puts "CRITICAL: Pingdom API unreachable" | |
exit 2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment