Created
January 4, 2016 06:53
-
-
Save josephholsten/484ecb25bab0a8a97ca8 to your computer and use it in GitHub Desktop.
Simplest HTTP+JSON sensu plugin
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 | |
# check-simple-http-json - show how to check a url, grab its json, and | |
require 'sensu-plugin/check/cli' | |
require 'json' | |
require 'net/http' | |
require 'uri' | |
class CheckSimpleHttpJson < Sensu::Plugin::Check::CLI | |
option :url, | |
description: 'URL', | |
short: '-u URL', | |
long: '--url URL', | |
default: 'http://localhost/' | |
option :key, | |
description: 'JSON Key', | |
short: '-k KEY', | |
long: '--key KEY', | |
default: 'foo' | |
option :warn, | |
short: '-w N', | |
long: '--warn N', | |
description: 'WARNING threshold', | |
proc: proc(&:to_i), | |
default: 9000 | |
option :crit, | |
short: '-c N', | |
long: '--crit N', | |
description: 'CRITICAL threshold', | |
proc: proc(&:to_i), | |
default: 4500 | |
def get(url) | |
url = URI.parse(url) | |
response = Net::HTTP.get_response(uri) | |
JSON.parse(response.body) | |
rescue Errno::ECONNREFUSED | |
warning 'Connection refused' | |
rescue JSON::ParserError | |
warning 'Invalid JSON' | |
end | |
def run | |
resource = get(config[:url]) | |
value = resource[config[:key]] | |
message "#{config[:url]}##{config[:key]} #{value}" | |
if value >= config[:crit] | |
critical | |
elsif value >= config[:warn] | |
warning | |
else | |
ok | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment