-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcheck_elasticsearch_cluster.pl
executable file
·336 lines (286 loc) · 9.71 KB
/
check_elasticsearch_cluster.pl
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env perl
# Copyright (c) 2013-, Simon Lundström <[email protected]>, IT Services, Stockholm University
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# Neither the name of Stockholm University nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
use strict;
use warnings;
use JSON;
sub load_module {
for my $module (@_) {
eval "use $module";
return $module if !$@;
}
die $@;
}
my $monitoring_plugin;
BEGIN {
$monitoring_plugin = load_module('Nagios::Plugin', 'Monitoring::Plugin');
}
use Data::Dumper;
use LWP::UserAgent;
my $np = $monitoring_plugin->new(
shortname => "#",
usage => "Usage: %s [-v|--verbose] [-t <timeout>] [--critical=<value to emit critical>] [--warning=<value to emit warning>] --one-of-the-checks-below",
version => "1.3.1",
timeout => 10,
extra => qq(
See <https://nagios-plugins.org/doc/guidelines.html#THRESHOLDFORMAT> for
information on how to use thresholds.
The STATUS label can have three values:
* green - All primary and replica shards are allocated. Your cluster is 100%
operational.
* yellow - All primary shards are allocated, but at least one replica is
missing. No data is missing, so search results will still be complete. However,
your high availability is compromised to some degree. If more shards disappear,
you might lose data. Think of yellow as a warning that should prompt
investigation.
* red - At least one primary shard (and all of its replicas) are missing. This
means that you are missing data: searches will return partial results, and
indexing into that shard will return an exception.
The defaults has been been taken from
<https://www.elastic.co/guide/en/elasticsearch/guide/current/_cluster_health.html>
),
);
$np->add_arg(
spec => 'cluster-status',
help => "--cluster-status\n Check the status of the cluster.",
);
$np->add_arg(
spec => 'index-status',
help => "--index-status\n Check the status of the indexes.",
);
$np->add_arg(
spec => 'nodes-online',
help => "--nodes-online\n Check the number of nodes online.",
);
$np->add_arg(
spec => 'split-brain',
help => "--split-brain\n Check if the cluster has a split-brain.",
);
$np->add_arg(
spec => 'warning|w=s',
help => [
'Set the warning threshold in INTEGER (applies to nodes-online)',
'Set the warning threshold in STATUS (applies to cluster-status and index-status)',
],
label => [ 'INTEGER', 'STATUS' ],
);
$np->add_arg(
spec => 'critical|c=s',
help => [
'Set the critical threshold in INTEGER (applies to nodes-online)',
'Set the critical threshold in STATUS (applies to cluster-status and index-status)',
],
label => [ 'INTEGER', 'STATUS' ],
);
$np->add_arg(
spec => 'url=s',
help => "--url\n URL to your Elasticsearch instance. (default: %s)",
default => 'http://localhost:9200',
);
$np->add_arg(spec => 'username|user|u=s',
help => "Username for authentication",
default => "",
);
$np->add_arg(spec => 'password|p=s',
help => "Password for authentication",
default => ""
);
$np->getopts;
my %ES_STATUS = (
"red" => 1,
"yellow" => 2,
"green" => 3,
);
my ($warning, $critical) = ($np->opts->warning, $np->opts->critical);
my $code;
my $json;
# Turns an array into "first, second & last"
sub pretty_join {
my ($a) = @_;
return @{$a}[0] if $#{$a} == 0;
return "" if $#{$a} == -1;
return join(', ', @{$a}[0..$#{$a}-1]).
' & '.@{$a}[$#{$a}];
}
# Checks the status of "something"
sub check_status($$) {
$code = $np->check_threshold(
check => (ref $_[0] eq "HASH") ? $ES_STATUS{$_[0]->{status}} : $ES_STATUS{$_[0]},
warning => "\@$ES_STATUS{$warning}",
critical => "\@$ES_STATUS{$critical}",
);
$np->add_message($code, $_[1]);
}
sub get_threshold_value {
my ($thresh, $value, $key) = @_;
if (ref $thresh eq 'CODE') {
return $thresh->($value, $key);
}
else {
return $thresh;
}
}
# Check a data structure with check_threshold.
# TODO Make sure it works recursively
sub check_each($$$$$) {
my %statuses;
my ($what, $where, $warning, $critical, $message) = @_;
# Run check_threshold on everything
foreach my $k (keys %$what) {
my $current_key = $where->($what->{$k});
my $warn = get_threshold_value($warning, $what->{$k}, $k);
my $crit = get_threshold_value($critical, $what->{$k}, $k);
my $code = $np->check_threshold(
check => $current_key,
warning => $warn,
critical => $crit,
);
# and put in in a hash where the status is the key and the value an array
# of the keys with that status
push @{$statuses{$code}}, $k;
}
for my $code (keys %statuses) {
# We don't care about OK checks, but add messages about everything else.
if ($code ne 0 && $statuses{$code}) {
$np->add_message($code, $message.pretty_join($statuses{$code}));
}
}
}
sub clean_extra_chars($) {
my ($ret) = @_;
$ret =~ s/[^\d\w]//g;
return $ret;
}
sub to_threshold($$) {
my ($ret, $original) = @_;
$ret =~ s/[\d\w]+%?/$original/;
return $ret;
}
sub decode_and_check_json {
my %opt = @_;
# Try to parse the JSON
my $json;
eval {
$json = decode_json($opt{json});
};
if ($@) {
$opt{np}->nagios_exit(CRITICAL, "JSON was invalid: $@");
}
return $json;
}
my $ua = LWP::UserAgent->new;
# NRPE timeout is 10 seconds, give us 1 second to run
$ua->timeout($np->opts->timeout-1);
my $url;
if ($np->opts->get('split-brain')) {
$url = $np->opts->url."/_cluster/state/master_node,nodes?pretty";
}
else {
# Time out 1 second before LWP times out.
$url = $np->opts->url."/_cluster/health?level=shards&timeout=".($np->opts->timeout-2)."s&pretty";
}
my $req = HTTP::Request->new(GET => $url);
# Username and Password are defined for basic auth
if ($np->opts->username and $np->opts->password) {
$req->authorization_basic($np->opts->username, $np->opts->password);
}
my $resp = $ua->request($req);
if (!$resp->is_success) {
$np->nagios_exit(CRITICAL, $resp->status_line);
}
$json = $resp->decoded_content;
$json = decode_and_check_json(json => $json, np => $np);
# Check that the cluster query didn't time out
if (defined $json->{timed_out} && $json->{timed_out}) {
$np->nagios_exit(CRITICAL, "Connection to cluster timed out!");
}
# Check the status of the cluster.
if ($np->opts->get('cluster-status')) {
# Set defaults
$warning = $warning || "yellow";
$critical = $critical || "red";
check_status($json, "Cluster $json->{cluster_name} is $json->{status}");
}
# Check the status of the cluster.
elsif ($np->opts->get('index-status')) {
# Set defaults
$warning = $warning || '@yellow';
$critical = $critical || '@red';
check_each($json->{indices},
sub {
my ($f) = @_;
return $ES_STATUS{$f->{status}};
},
to_threshold($warning, $ES_STATUS{clean_extra_chars($warning)}),
to_threshold($critical, $ES_STATUS{clean_extra_chars($critical)}),
"Indexes with issues: "
);
}
# Check that we have the number of nodes we prefer online.
elsif ($np->opts->get('nodes-online')) {
# Set defaults
$warning = $warning || '3:';
$critical = $critical || "2:";
$code = $np->check_threshold(
check => $json->{number_of_nodes},
warning => $warning,
critical => $critical,
);
$np->add_message($code, "Nodes online: $json->{number_of_nodes}");
}
# Check for split-brain of the cluster
elsif ($np->opts->get('split-brain')) {
my $master_node = $json->{master_node};
$req->uri->query($req->uri->query."&local=true");
my $header_host = $req->uri->host;
for my $node (keys %{$json->{nodes}}) {
my ($ip) = split(/:/, $json->{nodes}->{$node}->{transport_address});
my $uri = $req->uri;
$uri->host($ip);
$req->uri($uri);
# Let's use the original host as Host header to get "vhost support" since
# we connect directly to the IP and as the CN to verify in SSL
$req->header(Host => $header_host);
$ua->ssl_opts(SSL_verifycn_name => $header_host);
my $resp = $ua->request($req);
if (!$resp->is_success) {
$np->nagios_exit(CRITICAL, $resp->status_line.($resp->header("client-warning") eq "Internal response" ? " ".join(" ", split(/\n+/, $resp->decoded_content)) : ""));
}
my $node_json = $resp->decoded_content;
$node_json = decode_and_check_json(json => $node_json, np => $np);
if ($master_node ne $node_json->{master_node}) {
$np->nagios_exit(CRITICAL, "node=$node ip=$ip has splitbrain! It thinks $node_json->{master_node} is master but it's $master_node");
}
}
}
else {
exec ($0, "--help");
}
($code, my $message) = $np->check_messages();
$np->nagios_exit($code, $message);