From f2ceb90d13d59d41d10f2c1b57ac17b74a21ed4e Mon Sep 17 00:00:00 2001 From: Ed Silva Date: Thu, 28 Jul 2016 14:12:52 -0700 Subject: [PATCH 1/4] added option to retry query --- README.md | 2 ++ nagios_graphite/main.py | 29 +++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) 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/nagios_graphite/main.py b/nagios_graphite/main.py index f52d0f8..b92bdab 100755 --- a/nagios_graphite/main.py +++ b/nagios_graphite/main.py @@ -4,11 +4,11 @@ from __future__ import print_function -import sys -import urllib import functools - import requests +import sys +import urllib +from time import sleep from pynagios import Plugin, Response, make_option, UNKNOWN @@ -120,7 +120,22 @@ def graphite_fetch(opts, session=None): url = graphite_url(opts) 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,6 +176,12 @@ 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", From 59036560bac71d27e4902ff960d71b11463e3b99 Mon Sep 17 00:00:00 2001 From: Ed Silva Date: Fri, 29 Jul 2016 13:05:46 -0700 Subject: [PATCH 2/4] reorder stuff --- nagios_graphite/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nagios_graphite/main.py b/nagios_graphite/main.py index b92bdab..59ebf7f 100755 --- a/nagios_graphite/main.py +++ b/nagios_graphite/main.py @@ -5,10 +5,11 @@ from __future__ import print_function import functools -import requests import sys import urllib from time import sleep + +import requests from pynagios import Plugin, Response, make_option, UNKNOWN From 16a96ded94eaf02d70d40817c306036eb9b49fef Mon Sep 17 00:00:00 2001 From: Tim Whalen Date: Tue, 30 Aug 2016 17:03:12 -0700 Subject: [PATCH 3/4] Added GoCD build counter as minor version --- deploy/deploy.sh | 5 +++++ setup.py | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 deploy/deploy.sh 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/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], From 881e280abd162dba4a550d4393b6df81d1458478 Mon Sep 17 00:00:00 2001 From: Ed Silva Date: Tue, 11 Oct 2016 14:58:39 -0700 Subject: [PATCH 4/4] add notes and actions to output message --- nagios_graphite/main.py | 42 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/nagios_graphite/main.py b/nagios_graphite/main.py index 59ebf7f..9ed8270 100755 --- a/nagios_graphite/main.py +++ b/nagios_graphite/main.py @@ -5,6 +5,7 @@ from __future__ import print_function import functools +import os import sys import urllib from time import sleep @@ -12,7 +13,6 @@ import requests from pynagios import Plugin, Response, make_option, UNKNOWN - class EmptyQueryResult(Exception): pass @@ -119,6 +119,8 @@ 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) if opts.retry: @@ -189,6 +191,18 @@ class GraphiteNagios(Plugin): 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: @@ -200,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()