Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 154 lines (138 sloc) 4.993 kb
#!/usr/bin/env ruby
require "rubygems"
require "optparse"
require "rest-client"
require "json"
EXIT_OK = 0
EXIT_WARNING = 1
EXIT_CRITICAL = 2
EXIT_UNKNOWN = 3
@@options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [options]"
@@options[:url] = nil
opts.on("-u", "--url URL", "Target url") do |url|
@@options[:url] = url
end
@@options[:metric] = nil
opts.on("-m", "--metric NAME", "Metric path string") do |metric|
@@options[:metric] = metric
end
@@options[:shortname] = nil
opts.on("-s", "--shortname SHORTNAME", "Metric short name (used for performance data)") do |shortname|
@@options[:shortname] = shortname
end
@@options[:duration] = 5
opts.on("-d", "--duration LENGTH", "Length, in minute of data to parse (default: 5)") do |duration|
@@options[:duration] = duration
end
@@options[:function] = "average"
opts.on("-f", "--function \[average \| sum\]", "Function applied to metrics for thresholds (default: average)") do |function|
@@options[:function] = function
end
@@options[:warning] = nil
opts.on("-w", "--warning VALUE", "Warning threshold") do |warning|
@@options[:warning] = warning
end
@@options[:critical] = nil
opts.on("-c", "--critical VALUE", "Critical threshold") do |critical|
@@options[:critical] = critical
end
@@options[:units] = nil
opts.on("-U", "--units VALUE", "Adds a text tag to the metric count in the plugin output. Useful to identify the metric units. Doesn't affect data queries.") do |units|
@@options[:units] = units
end
@@options[:message] = "metric count:"
opts.on("-M", "--message VALUE", "Text message to output (default: \"metric count:\")") do |message|
@@options[:message] = message
end
@@options[:zero_on_error] = false
opts.on("-z", "--zero-on-error", "Return 0 on a graphite 500 error") do |zero_on_error|
@@options[:zero_on_error] = zero_on_error
end
@@options[:link_graph] = false
opts.on("-l", "--link-graph", "Add a link in the plugin output, showing a 24h graph for this metric in graphite. Requires 'escape_html_tags=0' option in cgi.cfg nagios config file") do |link_graph|
@@options[:link_graph] = link_graph
end
opts.on( "-h", "--help", "Display this screen" ) do
puts opts
exit
end
end
optparse.parse!
if (@@options[:url].nil? || @@options[:metric].nil? || @@options[:warning].nil? || @@options[:critical].nil?)
puts optparse
exit 2
end
def url
base_url = @@options[:url]
metric = @@options[:metric]
duration = @@options[:duration].to_s
base_url + "/render/?target=" + metric + "&format=json&from=-" + duration + "mins"
end
def output_url
base_url = @@options[:url]
metric = @@options[:metric]
if @@options[:link_graph]
"<a target=\"_blank\" href=\"http://" + base_url + "/render/?target=" + URI.escape(metric) + "&from=-24h&width=700&height=450\"> 24h graph </a>"
end
end
data = {}
data["total"] = 0
begin
if JSON.parse(RestClient.get(URI.encode(url))).empty?
puts "UNKNOWN no data reported by Graphite"
exit EXIT_UNKNOWN
else
JSON.parse(RestClient.get(URI.encode(url))).each do |cache|
data["#{cache['target']}"] = 0
count = 0
cache["datapoints"].each do |point|
unless (point[0].nil?)
data["#{cache['target']}"] += point[0]
count += 1
end
end
if (count == 0)
count = 1
end
if (@@options[:function] == "average")
data["#{cache['target']}"] /= count
end
data["total"] += data["#{cache['target']}"]
end
end
rescue RestClient::InternalServerError
if @@options[:zero_on_error]
data["total"] = 0
else
puts "CRITICAL 500 error from Graphite"
exit EXIT_CRITICAL
end
end
total = data["total"].to_f
perfdata = ""
perfdata = "| #{@@options[:shortname]}=#{total}" if !@@options[:shortname].nil?
if (@@options[:critical].to_f > @@options[:warning].to_f)
if (total >= @@options[:critical].to_f)
puts "CRITICAL #{@@options[:message]} #{total}#{@@options[:units]} threshold: #{@@options[:critical]} #{output_url} #{perfdata}"
exit EXIT_CRITICAL
elsif (total >= @@options[:warning].to_f)
puts "WARNING #{@@options[:message]} #{total}#{@@options[:units]} threshold: #{@@options[:warning]} #{output_url} #{perfdata}"
exit EXIT_WARNING
else
puts "OK #{@@options[:message]} #{total}#{@@options[:units]} #{output_url} #{perfdata}"
exit EXIT_OK
end
else
if (total <= @@options[:critical].to_f)
puts "CRITICAL #{@@options[:message]} #{total}#{@@options[:units]} threshold: #{@@options[:critical]} #{output_url} #{perfdata}"
exit EXIT_CRITICAL
elsif (total <= @@options[:warning].to_f)
puts "WARNING #{@@options[:message]} #{total}#{@@options[:units]} threshold: #{@@options[:warning]} #{output_url} #{perfdata}"
exit EXIT_WARNING
else
puts "OK #{@@options[:message]} #{total}#{@@options[:units]} #{output_url} #{perfdata}"
exit EXIT_OK
end
end
Jump to Line
Something went wrong with that request. Please try again.