Skip to content

added option to retry query #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions deploy/deploy.sh
Original file line number Diff line number Diff line change
@@ -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
68 changes: 64 additions & 4 deletions nagios_graphite/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

from __future__ import print_function

import functools
import os
import sys
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to keep builtin imports separate from third-party imports. Any chance we could re-sort these back into that order?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's that?

import urllib
import functools
from time import sleep

import requests
from pynagios import Plugin, Response, make_option, UNKNOWN


class EmptyQueryResult(Exception):
pass

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -227,7 +232,7 @@ def run_tests(self):
# <http://pythonhosted.org/setuptools/setuptools.html>
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],
Expand Down