Skip to content

Commit

Permalink
add check_graphite
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Pulec <[email protected]>
  • Loading branch information
Pavel Pulec committed Dec 15, 2016
1 parent 83c66c7 commit 1b3ef8e
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ An example Vagrant project has been included to get you started right away.
<td><a href="https://github.com/gmykhailiuta">gmykhailiuta</a></td>
<td><a href="https://raw.githubusercontent.com/gmykhailiuta/check_zmstatus/master/check_zmstatus.pl">upstream</a></td>
</tr>
<tr>
<td>check_graphite</td>
<td><a href="https://github.com/datacratic">datacratic</a></td>
<td><a href="https://github.com/datacratic/check_graphite">upstream</a></td>
</tr>



Expand Down
1 change: 1 addition & 0 deletions build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
94 changes: 94 additions & 0 deletions check_graphite
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/python

# Copyright (c) 2011 Recoset <[email protected]>
#
# 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()

0 comments on commit 1b3ef8e

Please sign in to comment.