forked from datacratic/check_graphite
-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_graphite
executable file
·111 lines (91 loc) · 4.02 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env 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)
def f_sum(values):
return sum(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 },
"sum":{ "label": "sum", "function": f_sum },
}
graphite = Plugin("Plugin to retrieve data from graphite", "1.0")
graphite.add_option("u", "url", "URL to query for data (required)", 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("s", "status_message", "Status Message (default function value of metric: value)",default=None)
graphite.add_option("f", "function", "Function to run on retrieved values: avg/min/max/last/sum (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)
counter=None
values=None
mdata = data.strip().split('\n')
for d in mdata:
try:
pieces = d.split("|")
counter = pieces[0].split(",")[0]
values = pieces[1].split(",")
except:
graphite.unknown_error("Graphite returned bad data")
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)
if graphite.options.status_message:
graphite.set_status_message(graphite.options.status_message)
else:
graphite.set_status_message("%s value of %s: %f" % (functionmap[graphite.options.function]["label"], counter, value))
graphite.finish()