forked from skyscrapers/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_ro_mounts
executable file
·140 lines (118 loc) · 3.76 KB
/
check_ro_mounts
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
#!/usr/bin/perl -w
# check_ro_mounts.pl Copyright (c) 2008 Valentin Vidic <[email protected]>
#
# Checks the mount table for read-only mounts - these are usually a sign of
# trouble (broken filesystem etc.)
#
#
# 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 2
# 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 (or with Nagios); if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA
use strict;
use Getopt::Long;
use lib "/usr/lib/nagios/plugins";
use utils qw (%ERRORS &support);
my $name = 'RO_MOUNTS';
my $mtab = '/proc/mounts';
my @includes = ();
my @excludes = ();
my @excluded_types = ();
my @ro_mounts = ();
my $want_help = 0;
Getopt::Long::Configure(qw(no_ignore_case));
my $res = GetOptions(
"help|h" => \$want_help,
"mtab|m=s" => \$mtab,
"path|p=s" => \@includes,
"partition=s" => \@includes,
"exclude|x=s" => \@excludes,
"exclude-type|X=s" => \@excluded_types,
);
if ($want_help or !$res) {
print_help();
exit $ERRORS{$res ? 'OK' : 'UNKNOWN'};
}
my $includes_re = globs2re(@includes);
my $excludes_re = globs2re(@excludes);
my $excluded_types_re = globs2re(@excluded_types);
open(MTAB, $mtab) or nagios_exit(UNKNOWN => "Can't open $mtab: $!");
MOUNT: while (<MTAB>) {
# parse mtab lines
my ($dev, $dir, $fs, $opt) = split;
my @opts = split(',', $opt);
# check includes/excludes
if ($includes_re) {
next MOUNT unless $dev =~ qr/$includes_re/
or $dir =~ qr/$includes_re/;
}
if ($excludes_re) {
next MOUNT if $dev =~ qr/$excludes_re/
or $dir =~ qr/$excludes_re/;
}
if ($excluded_types_re) {
next MOUNT if $fs =~ qr/$excluded_types_re/;
}
# check for ro option
if (grep /^ro$/, @opts) {
push @ro_mounts, $dir;
}
}
nagios_exit(UNKNOWN => "Read failed on $mtab: $!") if $!;
close(MTAB) or nagios_exit(UNKNOWN => "Can't close $mtab: $!");
# report findings
if (@ro_mounts) {
nagios_exit(CRITICAL => "Found ro mounts: @ro_mounts");
} else {
nagios_exit(OK => "No ro mounts found");
}
# convert glob patterns to a RE (undef if no patterns)
sub globs2re {
my(@patterns) = @_;
@patterns or return undef;
foreach (@patterns) {
s/ \\(.) / sprintf('\x%02X', ord($1)) /egx;
s/ ([^\\*?\w]) / sprintf('\x%02X', ord($1)) /egx;
s/\*/.*/g;
s/\?/./g;
}
return '\A(?:' . join('|', @patterns) . ')\z';
}
# output the result and exit plugin style
sub nagios_exit {
my ($result, $msg) = @_;
print "$name $result: $msg\n";
exit $ERRORS{$result};
}
sub print_help {
print <<EOH;
check_ro_mounts 0.1
Copyright (c) 2008 Valentin Vidic <vvidic\@carnet.hr>
This plugin checks the mount table for read-only mounts.
Usage:
check_ro_mounts [-m mtab] [-p path] [-x path] [-X type]
Options:
-h, --help
Print detailed help screen
-m, --mtab=FILE
Use this mtab instead (default is /proc/mounts)
-p, --path=PATH, --partition=PARTITION
Glob pattern of path or partition to check (may be repeated)
-x, --exclude=PATH <STRING>
Glob pattern of path or partition to ignore (only works if -p unspecified)
-X, --exclude-type=TYPE <STRING>
Ignore all filesystems of indicated type (may be repeated)
EOH
support();
}
# vim:sw=4:ts=4:et