forked from inuits/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_consul.py
80 lines (70 loc) · 2.04 KB
/
check_consul.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
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
#!/usr/bin/env python
import urllib2
import json
import pprint
import sys
import httplib
# Constants
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
# argument parsing
args = sys.argv[1:]
host = "localhost"
node_name = None
tls = False
ca_cert = None
while len(args) > 0:
if args[0] == "--host" or args[0] == "-h":
host = args[1]
elif args[0] == "--node-name":
node_name = args[1]
elif args[0] == "--tls":
tls = True
args = args[1:]
continue
elif args[0] == "--ca-cert":
ca_cert = args[1]
args = args[2:]
if node_name is None or node_name.strip() == "":
print("--node-name is a required parameter!")
sys.exit(UNKNOWN)
# check status
try:
scheme = "http://"
if tls:
scheme = "https://"
url = scheme + host + ':8500/v1/health/node/' + node_name
req = urllib2.Request(url)
response = urllib2.urlopen(req, timeout=10, cafile=ca_cert)
data = json.loads(response.read())
if len(data) == 0:
print("Node not found")
sys.exit(UNKNOWN)
checks = { check["CheckID"]: check for check in data} # convert into dict, key is CheckID
consul_check = checks["serfHealth"]
if consul_check["Status"] == "passing":
print(consul_check["Output"])
sys.exit(OK)
elif consul_check["Status"] == "warning":
print(consul_check["Output"])
sys.exit(WARNING)
elif consul_check["Status"] == "critical":
print(consul_check["Output"])
sys.exit(CRITICAL)
elif consul_check["Status"] == "maintenance":
print("[Service is in maintenance]" + str(consul_check["Output"]))
sys.exit(WARNING)
else:
print("Unknown status")
sys.exit(UNKNOWN)
except urllib2.URLError as e:
print("Can't connect to Consul, reported error: " + str(e.reason))
sys.exit(CRITICAL)
except httplib.HTTPException as e:
print("Can't connect to Consul, reported error: " + repr(e))
sys.exit(CRITICAL)
except Exception as e:
print("Exception while contacting consul: " + repr(e))
sys.exit(UNKNOWN)