-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_glusterfs.pl
356 lines (298 loc) · 9.47 KB
/
check_glusterfs.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/perl
#
# $Id: check_glusterfs.pl 508 2017-01-31 16:01:01Z phil $
#
# program: check_glusterfs
# author, (c): Philippe Kueck <projects at unixadm dot org>
#
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
$|++;
my ($vols, $status, @out, @perfdata);
my $s = {'b' => 0, 'kb' => 10, 'mb' => 20, 'gb' => 30, 'tb' => 40};
my $thrs = {
'diskwarn' => 90,
'diskcrit' => 95,
'inodewarn' => 90,
'inodecrit' => 95,
'volume' => 'all',
'perfdata' => 0
};
sub x_crit {$status = 2 if $status < 2}
sub x_warn {$status = 1 if $status < 1}
sub nagexit {
my $exitc = {0 => 'OK', 1 => 'WARNING', 2 => 'CRITICAL', 3 => 'UNKNOWN'};
printf "%s - %s%s\n", $exitc->{$_[0]}, $_[1],
($thrs->{'perfdata'}?"|".join ' ', @perfdata:"");
exit $_[0]
}
Getopt::Long::Configure("no_ignore_case");
GetOptions(
'H=s' => sub {},
'w|diskwarn=i' => \$thrs->{'diskwarn'},
'c|diskcrit=i' => \$thrs->{'diskcrit'},
'W|inodewarn=i' => \$thrs->{'inodewarn'},
'C|inodecrit=i' => \$thrs->{'inodecrit'},
'l|volume=s' => \$thrs->{'volume'},
'p|perfdata' => \$thrs->{'perfdata'},
'f|warnonfailedheal' => \$thrs->{'warnonfailedheal'},
'h|help' => sub {pod2usage({'-exitval' => 3, '-verbose' => 2})}
) or pod2usage({'-exitval' => 3, '-verbose' => 0});
sub fetchinfo {
my ($v, $gh, $bi, @types);
open $gh, "gluster volume info $thrs->{'volume'}|" or die $@;
while (<$gh>) {
$v = $1 if /^Volume Name: (.+)$/;
next unless $v;
if (/^Status: ((?:Start|Stopp|Creat)ed)$/) {
$vols->{$v}->{'status'} = $1; next
}
if (/^Type: (.+)$/) {
# Remember the order of the types
@types = ();
foreach my $t ( split("-", $1) ) {
$t =~ s/d$//g;
push @types, $t,
}
# there are always 2 values in the info about brick numbers
unshift @types, "Distribute"
if (scalar(@types) == 1);
next
}
if (my @tmp = /^Number of Bricks:(?: (\d+) x (\d+)(?: x (\d+))? =)? \d+$/) {
# set the number of bricks per type
@tmp = grep { defined $_ && $_ ne '' } @tmp;
for (my $i = 0; $i <= $#types; $i++) {
if (! defined($tmp[$i])) {
nagexit 3, "Cannot read architecture of volume '$v': $_ with types '".join(',', @types)."'";
}
$vols->{$v}->{'type'}->{ $types[$i] } = $tmp[$i];
}
next
}
if (my @tmp = /^Brick(\d+): ([\w._-]+:\/.+?)(?: +\([a-z]+\))?$/) {
$vols->{$v}->{'bricks'}->{$2}->{'online'} = 0;
$vols->{$v}->{'bricks'}->{$2}->{'index'} = $1;
next
}
}
close $gh;
undef $v;
open $gh, "gluster volume status $thrs->{'volume'} detail 2>&1|" or die $@;
while (<$gh>) {
nagexit 3, "another copy might be running, cannot continue"
if /^Another transaction .+ in progress/;
if (/^Status of volume: (.+)$/) {$v = $1; undef $bi; next}
next unless $v;
if (/^Brick\s+: Brick ([\w._-]+:\/.*)$/) {$bi = $1; next}
next unless defined $bi;
if (/^Online +: ([YN])/) {
$vols->{$v}->{'bricks'}->{$bi}->{'online'} = ($1 eq "Y");
next
}
if (/^(Total)? ?Disk Space (Free)? *: (\d+\.\d+)([TGMK]?B)/) {
$vols->{$v}->{'bricks'}->{$bi}->{lc ($1||$2)} = $3*2**$s->{lc $4};
next
}
if (/^(Free)? ?(Inode)s? (Count)? *: (\d+)/) {
$vols->{$v}->{'bricks'}->{$bi}->{lc $2 . lc ($1||$3)} = $4;
}
}
close $gh
}
sub fetchvolumesizes {
my $units = 'M';
foreach my $v (keys %$vols) {
my @stdout = `df -aB$units`;
@stdout = grep { /localhost:\/$v\s/ } @stdout;
if (scalar(@stdout) == 0) {
# mount it
`mkdir -p /mnt/$v 2>&1 && mount -t glusterfs localhost:/$v /mnt/$v`;
@stdout = `df -aB$units`;
}
foreach (@stdout) {
if (/localhost:\/$v\s+([0-9]+)$units\s+([0-9]+)$units\s+([0-9]+)$units/) {
my ($size, $used, $free) = ($1, $2, $3);
push @perfdata, sprintf "'%s_used'=%d%s;%d;%d;0;%d",
$v,
$used, $units."B",
$size / 100 * $thrs->{'diskwarn'},
$size / 100 * $thrs->{'diskcrit'},
$size;
last;
}
}
}
}
sub fetchheal {
my $bi;
foreach my $v (keys %$vols) {
foreach my $h ('split-brain', 'heal-failed', 'healed') {
open my $gh, "gluster volume heal $v info $h 2>&1|"
or nagexit 3, $@;
while (<$gh>) {
nagexit 3, "another copy might be running, cannot continue"
if /^Another transaction .+ in progress/;
if (/^Brick ([\w._-]+:\/.*)$/) {$bi = $1; next}
next unless $bi;
if (/^Number of entries: (\d+)/) {
push @perfdata, sprintf "'%s_b%d_%s'=%d;0;0;0;",
$v, $vols->{$v}->{'bricks'}->{$bi}->{'index'},
$h, $1;
$vols->{$v}->{'bricks'}->{$bi}->{$h} = $1
}
}
close $gh
}
}
}
sub check_peer_status {
my $hostname;
my $failed_count = 0;
open my $gh, "gluster peer status 2>&1|"
or nagexit 3, $@;
while (<$gh>) {
if (/^Hostname: (.*)$/) {
$hostname = $1;
next;
}
next unless $hostname;
if (/^State: (.+)$/) {
my $state = $1;
if ($state ne 'Peer in Cluster (Connected)') {
push @out, sprintf("Peer '%s' is in non-acceptable state '%s'", $hostname, $state);
$failed_count++;
x_warn if ($failed_count == 1);
x_crit if ($failed_count > 1);
}
}
}
close $gh
}
sub check {
foreach (keys %$vols) {
# warn on stopped volumes
unless ($vols->{$_}->{'status'} eq 'Started') {
if ($vols->{$_}->{'status'} ne 'Created') {
push @out, sprintf "%s stopped", $_;
x_warn
}
next
}
# get brick redundancy
my $offline = 0;
my $redundancy = ($vols->{$_}->{'type'}->{'Replicate'} || 1);
# check bricks
foreach my $brick (sort {$vols->{$_}->{'bricks'}->{$a}->{'index'} >
$vols->{$_}->{'bricks'}->{$b}->{'index'}}
keys %{$vols->{$_}->{'bricks'}}) {
#print STDERR "Brick ".Dumper($vols->{$_}->{'bricks'}->{$brick});
unless ($vols->{$_}->{'bricks'}->{$brick}->{'online'}) {
push @out, sprintf "%s_b%d is offline",
$_, $vols->{$_}->{'bricks'}->{$brick}->{'index'};
if (++$offline > $redundancy - 1) {x_crit}
else {x_warn}
next
}
# should never happen
next unless defined $vols->{$_}->{'bricks'}->{$brick}->{'inodefree'} &&
defined $vols->{$_}->{'bricks'}->{$brick}->{'inodecount'} &&
defined $vols->{$_}->{'bricks'}->{$brick}->{'free'} &&
defined $vols->{$_}->{'bricks'}->{$brick}->{'total'};
# check heal status
if ($thrs->{'warnonfailedheal'} &&
($vols->{$_}->{'bricks'}->{$brick}->{'heal-failed'}||0) > 0) {
push @out, sprintf "%s has %d failed heals",
$_, $vols->{$_}->{'bricks'}->{$brick}->{'heal-failed'};
x_warn
}
# check split brain status
if (($vols->{$_}->{'bricks'}->{$brick}->{'split-brain'}||0) > 0) {
push @out, sprintf "%s has %d split-brains",
$_, $vols->{$_}->{'bricks'}->{$brick}->{'split-brain'};
x_crit
}
# check disk and inode usage
for my $i (
['inodewarn', 'inodecrit', 'inodefree', 'inodecount', 'inodes'],
['diskwarn', 'diskcrit', 'free', 'total', 'diskspace']) {
my $tused = $vols->{$_}->{'bricks'}->{$brick}->{$i->[3]} -
$vols->{$_}->{'bricks'}->{$brick}->{$i->[2]};
my $twarn = $vols->{$_}->{'bricks'}->{$brick}->{$i->[3]} *
$thrs->{$i->[0]}/100;
my $tcrit = $vols->{$_}->{'bricks'}->{$brick}->{$i->[3]} *
$thrs->{$i->[1]}/100;
if ($tused >= $tcrit) {
push @out, sprintf "%s_b%d %s is critical",
$_, $vols->{$_}->{'bricks'}->{$brick}->{'index'},
$i->[4];
x_crit
} elsif ($tused >= $twarn) {
push @out, sprintf "%s_b%d %s warning",
$_, $vols->{$_}->{'bricks'}->{$brick}->{'index'},
$i->[4];
x_warn
}
push @perfdata, sprintf "'%s_b%d_%s'=%d;%d;%d;0;%d",
$_, $vols->{$_}->{'bricks'}->{$brick}->{'index'},
$i->[4], $tused, $twarn, $tcrit,
$vols->{$_}->{'bricks'}->{$brick}->{$i->[3]};
}
}
}
}
eval {foreach (split ':', $ENV{'PATH'}) {die "" if -x "$_/gluster"}};
nagexit 3, "no gluster binary found" unless $@;
fetchinfo;
nagexit 3, "no volumes found" unless scalar keys %$vols;
fetchheal;
$status = 0;
check;
check_peer_status;
fetchvolumesizes if ($thrs->{'perfdata'});
@out = ("Everything is OK")
if (scalar(@out) == 0);
nagexit $status, join ", ", @out
__END__
=head1 NAME
check_glusterfs
=head1 VERSION
$Revision: 508 $
=head1 SYNOPSIS
check_glusterfs [-H HOST] [-p] [-f] [-l VOLUME]
[-w DISKWARN] [-c DISKCRIT]
[-W INODEWARN] [-C INODECRIT]
=head1 OPTIONS
=over 8
=item B<H>
Optional. Dummy for compatibility with Nagios.
=item B<p>,B<perfdata>
Optional. Print perfdata of all or the specified volume. I<Warning>: depending on how many volumes and bricks you have, this may result in a lot of data.
=item B<f>,B<warnonfailedheal>
Optional. Warn if the I<heal-failed> log contains entries. The log can be cleared by restarting C<glusterd>.
=item B<l>,B<volume>
Optional. Only check the specified I<VOLUME>. If B<--volume> is not set, all volumes are checked.
=item B<w>,B<diskwarn>
Optional. Warn if disk usage is above I<DISKWARN>. Defaults to 90 (percent).
=item B<c>,B<diskcrit>
Optional. Return a critical error if disk usage is above I<DISKCRIT>. Defaults to 95 (percent).
=item B<W>,B<inodewarn>
Optional. Warn if inode usage is above I<DISKWARN>. Defaults to 90 (percent).
=item B<C>,B<inodcrit>
Optional. Return a critical error if inode usage is above I<DISKCRIT>. Defaults to 95 (percent).
=back
=head1 DESCRIPTION
This nagios/icinga check script checks the glusterfs volumes, their bricks and the heal logs. If enabled, it returns the per-brick perfdata split-brain, heal-failed, healed, disk used and inodes used.
=head1 CAVEATS
Do B<NOT> run multiple copies of C<check_glusterfs> simultanously in a cluster. All bricks will appear offline.
=head1 DEPENDENCIES
=over 8
=item C<Getopt::Long>
=item C<pod::Usage>
=item C<gluster>
=back
=head1 AUTHOR
Philippe Kueck <projects at unixadm dot org>