blib/lib/Monitoring/Sneck/Checks/mount.pm

filter: Statementstmt 98.7% 79 / 80 Branchbran 87.5% 28 / 32 Conditioncond 87.5% 21 / 24 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 93.2% 153 / 164 CC 28 SCAR 21.9
CC
28
Cov
94%
check
15.0
_parse
9.1
mounts
4.2
CRAP 8.9
« blib/lib/Monitoring/Sneck/Checks/cert_dir.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/reboot_needed.pm »
1 package Monitoring::Sneck::Checks::mount;
2  
3 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::mount - checks file systems are mounted and writable
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 4 our $VERSION = '0.0.1';
20  
21 # Where things are. Set by tests.
22 4 our $FSTAB = '/etc/fstab';
23 4 our $MOUNT = '/sbin/mount';
24  
25 # Types in fstab that are not mounted at a path.
26 4 our %NOT_MOUNTED_TYPES = map { $_ => 1 } ( 'swap', 'ignore' );
27  
28 =head1 SYNOPSIS
29  
30 mounts|perl:mount
31 data_mounted|perl:mount --no-fstab -p /data -p /backup
32 mounts_not_cd|perl:mount -i /media/cdrom
33  
34 =head1 DESCRIPTION
35  
36 Checks every file system in fstab is mounted, and that those not set
37 ro there are not mounted read only, as one going read only after
38 errors often goes unnoticed. Either is CRITICAL. Paths given with
39 --path must be mounted too, but are not checked for being read only.
40  
41 Swap is left alone. Entries that are noauto or x-systemd.automount are
42 not expected to be mounted, though they are checked for being read
43 only if they are.
44  
45 Mounts are read from /proc/mounts on Linux and mount -p on FreeBSD.
46 Other OSes are UNKNOWN, as is fstab not being readable unless
47 --no-fstab is given.
48  
49 =head1 OPTIONS
50  
51 - -p, --path :: A path that must be a mount point. May be given more
52 than once.
53  
54 - -i, --ignore :: A mount point in fstab to leave alone. May be given
55 more than once.
56  
57 - -n, --no-fstab :: Only check --path, which must then be given.
58  
59 =cut
60  
61 16 ▶sub option_spec { return ( 'path|p=s@', 'ignore|i=s@', 'no-fstab|n' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
62  
63 16 ▶sub validate {
Subroutine validate: called CC=3 SCAR=11.0
Pod documented
64 16 my ( $self, $options ) = @_;
65  
66 16 ▶ return ('--no-fstab needs --path') if $options->{'no-fstab'} && !defined( $options->{path} );
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"no-fstab"} and not defined $options->{"path"} 2 14 16
Condition100%3/3
$options->{"no-fstab"} and not defined $options->{"path"}
!ll&&!rl&&r
1222
MC/DC100%2/2
$options->{"no-fstab"} and not defined $options->{"path"}
$options->{"no-fstab"} not defined $options->{"path"}
Truth table100%3/3
A and not(B)
A= $options->{"no-fstab"}B= not defined $options->{"path"}
ABresult
0X0
100
111
67  
68 14 return ();
69 }
70  
71 =head1 METHODS
72  
73 =head2 check
74  
75 Does the check.
76  
77 =cut
78  
79 14 ▶sub check {
Subroutine check: called CC=15 SCAR=27.1
Pod documented
80 14 my ($self) = @_;
81  
82 14 my %mounted = map { $_->{path} => $_ } $self->mounts;
83 13 ▶ my %ignore = map { _normal_path($_) => 1 } @{ $self->opt('ignore') // [] };
Condition100%2/2
$self->opt("ignore") // []
l!l
49
MC/DC100%1/1
$self->opt("ignore") // []
$self->opt("ignore")
Truth table100%2/2
A
A= $self->opt("ignore")
Aresult
11
00
84 13 my @wanted;
85 13 ▶ if ( !$self->opt('no-fstab') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->opt("no-fstab")) 11 2 13
86 11 my $fstab = read_file( $FSTAB, err_mode => 'quiet' );
87 11 ▶ return $self->finish( $self->UNKNOWN, 'can not read ' . $FSTAB . ', ' . $! ) if !defined($fstab);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fstab 2 9 11
88 9 push( @wanted, grep { !$ignore{ $_->{path} } } _parse($fstab) );
89 }
90 11 ▶ push( @wanted, map { { path => _normal_path($_), options => {}, given => 1 } } @{ $self->opt('path') // [] } );
Condition100%2/2
$self->opt("path") // []
l!l
56
MC/DC100%1/1
$self->opt("path") // []
$self->opt("path")
Truth table100%2/2
A
A= $self->opt("path")
Aresult
11
00
91  
92 11 my @problems;
93 11 my $checked = 0;
94 11 my %seen;
95 11 foreach my $want (@wanted) {
96 38 ▶ next if $seen{ $want->{path} }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$want->{"path"}}++ 0 38 38
97 38 my $options = $want->{options};
98 38 my $mount = $mounted{ $want->{path} };
99 38 ▶ my $lazy = $options->{noauto} || $options->{'x-systemd.automount'};
Condition100%3/3
$options->{'noauto'} || $options->{'x-systemd.automount'}
l!l&&r!l&&!r
8822
MC/DC100%2/2
$options->{'noauto'} || $options->{'x-systemd.automount'}
$options->{'noauto'} $options->{'x-systemd.automount'}
Truth table100%3/3
A || B
A= $options->{'noauto'}B= $options->{'x-systemd.automount'}
ABresult
1X1
011
000
100 38 ▶ if ( !defined($mount) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mount) 14 24 38
101 14 ▶ push( @problems, $want->{path} . ' is not mounted' ) if !$lazy;
Branch: 1 branch
BranchTrueFalseTotal
unless $lazy 4 10 14
102 14 next;
103 }
104 24 $checked++;
105 24 ▶ if ( $mount->{options}{ro} && !$options->{ro} && !$want->{given} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mount->{"options"}{"ro"} and not $options->{"ro"} and not $want->{"given"}) 2 22 24
Condition83%5/6
$mount->{"options"}{"ro"} and not $options->{"ro"} and not $want->{"given"}
!ll&&!rl&&r
2202
$mount->{"options"}{"ro"} and not $options->{"ro"}
!ll&&!rl&&r
1662
MC/DC66%2/3
$mount->{"options"}{"ro"} and not $options->{"ro"} and not $want->{"given"}
$mount->{"options"}{"ro"} not $options->{"ro"} not $want->{"given"}
Truth table75%3/4
A and not(B) and not(C)
A= $mount->{"options"}{"ro"}B= not $options->{"ro"}C= not $want->{"given"}
ABCresult
0XX0
10X0
1100
1111
106 2 push( @problems, $want->{path} . ' is read only' );
107 }
108 } ## end foreach my $want (@wanted)
109  
110 11 my $message = $checked . ' mounted';
111 11 ▶ $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 4 7 11
112  
113 11 ▶ return $self->finish( defined( $problems[0] ) ? $self->CRITICAL : $self->OK, $message );
Branch: 1 branch
BranchTrueFalseTotal
defined $problems[0] ? : 4 7 11
114 } ## end sub check
115  
116 =head2 mounts
117  
118 Returns what is mounted, as hash refs of path and options, a hash ref
119 of each option to its value, or 1. Dies if it can not be found.
120  
121 =cut
122  
123 14 ▶sub mounts {
Subroutine mounts: called CC=4 SCAR=14.2
Pod documented
124 14 my ($self) = @_;
125  
126 14 ▶ if ( $self->os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux") 12 2 14
127 12 return _parse( $self->proc_file('mounts'), 1 );
128 }
129 2 ▶ if ( $self->os eq 'freebsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "freebsd") 2 0 2
130 2 my ( $exit, $output ) = $self->run_command( $MOUNT, '-p' );
131 1 ▶ die( $MOUNT . ' -p exited ' . $exit . "\n" ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 1 1
132 1 return _parse( $output, 1 );
133 }
134  
135 0 die( 'not supported on ' . $self->os . "\n" );
136 } ## end sub mounts
137  
138 # Parses fstab, or something in the same format, such as /proc/mounts
139 # and mount -p.
140 #
141 # Args...
142 #
143 # - content :: The text.
144 #
145 # - all :: 1 to keep everything, for what is mounted, else entries
146 # not mounted at a path, such as swap, are left out.
147 #
148 # Returns a list of hash refs of device, path, type, and options, a hash
149 # ref of each option to its value, or 1. \040 and the like in paths
150 # are turned into what they stand for.
151 #
152 # Example...
153 #
154 # _parse("/dev/ada0p2 / ufs rw,noatime 1 1\n");
155 # # ( { device => '/dev/ada0p2', path => '/', type => 'ufs', options => { rw => 1, noatime => 1 } } )
156 22 ▶sub _parse {
Subroutine _parse: called CC=9 SCAR=22.1
157 22 my ( $content, $all ) = @_;
158  
159 22 my @entries;
160 22 foreach my $line ( split( /\n/, $content ) ) {
161 98 ▶ next if $line =~ /\A\s*(?:#|\z)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*(?:#|\z)/u 8 90 98
162 90 my ( $device, $path, $type, $options ) = split( /\s+/, $line =~ s/\A\s+//r );
163 90 ▶ next if !defined($type);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 90 90
164 90 $path = _normal_path( $path =~ s/\\([0-7]{3})/chr(oct($1))/ger );
165 90 ▶ next if !$all && ( $NOT_MOUNTED_TYPES{$type} || $path !~ m{\A/} );
Branch: 1 branch
BranchTrueFalseTotal
if not $all and $Monitoring::Sneck::Checks::mount::NOT_MOUNTED_TYPES{$type} || !($path =~ m[\A/]u) 9 81 90
Condition83%5/6
not $all and $Monitoring::Sneck::Checks::mount::NOT_MOUNTED_TYPES{$type} || !($path =~ m[\A/]u)
!ll&&!rl&&r
40419
$Monitoring::Sneck::Checks::mount::NOT_MOUNTED_TYPES{$type} or not $path =~ m[\A/]u
l!l&&r!l&&!r
9041
MC/DC66%2/3
not $all and $Monitoring::Sneck::Checks::mount::NOT_MOUNTED_TYPES{$type} || !($path =~ m[\A/]u)
not $all $Monitoring::Sneck::Checks::mount::NOT_MOUNTED_TYPES{$type} not $path =~ m[\A/]u
Truth table75%3/4
not(A) and B or not(C)
A= not $allB= $Monitoring::Sneck::Checks::mount::NOT_MOUNTED_TYPES{$type}C= not $path =~ m[\A/]u
ABCresult
0XX0
1000
11X1
1011
166 81 ▶ my %options = map { /\A([^=]+)=(.*)\z/ ? ( $1 => $2 ) : ( $_ => 1 ) } split( /,/, $options // '' );
Branch: 1 branch
BranchTrueFalseTotal
/\A([^=]+)=(.*)\z/u ? : 10 116 126
Condition50%1/2
$options // ""
l!l
810
MC/DC0%0/1
$options // ""
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
167 81 push( @entries, { device => $device, path => $path, type => $type, options => \%options } );
168 }
169  
170 22 return @entries;
171 } ## end sub _parse
172  
173 # Drops a trailing / from a path, other than for /.
174 #
175 # Args...
176 #
177 # - path :: The path.
178 #
179 # Returns the path.
180 #
181 # Example...
182 #
183 # _normal_path('/data/'); # '/data'
184 103 ▶sub _normal_path {
Subroutine _normal_path: called CC=1 SCAR=0.0
185 103 my ($path) = @_;
186  
187 103 $path =~ s{(?<=.)/+\z}{};
188  
189 103 return $path;
190 }
191  
192 4 1;
« blib/lib/Monitoring/Sneck/Checks/cert_dir.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/reboot_needed.pm »