From 1b3ef8e9b8225b75ea9e478b983fe1d1e0cf8e0c Mon Sep 17 00:00:00 2001 From: Pavel Pulec Date: Thu, 15 Dec 2016 14:32:56 +0100 Subject: [PATCH] add check_graphite Signed-off-by: Pavel Pulec --- README.md | 5 +++ build.txt | 1 + check_graphite | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100755 check_graphite diff --git a/README.md b/README.md index 7cf3520..25d6ea5 100644 --- a/README.md +++ b/README.md @@ -289,6 +289,11 @@ An example Vagrant project has been included to get you started right away. gmykhailiuta upstream + + check_graphite + datacratic + upstream + diff --git a/build.txt b/build.txt index c6bfe4a..9add125 100644 --- a/build.txt +++ b/build.txt @@ -5,6 +5,7 @@ check_bacula 0.0.5 check_collective-access.rb 1.0 check_crm 0.7 check_drbd 3e0b548f55 +check_graphite 1.0 check_iostat 0.0.4 check_ipsec 2.0 check_linux-procstat.pl 0.4 diff --git a/check_graphite b/check_graphite new file mode 100755 index 0000000..80d15ce --- /dev/null +++ b/check_graphite @@ -0,0 +1,94 @@ +#!/usr/bin/python + +# Copyright (c) 2011 Recoset +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +from NagAconda import Plugin +import urllib2 + +def f_avg(values): + return sum(values)/len(values); + +def f_last(values): + return values[-1] + +def f_min(values): + return min(values) + +def f_max(values): + return max(values) + +functionmap = { + "avg":{ "label": "average", "function": f_avg }, + "last":{ "label": "last", "function": f_last }, + "min":{ "label": "minimum", "function": f_min }, + "max":{ "label": "maximum", "function": f_max }, +} + +graphite = Plugin("Plugin to retrieve data from graphite", "1.0") +graphite.add_option("u", "url", "URL to query for data", required=True) +graphite.add_option("U", "username", "User for authentication") +graphite.add_option("P", "password", "Password for authentication") +graphite.add_option("H", "hostname", "Host name to use in the URL") +graphite.add_option("n", "none", "Ignore None values: 'yes' or 'no' (default no)") +graphite.add_option("f", "function", "Function to run on retrieved values: avg/min/max/last (default 'avg')", + default="avg") + +graphite.enable_status("warning") +graphite.enable_status("critical") +graphite.start() + +if graphite.options.username and graphite.options.password: + password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() + password_mgr.add_password(None,uri=graphite.options.url, + user=graphite.options.username, + passwd=graphite.options.password) + auth_handler =urllib2.HTTPBasicAuthHandler(password_mgr) + opener = urllib2.build_opener(auth_handler) + urllib2.install_opener(opener) + +if graphite.options.hostname: + graphite.options.url = graphite.options.url.replace('@HOSTNAME@', + graphite.options.hostname.replace('.','_')) + +usock = urllib2.urlopen(graphite.options.url) +data = usock.read() +usock.close() + +if graphite.options.function not in functionmap: + graphite.unknown_error("Bad function name given to -f/--function option: '%s'" % graphite.options.function) + +pieces = data.split("|") +counter = pieces[0].split(",")[0] +values = pieces[1].split(",")[:-1] + +if graphite.options.none == 'yes': + values = map(lambda x: float(x), filter(lambda x: x != 'None', values)) +else: + values = map(lambda x: 0.0 if x == 'None' else float(x), values) +if len(values) == 0: + graphite.unknown_error("Graphite returned an empty list of values") +else: + value = functionmap[graphite.options.function]["function"](values) + +graphite.set_value(counter, value) +graphite.set_status_message("%s value of %s: %f" % (functionmap[graphite.options.function]["label"], counter, value)) + +graphite.finish()