Skip to content

Instantly share code, notes, and snippets.

@josephholsten
Created January 4, 2016 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save josephholsten/484ecb25bab0a8a97ca8 to your computer and use it in GitHub Desktop.
Save josephholsten/484ecb25bab0a8a97ca8 to your computer and use it in GitHub Desktop.
Simplest HTTP+JSON sensu plugin
#! /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