Skip to content

Instantly share code, notes, and snippets.

@pandemicsyn
Created April 28, 2015 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pandemicsyn/3366b7d5ce147232a759 to your computer and use it in GitHub Desktop.
Save pandemicsyn/3366b7d5ce147232a759 to your computer and use it in GitHub Desktop.
check_graphite
#!/usr/bin/env python
import sys
import urllib2
import json
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-u", "--url", default=None, help="Graphite URL")
parser.add_option("-s", "--short-name", default=None,
help="Short name to use when printing info")
parser.add_option(
"-t", "--threshold", default=None, help="Threshold value to use")
parser.add_option("--less-than", default=False, action="store_true", help="Use less than for comparison instread of greater than. Default = false")
parser.add_option("-m", "--measure", default="avg", help="Use one of [max, min, sum, avg, last] value(s) when performing comparison. Default = avg")
(options, args) = parser.parse_args()
profile = {'max': None, 'min': None, 'sum': None, 'avg': None, 'last': None}
measure = 'avg'
threshold_type = 'greater'
if not options.url:
parser.print_help()
print "Must specificy graphite url to hit"
sys.exit(1)
else:
target = options.url + "&format=json"
if not options.short_name:
parser.print_help()
print "Must provide a short name"
sys.exit(1)
else:
short_name = options.short_name
if not options.threshold:
parser.print_help()
print "Must provide a threshold value."
sys.exit(1)
else:
try:
threshold = float(options.threshold)
except ValueError:
print "Error threshold must be int or float"
sys.exit(1)
if options.less_than:
threshold_type = 'lesser'
if options.measure:
if options.measure in profile.keys():
measure = options.measure
else:
parser.print_help()
print "Measure must be one of %s" % profile.keys()
sys.exit(1)
req = urllib2.Request(target)
res = urllib2.urlopen(req)
try:
payload = json.loads(res.read())
if not payload:
print "OK: Metric doesn't exist yet"
sys.exit(0)
content = payload[0]
values = [x[0] for x in content['datapoints'] if x[0] is not None]
if not values:
print "OK: No values yet for %s" % short_name
sys.exit(0)
profile['max'] = max(values)
profile['min'] = min(values)
profile['sum'] = sum(values)
profile['avg'] = profile['sum'] / len(values)
profile['last'] = values[-1]
if threshold_type == 'greater':
if profile[measure] > threshold:
print "CRITICAL: %s %s value (%d) is > %d" % (short_name, measure, profile[measure], threshold)
sys.exit(1)
else:
print "OK: %s %s value (%d) is > %d" % (short_name, measure, profile[measure], threshold)
sys.exit()
else:
if profile[measure] < threshold:
print "CRITICAL: %s %s value (%d) is < %d" % (short_name, measure, profile[measure], threshold)
sys.exit(1)
else:
print "OK: %s %s value (%d) is < %d" % (short_name, measure, profile[measure], threshold)
sys.exit()
except Exception as err:
print err
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment