forked from HariSekhon/Nagios-Plugins
/
check_consul_key.py
executable file
·135 lines (121 loc) · 4.7 KB
/
check_consul_key.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2016-01-16 00:46:07 +0000 (Sat, 16 Jan 2016)
#
# https://github.com/harisekhon/nagios-plugins
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback
# to help improve or steer this or other code I publish
#
# https://www.linkedin.com/in/harisekhon
#
"""
Nagios Plugin to check a given key in a Consul key-value store
Optionally may match the contents against a given regex or numeric thresholds if the key contents are numeric
Tested on Consul 0.6.3
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
#from __future__ import unicode_literals
import base64
import json
import os
import re
import sys
try:
import requests
except ImportError as _:
print(_)
sys.exit(4)
libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'pylib'))
sys.path.append(libdir)
try:
# pylint: disable=wrong-import-position
from harisekhon.utils import qquit, log, isFloat, isList, isStr, support_msg_api
from harisekhon.utils import validate_host, validate_port, validate_chars, validate_regex
from harisekhon import NagiosPlugin
except ImportError as _:
print('module import failed: %s' % _)
sys.exit(4)
__author__ = 'Hari Sekhon'
__version__ = '0.2.1'
class ConsulCheckKey(NagiosPlugin):
def add_options(self):
self.add_hostoption('Consul', default_host='localhost', default_port='8500')
self.add_opt('-k', '--key', help='Key to query from Consul')
self.add_opt('-r', '--regex', help="Regex to compare the key's value against (optional)")
self.add_thresholds()
def extract_value(self, content): # pylint: disable=no-self-use
json_data = None
try:
json_data = json.loads(content)
except ValueError:
qquit('UNKNOWN', "non-json data returned by consul: '%s'. %s" % (content, support_msg_api()))
value = None
if not isList(json_data):
qquit('UNKNOWN', "non-list returned by consul: '%s'. %s" % (content, support_msg_api()))
if not json_data:
qquit('UNKNOWN', "blank list returned by consul! '%s'. %s" % (content, support_msg_api()))
if len(json_data) > 1:
qquit('UNKNOWN', "more than one key returned by consul! response = '%s'. %s" \
% (content, support_msg_api()))
try:
value = json_data[0]['Value']
except KeyError:
qquit('UNKNOWN', "couldn't find field 'Value' in response from consul: '%s'. %s" \
% (content, support_msg_api()))
try:
value = base64.decodestring(value)
except TypeError as _:
qquit('UNKNOWN', "invalid data returned for key '%(key)s' value = '%(value)s', failed to base64 decode" \
% locals())
return value
def run(self):
self.no_args()
host = self.get_opt('host')
port = self.get_opt('port')
validate_host(host)
validate_port(port)
key = self.get_opt('key')
regex = self.get_opt('regex')
if not key:
self.usage('--key not defined')
key = key.lstrip('/')
validate_chars(key, 'key', r'\w\/-')
if regex:
validate_regex(regex, 'key')
self.validate_thresholds(optional=True)
req = None
url = 'http://%(host)s:%(port)s/v1/kv/%(key)s' % locals()
log.debug('GET %s' % url)
try:
req = requests.get(url)
except requests.exceptions.RequestException as _:
qquit('CRITICAL', _)
log.debug("response: %s %s" % (req.status_code, req.reason))
log.debug("content: '%s'" % req.content)
if req.status_code != 200:
err = ''
if req.content and isStr(req.content) and len(req.content.split('\n')) < 2:
err += ': ' + req.content
qquit('CRITICAL', "failed to retrieve consul key '%s': '%s' %s%s" % (key, req.status_code, req.reason, err))
value = self.extract_value(req.content)
log.info("value = '%(value)s'" % locals())
self.ok()
self.msg = "consul key '%s' value = '%s'" % (key, value)
if regex:
if not re.search(regex, value):
self.critical()
self.msg += " (did not match expected regex '%s')" % regex
#elif self.get_verbose():
# self.msg += " (matched regex '%s')" % regex
self.check_thresholds(value)
if isFloat(value):
self.msg += " | '%s'=%s" % (key, value)
if __name__ == '__main__':
ConsulCheckKey().main()