diff --git a/README.md b/README.md index 5a757a7..ee15bc7 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ Usage: nagios_graphite [options] Options: -U USERNAME, --username=USERNAME Username (HTTP Basic Auth) + -r RETRY, --retry=RETRY + Retry graphite request n times before giving up -N NAME, --name=NAME Metric name -A FUNC, --algorithm=FUNC Algorithm for combining metrics, options: nullpct, diff --git a/deploy/deploy.sh b/deploy/deploy.sh new file mode 100644 index 0000000..edc572f --- /dev/null +++ b/deploy/deploy.sh @@ -0,0 +1,5 @@ +virtualenv-2.7 #{VIRTUALENV} +#{VIRTUALENV}/bin/pip install -U pip -i #{PYPI} +#{VIRTUALENV}/bin/pip install -r requirements.txt +/bin/bash -c /usr/bin/python2.7 setup.py bdist_rpm --release ${GO_STAGE_COUNTER} --fix-python +/bin/bash -c /usr/bin/ox-upload-artifact -r libs-snapshot-local -p python2.7/pypi -n dist/*.noarch.rpm --verbose diff --git a/nagios_graphite/main.py b/nagios_graphite/main.py index f52d0f8..9ed8270 100755 --- a/nagios_graphite/main.py +++ b/nagios_graphite/main.py @@ -4,14 +4,15 @@ from __future__ import print_function +import functools +import os import sys import urllib -import functools +from time import sleep import requests from pynagios import Plugin, Response, make_option, UNKNOWN - class EmptyQueryResult(Exception): pass @@ -118,9 +119,26 @@ def graphite_fetch(opts, session=None): session = graphite_session(opts) url = graphite_url(opts) + if opts.debug: + print("{0}".format(url)) resp = session.get(url, timeout=opts.http_timeout) - return resp.json() if resp.ok else [] + if opts.retry: + retry_count = 0 + while retry_count < opts.retry: + retry_count = retry_count + 1 + + try: + ret = resp.json() + if resp.ok and ret: + return ret + except: + pass + + # pause before hitting the server again + sleep(0.2) + else: + return resp.json() if resp.ok else [] def check_graphite(opts, session=None): @@ -161,12 +179,30 @@ class GraphiteNagios(Plugin): "{0}, (default: avg)".format(F_OPTS)), default="avg", choices=FUNCTIONS.keys()) + retry = make_option( + "--retry", "-r", + help="Retry graphite request n times before giving up", + default=0, + type=int) + http_timeout = make_option( "--http-timeout", "-o", help="HTTP request timeout", default=10, type=int) + notes = make_option( + "--notes", "-n", + help="Notes to be added to the alert") + + action = make_option( + "--action", "-a", + help="Actions to be listed in the alert") + + debug = make_option( + "--debug", "-d", + help="Print the graphite URL", action="store_true") + def check(self): value = check_graphite(self.options) if value is None: @@ -178,10 +214,34 @@ def check(self): response.set_perf_data(self.options.func, value) return response + def status_str(self, response): + result = "%s:" % response.status.name + + if response.message is not None: + result += " %s" % response.message + + if len(response.perf_data) > 0: + # Attach the performance data to the result + data = [str(val) for key,val in response.perf_data.iteritems()] + result += '|%s' % (' '.join(data)) + + if self.options.notes: + result += "\nNotes: {0}\n".format(self.options.notes) + + if self.options.action: + result += "\nAction: {0}\n".format(self.options.action) + + return (response.status.exit_code, result) + def main(args): try: - return GraphiteNagios(args).check().exit() + nag = GraphiteNagios(args) + response = nag.check() + (exit_code, msg) = nag.status_str(response) + + print(msg) + return sys.exit(exit_code) except Exception as e: message = "{0}: {1}".format(e.__class__.__name__, str(e)) Response(UNKNOWN, "Client error: " + message).exit() diff --git a/setup.py b/setup.py index e05767f..aa51aff 100644 --- a/setup.py +++ b/setup.py @@ -55,6 +55,11 @@ def check_output(cmd_args, *args, **kwargs): metadata = imp.load_source( 'metadata', os.path.join(CODE_DIRECTORY, 'metadata.py')) +if os.environ['GO_PIPELINE_COUNTER']: + rpm_version = '%s.%s' % (metadata.version, os.environ['GO_PIPELINE_COUNTER']) +else: + rpm_version = metadata.version + ## Miscellaneous helper functions @@ -227,7 +232,7 @@ def run_tests(self): # setup_dict = dict( name=metadata.package, - version=metadata.version, + version=rpm_version, author=metadata.authors[0], author_email=metadata.emails[0], maintainer=metadata.authors[0],