-
Notifications
You must be signed in to change notification settings - Fork 57
/
check_graphite
executable file
·94 lines (77 loc) · 3.55 KB
/
check_graphite
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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()