forked from etsy/nagios_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_graphite_data
executable file
·104 lines (84 loc) · 3.33 KB
/
check_graphite_data
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
#!/usr/bin/env python
import sys
STATE_OK = 0
STATE_WARNING = 1
STATE_CRITICAL = 2
STATE_UNKNOWN = 3
STATE_DEPENDENT = 4
__version__ = "1.0.0"
functionmap = {
"avg": {"label": "average", "function": lambda x: sum(x) / len(x)},
"last": {"label": "last", "function": lambda x: x[-1]},
"min": {"label": "minimum", "function": lambda x: min(x)},
"max": {"label": "maximum", "function": lambda x: max(x)},
"sum": {"label": "sum", "function": lambda x: sum(x)},
}
def get_http_data(url):
try:
# For Python 3.0 and later
from urllib.request import urlopen
response = urlopen(url)
return str(response.read(), encoding='utf8')
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
response = urlopen(url)
return response.read()
def pull_graphite_data(url):
"""Pull down raw data from Graphite"""
# Make sure the url ends with '&rawData'
if not url.endswith('&rawData'):
url = url + '&rawData'
data = get_http_data(url)
pieces = data.split("|")
counter = pieces[0].split(",")[0]
values = pieces[1].split(",")[:-1]
return (counter, values)
def get_value(url, function='avg'):
"""Get a single value from a Graphite metric"""
(counter_name, text_values) = pull_graphite_data(url)
values = [float(x) for x in text_values if x != 'None']
if len(values) == 0:
raise Exception("Graphite returned an empty list of values")
else:
value = functionmap[function]["function"](values)
return value
def main(argv):
import argparse
parser = argparse.ArgumentParser(description='Check graphite data')
parser.add_argument('-c', '--crit', type=float, help='Critical threshold')
parser.add_argument('-w', '--warn', type=float, help='Warning threshold')
parser.add_argument('-u', '--url', help='Graphite graph URL')
parser.add_argument('-f', '--function', choices=['avg', 'min', 'max', 'last', 'sum'], default='avg', help='Function to run on retrieved values')
parser.add_argument('-r', '--reverse', action='store_true', help='Alert when the value is UNDER warn/crit instead of OVER')
parser.add_argument('-v', '--verbose', action='count')
parser.add_argument('-V', '--version', action='version', version='{prog} {version}'.format(prog=parser.prog, version=__version__))
try:
args = parser.parse_args()
except Exception as err:
args.print_help()
sys.exit(STATE_UNKNOWN)
if ((args.url is None) or (args.warn is None) or (args.crit is None)):
sys.exit(STATE_UNKNOWN)
try:
graphite_data = get_value(args.url, args.function)
except Exception as err:
print("Graphite query error: {0}".format(err))
sys.exit(STATE_UNKNOWN)
print('Current value: %s, warn threshold: %s, crit threshold: %s' % (graphite_data, args.warn, args.crit))
if args.reverse:
if args.crit >= graphite_data:
sys.exit(STATE_CRITICAL)
elif args.warn >= graphite_data:
sys.exit(STATE_WARNING)
else:
sys.exit(STATE_OK)
else:
if graphite_data >= args.crit:
sys.exit(STATE_CRITICAL)
elif graphite_data >= args.warn:
sys.exit(STATE_WARNING)
else:
sys.exit(STATE_OK)
if __name__ == '__main__':
main(sys.argv[1:])