|
| 1 | +#!/usr/bin/python |
| 2 | +from nagioscheck import NagiosCheck, UsageError |
| 3 | +from nagioscheck import PerformanceMetric, Status |
| 4 | +import urllib2 |
| 5 | +import optparse |
| 6 | + |
| 7 | +try: |
| 8 | + import json |
| 9 | +except ImportError: |
| 10 | + import simplejson as json |
| 11 | + |
| 12 | +parser = optparse.OptionParser() |
| 13 | +parser.add_option('-E','--expected_nodes_in_cluster', dest='nodes_in_cluster') |
| 14 | +parser.add_option('-H','--host', dest='host') |
| 15 | +parser.add_option('-P','--port', dest='port') |
| 16 | +(opts, args) = parser.parse_args() |
| 17 | + |
| 18 | +mandatories = ['nodes_in_cluster', 'host'] |
| 19 | +for m in mandatories: |
| 20 | + if not opts.__dict__[m]: |
| 21 | + print "mandatory parameter is missing\n" |
| 22 | + exit(-1) |
| 23 | + |
| 24 | +def get_json(uri): |
| 25 | + try: |
| 26 | + response = urllib2.urlopen(uri) |
| 27 | + except urllib2.HTTPError, e: |
| 28 | + raise Status('unknown', ("API failure", None, "API failure:\n\n%s" % str(e))) |
| 29 | + except urllib2.URLError, e: |
| 30 | + raise Status('critical', (e.reason)) |
| 31 | + |
| 32 | + response_body = response.read() |
| 33 | + |
| 34 | + try: |
| 35 | + json_rep = json.loads(response_body) |
| 36 | + except ValueError: |
| 37 | + raise Status('unknown', ("API returned nonsense",)) |
| 38 | + |
| 39 | + return json_rep |
| 40 | + |
| 41 | +def main(): |
| 42 | + host = opts.host |
| 43 | + port = int(opts.port or '9200') |
| 44 | + nodes_in_cluster = int(opts.nodes_in_cluster) |
| 45 | + |
| 46 | + es_cluster_health = get_json(r'http://%s:%d/_cluster/health' %(host, port)) |
| 47 | + |
| 48 | + active_cluster_nodes = es_cluster_health['number_of_nodes'] |
| 49 | + |
| 50 | + if active_cluster_nodes != nodes_in_cluster: |
| 51 | + raise Status('CRITICAL', "The number of nodes in the cluster is not '%s'" %es_cluster_health['number_of_nodes']) |
| 52 | + else: |
| 53 | + raise Status('OK', "The number of nodes in the clust is good") |
| 54 | + |
| 55 | +if __name__ == "__main__":main() |
0 commit comments