-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_mount.pl
executable file
·298 lines (239 loc) · 8.44 KB
/
check_mount.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
#!/usr/bin/perl -w
# check_mount.pl
#---------------------------------------------------------------------------
# Author(s): hbrennhaeuser
# Created: Sep 15, 2023
# Last modified: Sep 15, 2023
# License: GPL v3
# Version: 1.0.1
#
# License information:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Changelog:
# v1.0.1 - hbrennhaeuser, Sep 15, 2023
# Minor Changes
# v1.0.0 - hbrennhaeuser, Sep 15, 2023
# Initial commit
#---------------------------------------------------------------------------
use warnings;
use strict;
use Getopt::Long;
# This plugin is intentionally using as few libraries as possible.
our $VERSION = '1.0.1';
BEGIN{
use warnings;
use strict;
package Check_mount::Mountpoint;
sub new {
my $class = shift;
my $self = {
'mountline' => shift,
'os' => shift,
};
bless $self, $class;
$self->{'mountline'} = $self->trim_whitespaces($self->{'mountline'});
if ( $self->{'os'} eq 'linux' ){
die('invalid mount output') if ( ! $self->validate_mountline_linux() );
$self->parse_mountline_linux();
} elsif ( $self->{'os'} eq 'bsd' ){
die('BSD is not supported yet!');
die('invalid mount output') if ( ! $self->validate_mountline_bsd() );
$self->parse_mountline_bsd();
} else {
die('Unsupported/unknown os '.$self->{'os'})
}
return $self;
}
sub trim_whitespaces {
my ( $self, $string ) = @_;
$string =~ s/^\s*//msx;
$string =~ s/\s*$//msx;
return $string;
}
sub validate_mountline_linux {
my ( $self ) =@_;
my $status = 0;
$status = 1 if ( $self->{'mountline'} =~ m/^.*\son\s.*\stype\s.*\s\(.*\)$/ixms);
return $status;
}
sub parse_mountline_linux {
my ( $self ) = @_;
if ( $self->{'mountline'} =~ qr/^(.+)\son\s/ixms ){
$self->{'dev'}= $1;
} else {
die('An error occured parsing dev from mountline! mountline: '.$self->{'mountline'});
}
if ( $self->{'mountline'} =~ qr/^(?:.*\son\s)(.+)(?:\stype)/ixms ){
$self->{'mountpoint'}= $1;
} else {
die('An error occured parsing mountpoint from mountline! mountline: '.$self->{'mountline'});
}
if ( $self->{'mountline'} =~ qr/^(?:.*\stype\s)(\S+)(?:\s\()/ixms ){
$self->{'fstype'}= $1;
}else {
die('An error occured parsing fstype from mountline! mountline: '.$self->{'mountline'});
}
if ( $self->{'mountline'} =~ qr/^(?:.*\stype\s)(?:\S+\s\()(.+)(?:\))$/ixms ){
$self->{'params'}= $1;
} else {
die('An error occured parsing params from mountline! mountline: '.$self->{'mountline'});
}
return 1;
}
sub validate_mountline_bsd {
my ( $self ) = @_;
die('BSD is not supported yet!');
}
sub parse_mountline_bsd {
my ( $self ) = @_;
die('BSD is not supported yet!');
}
sub check_dev {
my ( $self, $path ) = @_;
my $status = 0;
if ( $self->{'dev'} eq $path ){
$status = 1;
}
return $status
}
sub check_fstype {
my ( $self, $expected ) = @_;
my $status = 0;
if ( $self->{'fstype'} eq $expected ){
$status = 1;
}
return $status;
}
sub check_params {
my ( $self, $params, $mode ) = @_;
# Modes:
# 1: Expect the @$params to 100% match
# 2: Expect all the defined params in @$params to be there, the rest is ignored
# 3: Blacklist, check if none of the defined params are there
my $status = 0;
my $params_array = split( /,/ , $self->{'params'});
die('param-checking is not implemented yet!');
return $status;
}
sub check_mountpoint {
my ( $self, $path ) = @_;
my $status = 0;
if ( $self->{'mountpoint'} eq $path ){
$status = 1;
}
return $status;
}
}
#===========================================================================
my ($MP_OK, $MP_WARNING, $MP_CRITICAL, $MP_UNKNOWN) = (0,1,2,3);
my $ec = $MP_OK;
my $et = '';
my $os = 'linux';
# --
sub set_ec{
my ($ec_current, $ec_new) = @_;
my $ec_return = $ec_current;
$ec_return = $ec_new if ($ec_new > $ec_current);
return $ec_return;
}
sub print_help{
print("check_mount.pl $VERSION
This plugin is licensed under the terms of the GNU General Public License Version 3,you will find a copy of this license in the LICENSE file included in the source package.
check_mount.pl lets you check a mountpoint by specifying a mountpoint, a mounted source or both. Additionally you can check the used filesystem.
The plugin evaluates the output of the 'mount'-command. It is currently only compatible with the linux-variant (util-linux), support for bsd-variants still under development.
SYNTAX: check_mount.sh < -s SOURCE | -m MOUNTPOINT > [-f FileSystem]
-m <MOUNTPOINT> - Mountpoint (e.g. /mnt/datashare)
-s <SOURCE> - Mounted Source (e.g. /dev/sdb or //192.168.163.25/share)
-f <FileSystem> - [optional] Filesystem (e.g. ext4, nfs, cifs, ...)
--bsd - [optional] Evaluate bsd-mount instead of linux (default) [Not implemented yet]
Either -m or -s has to be given. Both can be specified simultaneously. -f is optional.
");
exit($MP_UNKNOWN);
}
# --
Getopt::Long::Configure(qw( gnu_getopt ignore_case_always ));
Getopt::Long::GetOptions(
'source|s=s' => \my $opt_source,
'mountpoint|m=s' => \my $opt_mountpoint,
'fs|f=s' => \my $opt_filesystem,
'bsd' => \my $opt_bsd,
'help|h' => \my $opt_help,
);
print_help() if (defined($opt_help));
if ( !defined($opt_source) && !defined($opt_mountpoint)){
print("Please specify either mountpoint or source to check!\n"); exit($MP_UNKNOWN);
}
if ( defined($opt_bsd) ){
$os = 'bsd';
}
# --
my @mount_output = `mount`;
my @mounts=();
foreach my $line (@mount_output){
my $obj = Check_mount::Mountpoint->new($line,$os);
if ( defined($opt_mountpoint) && defined($opt_source)){
if ($obj->check_mountpoint($opt_mountpoint) && $obj->check_dev($opt_source)){
push(@mounts,$obj);
}
} elsif (defined($opt_mountpoint) && !defined($opt_source)) {
if ($obj->check_mountpoint($opt_mountpoint)){
push(@mounts,$obj);
}
} elsif (!defined($opt_mountpoint) && defined($opt_source)) {
if ($obj->check_dev($opt_source)){
push(@mounts,$obj);
}
}
}
if ( @mounts == 1 ){
$et.=' is mounted';
} elsif ( @mounts == 0 ){
$et.=' is not mounted';
$ec=set_ec($ec,$MP_CRITICAL)
} elsif ( @mounts > 1 ){
$et.=' is mounted '.(@mounts).' times';
$ec=set_ec($ec,$MP_WARNING);
}
if ( defined($opt_mountpoint) && defined($opt_source)){
$et=$opt_source.$et.' on '.$opt_mountpoint;
} elsif (defined($opt_mountpoint) && !defined($opt_source)) {
$et=$opt_mountpoint.$et;
} elsif (!defined($opt_mountpoint) && defined($opt_source)) {
$et=$opt_source.$et;
}
if ( @mounts == 1 && defined($opt_filesystem)){
if ($mounts[0]->check_fstype($opt_filesystem)){
$et=$et." as $opt_filesystem";
} else {
$et=$et.' as '.$mounts[0]->{'fstype'}." (not $opt_filesystem)";
$ec=set_ec($ec,$MP_CRITICAL);
}
}
if (@mounts >=1 ){
foreach my $mountpoint (@mounts){
$et.="\n* ".$mountpoint->{'dev'}.' on '.$mountpoint->{'mountpoint'}.' as '.$mountpoint->{'fstype'};
}
}
if ( $ec eq $MP_OK ){
$et = 'OK: '.$et;
} elsif ( $ec eq $MP_WARNING ){
$et = 'WARNING: '.$et;
} elsif ( $ec eq $MP_CRITICAL ){
$et = 'CRITICAL: '.$et;
} else {
$et = 'UNKNOWN: '.$et;
}
print($et."\n");
exit($ec);