Skip to content

Instantly share code, notes, and snippets.

@dcolish
Created May 2, 2013 23:53
Show Gist options
  • Save dcolish/5506350 to your computer and use it in GitHub Desktop.
Save dcolish/5506350 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.7
import argparse
import contextlib
import httplib
import json
import os
import re
import sys
LIST_ALL = False
metrics = ['OneMinuteRate', 'Count']
excludes = map(re.compile, [
'.*scope=_servers.*',
'.*datacubeio.*',
'.*_scope__regions.*',
'.*scope=_regions.*',
'.*dbharness.*',
'.*statshtable.*',
])
def build_check(bean, metric, port, uri):
uom = 'c' if metric == 'Count' else ''
return {
bean: {
'key': metric,
'port': port,
'uom': uom,
'uri': uri,
'warn': '0',
'warn_min': '0',
'crit': '0',
'crit_min': '0'
}
}
def load_json(host, port, url_path='/'):
with contextlib.closing(httplib.HTTPConnection(host, port)) as conn:
conn.request('GET', url_path)
resp = conn.getresponse()
if 200 == resp.status:
return json.loads(resp.read())
else:
print >> sys.stderr, '%s - %s %s' % (url_path, resp.status, resp.reason)
return {}
def aggregate_checks(service, host, port):
checks = {}
for bean in load_json(host, port).get('beans', ()):
if any(exclude.search(bean) for exclude in excludes):
continue
uri = 'http://%s:%s/%s' % (host, port, bean)
bean_json = load_json(host, port, uri)
data = bean_json['data']
if LIST_ALL:
for metric in data:
print '%s:%s' % (bean, metric)
make_check(bean, metric, port, uri)
else:
for metric in metrics:
if metric in data.keys():
name = '%s_%s_%s' % (service, bean.replace('/','_'), metric.lower())
# Nagios doesn't like these :(
name = name.replace(',', '_').replace('=', '_')
checks.update(build_check(name, metric, port, bean))
return checks
def main():
parser = argparse.ArgumentParser()
parser.add_argument('service', metavar='SERVICE', nargs=1)
parser.add_argument('host', metavar='HOST', nargs=1)
parser.add_argument('port', metavar='PORT', nargs=1)
namespace = parser.parse_args()
print json.dumps(aggregate_checks(namespace.service[0], namespace.host[0], namespace.port[0]), indent=True)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment