-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pavel Pulec <[email protected]>
- Loading branch information
Pavel Pulec
committed
Dec 15, 2016
1 parent
83c66c7
commit 1b3ef8e
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |