Skip to content

Instantly share code, notes, and snippets.

@artbikes
Created February 3, 2014 21:20
Show Gist options
  • Save artbikes/8792657 to your computer and use it in GitHub Desktop.
Save artbikes/8792657 to your computer and use it in GitHub Desktop.
Check URL's for link elements. #sensu
#!/usr/bin/env ruby
#
# Check Endpoint Plugin
# ===
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-plugin/check/cli'
require 'open-uri'
require 'openssl'
require 'json'
require 'net/http'
class CheckEndpoint < Sensu::Plugin::Check::CLI
option :endpoint,
:description => "Endpoint to test",
:short => '-e PAT',
:long => '--endpoint PAT'
def run
unknown "No endpoint specified" unless config[:endpoint]
links = get_link_count(config[:endpoint])
n_warns = links
n_crits = links
message "#{links} links found at #{config[:endpoint]}"
if n_crits == 0
critical
else
ok
end
end
def get_link_count(link)
count = 0
uri = URI.parse(link)
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)
response = http.request(request)
if response.code == "200"
result = JSON.parse(response.body)
result.each do |key,array|
if key.kind_of?(Hash)
key["products"].each do |link|
count = count + 1
end
end
if array.kind_of?(Array)
array.each do |link|
count = count + 1
end
end
end
else
puts "ERROR!!!"
end
count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment