-
Notifications
You must be signed in to change notification settings - Fork 26
/
check_mounts
executable file
·110 lines (97 loc) · 2.96 KB
/
check_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
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use Cwd qw/abs_path/;
# TO DO:
# inspect swap
my $valid_fs = get_valid_fstypes();
my $mount_fs = get_mounted_fs();
my $fstab_fs = get_fstab_fs();
#print Dumper($valid_fs);
#print Dumper($mount_fs);
#print Dumper($fstab_fs);
my $errors;
for my $file (keys %$fstab_fs) {
unless ($fstab_fs->{$file}->{'device'} eq $mount_fs->{$file}->{'device'}) {
$errors .= "/etc/fstab reports $file should be mounted on $fstab_fs->{$file}->{'device'}\; ";
$errors .= "/proc/mounts reports $file is mounted on $mount_fs->{$file}->{'device'}\; ";
}
unless ($fstab_fs->{$file}->{'fstype'} eq $mount_fs->{$file}->{'fstype'}) {
$errors .= "/etc/fstab reports $file should be fs type $fstab_fs->{$file}->{'fstype'}\; ";
$errors .= "/proc/mounts reports $file is fs type $mount_fs->{$file}->{'fstype'}\; ";
}
unless ($fstab_fs->{$file}->{'writeable'} eq $mount_fs->{$file}->{'writeable'}) {
$errors .= "/etc/fstab reports $file should be writeable = $fstab_fs->{$file}->{'writeable'} (1=rw,0=ro)\; ";
$errors .= "/proc/mounts reports $file is mounted writeable = $mount_fs->{$file}->{'writeable'} (1=rw,0=ro)\; ";
}
}
if ($errors) {
print "MOUNTS CRITICAL: $errors\n";
exit 2;
} else {
print "MOUNTS OK\n";
exit 0;
}
exit;
sub get_valid_fstypes {
open(IN, '/proc/filesystems') || die "Unable to read: $!";
my %results;
while (<IN>) {
next if /^nodev/;
/^\s+(\w+)$/;
$results{$1} = 1;
}
close(IN);
return \%results;
}
sub get_mounted_fs {
open(IN, '/proc/mounts') || die "Unable to read: $!";
my %results;
while (<IN>) {
my ($spec, $file, $vfstype, $mntops, $freq, $passno) = split(/ /, $_);
next unless ($valid_fs->{$vfstype});
$results{$file} = {
device => ($spec =~ m(/dev/root) ? resolve_rootfs_device() : $spec ),
options => $mntops,
fstype => $vfstype,
writeable => ($mntops =~ /^rw/ ? 1 : 0),
};
}
close(IN);
return \%results;
}
sub resolve_rootfs_device {
my $device;
my @rootfs_stat = stat('/dev/root');
opendir(DIR, '/dev') || die "Unable to opendir: $!";
while ($device = readdir(DIR)) {
my @device_stat = stat('/dev/' . $device);
next unless $rootfs_stat[6] eq $device_stat[6];
return '/dev/' . $device unless $device eq 'root';
}
closedir(DIR);
}
sub resolve_device_label {
my $labeled_devname = shift;
if ($labeled_devname eq '/') {
return resolve_rootfs_device();
} else {
return abs_path('/dev/disk/by-label/' . $labeled_devname);
}
}
sub get_fstab_fs {
open(IN, '/etc/fstab') || die "Unable to read: $!";
my %results;
while (<IN>) {
my ($spec, $file, $vfstype, $mntops, $freq, $passno) = split(/\s+/, $_);
next unless ($valid_fs->{$vfstype});
$results{$file} = {
device => ($spec =~ /^LABEL=(\S+)$/ ? resolve_device_label($1) : $spec),
options => $mntops,
fstype => $vfstype,
writeable => ($mntops =~ /^(defaults|rw)/ ? 1 : 0),
};
}
close(IN);
return \%results;
}