-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphite-check.py
52 lines (46 loc) · 1.54 KB
/
graphite-check.py
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
#!/usr/bin/env python
from optparse import OptionParser
import sys
import requests
options = OptionParser()
options.add_option("-u", "--url", dest="url", help="graphite url",
default="http://graphite.example.com")
options.add_option("-c", "--critical", dest="critical", help="critical limit",
default="1024", type="int")
options.add_option("-w", "--warning", dest="warning",
help="warning limit", default="800", type="int")
options.add_option("-t", "--target", dest="target",
help="graphite target",
default="server.web1.load.load.longterm")
options.add_option("-T", "--time", dest="time", help="time in minutes",
default="5", type="int")
(options, args) = options.parse_args()
URL = ("%s/render/?target=%s&from=-%dminutes&format=json" %
(options.url, options.target, options.time))
try:
r = requests.get(URL).json()
except requests.exceptions.ConnectionError:
print ("Critial : Connection Error")
sys.exit(1)
def get_average(j):
total = 0
i = 0
for x in r:
for d in x['datapoints']:
if d[0] is not None:
total = total + d[0]
i = i + 1
return total / i
avg = get_average(r)
if avg >= options.critical:
print (("Critical Avg : %d " % avg))
sys.exit(1)
elif avg >= options.warning:
print (("Warning Avg : %d " % avg))
sys.exit(2)
elif avg < options.warning:
print (("OK Avg : %i " % avg))
sys.exit(0)
else:
print ("Unknow")
sys.exit(3)