Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 154 lines (138 sloc) 4.993 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#!/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_i
perfdata = ""
perfdata = "| #{@@options[:shortname]}=#{total}" if !@@options[:shortname].nil?

if (@@options[:critical].to_i > @@options[:warning].to_i)
  if (total >= @@options[:critical].to_i)
    puts "CRITICAL #{@@options[:message]} #{total}#{@@options[:units]} threshold: #{@@options[:critical]} #{output_url} #{perfdata}"
    exit EXIT_CRITICAL
  elsif (total >= @@options[:warning].to_i)
    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_i)
    puts "CRITICAL #{@@options[:message]} #{total}#{@@options[:units]} threshold: #{@@options[:critical]} #{output_url} #{perfdata}"
    exit EXIT_CRITICAL
  elsif (total <= @@options[:warning].to_i)
    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

Something went wrong with that request. Please try again.