forked from appuio/nagios-plugins-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_openshift_node_log_heartbeat
executable file
·306 lines (249 loc) · 9.46 KB
/
check_openshift_node_log_heartbeat
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/python3
"""
A Nagios plugin to verify Fluentd is working as expected. Looks for heartbeat
log entries from the system log that should have made it to Elasticsearch via
Fluentd.
Age is the time difference between sending timestamp (relative to sending host)
and querying time. Latency is the time difference between sending timestamp
(relative to sending host) and receiving it in Elasticsearch (relative to
logging system).
"""
import argparse
import collections
import datetime
import dateutil.parser
import math
import nagiosplugin
import re
import requests
import urllib.parse
from vshn_npo import utils
_ENTRY_MSG_RE = \
re.compile(r"(?i)\s*Logging\s+heartbeat\s+ts\s*=\s*(?P<ts>\d+)\s*$")
def make_query(query_from, query_to, expected_hostcount):
"""Return the search query for the JSON payload.
"""
return {
"query": {
"constant_score": {
"filter": {
"bool": {
"filter": [
{
"range": {
"@timestamp": {
"gte": query_from.isoformat(),
"lte": query_to.isoformat(),
},
},
},
{
# FIXME: In OpenShift Origin 3.9 the field is analyzed and
# can't be matched for exact equality. Must be switched to
# "term" once the field is no longer analyzed (as is the case
# in OpenShift Container Platform 3.9).
"match": {
"systemd.u.SYSLOG_IDENTIFIER":
"syslog-heartbeat-profile_openshift3",
},
},
{
"match": {
"message": {
# Exact match
"type": "boolean",
"operator": "and",
"query": "Logging heartbeat ts",
},
},
},
],
},
},
},
},
"aggs": {
# Create a bucket for each hostname
"hostname_buckets": {
"terms": {
"field": "hostname",
"size": expected_hostcount,
},
"aggs": {
# Most recent log entries per host
"most_recent_entry": {
"top_hits": {
"sort": [{
"@timestamp": {
"order": "desc",
},
}],
# Number of entries to return for each host (more than one in
# case there are spurious, unrelated messages also matching the
# filter)
"size": 3,
# Fields to return
"_source": {
"includes": [
"@timestamp",
"hostname",
"message",
],
},
},
},
},
},
},
}
class NodeNameContext(nagiosplugin.Context):
def __init__(self, name, nodes):
super().__init__(name)
self._nodes = nodes
def evaluate(self, metric, resource):
expected = frozenset(self._nodes)
actual = frozenset(metric.value)
missing = expected - actual
if missing:
hint = ("Node(s) not reporting heartbeat or not contained in query"
" result: {}".
format(", ".join(sorted(missing))))
return self.result_cls(nagiosplugin.Critical, hint=hint, metric=metric)
# Extraneous nodes are not reported as they may send logs for as long as
# they wish
return self.result_cls(nagiosplugin.Ok, metric=metric)
class LogEntry(collections.namedtuple("LogEntry", [
"hostname",
"entry_timestamp",
"heartbeat_timestamp",
])):
@property
def latency(self):
return self.entry_timestamp - self.heartbeat_timestamp
@classmethod
def from_search_hit(cls, fields):
m = _ENTRY_MSG_RE.match(fields["message"])
if not m:
return None
entry_timestamp = dateutil.parser.parse(fields["@timestamp"])
heartbeat_timestamp = \
datetime.datetime.fromtimestamp(int(m.group("ts")),
datetime.timezone.utc)
return cls(fields["hostname"], entry_timestamp, heartbeat_timestamp)
class Heartbeat(nagiosplugin.Resource):
"""Query Fluentd heartbeat information from Elasticsearch.
"""
def __init__(self, endpoint, auth_token, query_timeout, query_duration, expected_hostcount):
self._query_duration = query_duration
self._expected_hostcount = expected_hostcount
# * = YYYY.MM.DD (e.g. "2018.05.11")
index = ".operations.*"
self._url = "{}/{}/_search?{}".format(
endpoint,
urllib.parse.quote(index),
urllib.parse.urlencode({
"timeout": "{}s".format(query_timeout),
"ignore_unavailable": "true",
# Only aggregation results are used
"size": "0",
}),
)
self._headers = {
"Authorization": "Bearer {}".format(auth_token),
# Required by Kibana proxy
"kbn-xsrf": "dummy",
}
def probe(self):
"""Verify that heartbeats are coming in every few minutes.
"""
query_to = datetime.datetime.now(datetime.timezone.utc)
query_from = query_to - datetime.timedelta(seconds=self._query_duration)
query = make_query(query_from, query_to, self._expected_hostcount)
response = requests.post(self._url, headers=self._headers, json=query,
allow_redirects=False)
utils.raise_for_elasticsearch_response(response)
data = response.json()
hostname_buckets = data["aggregations"]["hostname_buckets"]
for (key, context) in [
("sum_other_doc_count", "other-doc-count"),
("doc_count_error_upper_bound", "default"),
]:
yield nagiosplugin.Metric(key, hostname_buckets[key], context=context)
log_entries = []
found = set()
for i in hostname_buckets["buckets"]:
for j in i["most_recent_entry"]["hits"]["hits"]:
le = LogEntry.from_search_hit(j["_source"])
if not (le is None or le.hostname in found):
found.add(le.hostname)
log_entries.append(le)
yield nagiosplugin.Metric("nodes", found, context="nodes")
for i in log_entries:
age = (query_to - i.heartbeat_timestamp).total_seconds()
latency = i.latency.total_seconds()
yield nagiosplugin.Metric("{}/age".format(i.hostname), age, uom="s",
context="heartbeat-age")
yield nagiosplugin.Metric("{}/latency".format(i.hostname), latency,
uom="s",
context="heartbeat-latency")
yield nagiosplugin.Metric("{}/ts/heartbeat".format(i.hostname),
i.heartbeat_timestamp.timestamp(),
context="default")
yield nagiosplugin.Metric("{}/ts/entry".format(i.hostname),
i.entry_timestamp.timestamp(),
context="default")
@nagiosplugin.guarded
def main():
"""
Entry point for script execution.
"""
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
utils.add_verbose_argument(parser)
utils.add_token_arguments(parser)
parser.add_argument("nodes", nargs="+", metavar="NODE",
help="List of nodes sending heartbeats")
parser.add_argument("--endpoint", required=True, metavar="URL",
help="Elasticsearch API endpoint")
parser.add_argument("--warning", type=int, default=(5 * 60),
metavar="THRESHOLD",
help="Number of seconds recorded not receiving a"
" heartbeat before triggering a warning status")
parser.add_argument("--critical", type=int, default=(15 * 60),
metavar="THRESHOLD",
help="Number of seconds recorded not receiving a"
" heartbeat before triggering a critical status")
parser.add_argument("--latency-warning", type=int, default=30,
metavar="THRESHOLD",
help="Maximum latency in seconds before triggering"
" a warning status")
parser.add_argument("--latency-critical", type=int, default=60,
metavar="THRESHOLD",
help="Maximum latency in seconds before triggering"
" a critical status")
parser.add_argument("--query-timeout", type=int, default=30,
metavar="SECONDS",
help="Elasticsearch query timeout")
args = parser.parse_args()
utils.setup_basic_logging(args.verbose)
token = utils.extract_token_argument(args)
query_duration = int(1.25 * max(args.warning, args.critical))
# Expected number of hosts (required to apply query to enough documents)
expected_hostcount = int(1.5 * len(args.nodes))
check = nagiosplugin.Check(
Heartbeat(args.endpoint, token, args.query_timeout, query_duration,
expected_hostcount),
nagiosplugin.ScalarContext("other-doc-count", warning="0"),
nagiosplugin.ScalarContext("heartbeat-age",
warning=args.warning,
critical=args.critical),
nagiosplugin.ScalarContext("heartbeat-latency",
warning=args.latency_warning,
critical=args.latency_critical),
NodeNameContext("nodes", args.nodes),
)
check.main(verbose=args.verbose, timeout=None)
if __name__ == "__main__":
main()
# vim: set sw=2 sts=2 et :