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

filter: Statementstmt 98.8% 82 / 83 Branchbran 92.8% 26 / 28 Conditioncond 69.2% 18 / 26 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 88.6% 149 / 168 CC 34 SCAR 26.1
CC
34
Cov
92%
check
23.3
validate
6.0
blocked
5.0
CRAP 13.6
« blib/lib/Monitoring/Sneck/Checks/sensors.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/interface.pm »
1 package Monitoring::Sneck::Checks::kernel_modules;
2  
3 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Monitoring::Sneck::Checks::kernel_modules - checks kernel modules are or are not loaded
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 7 our $VERSION = '0.0.1';
21  
22 # Where things are. Set by tests.
23 7 our $MODULES = '/lib/modules';
24 our @MODPROBE_DIRS
25 7 = ( '/etc/modprobe.d', '/run/modprobe.d', '/usr/local/lib/modprobe.d', '/usr/lib/modprobe.d', '/lib/modprobe.d' );
26 7 our $KLDSTAT = '/sbin/kldstat';
27  
28 # The running kernel's release, if set. Set by tests.
29 7 our $RELEASE;
30  
31 =head1 SYNOPSIS
32  
33 !no_cramfs|control=CIS-1.1.1.1 framework=CIS|perl:kernel_modules -n cramfs
34 !no_usb_storage||perl:kernel_modules -a usb_storage -a firewire_core
35 zfs_loaded|perl:kernel_modules -p zfs
36  
37 =head1 DESCRIPTION
38  
39 Checks modules are loaded, or are not, as given. Anything not as
40 given is CRITICAL. - and _ are the same in names, as the kernel takes
41 them.
42  
43 --not-loadable goes further than --absent, as CIS wants. The module
44 must not be loaded, and must also have a "install name /bin/false" or
45 "/bin/true" line in modprobe.d, so modprobe can not load it either.
46  
47 On Linux, loaded modules are read from /proc/modules. Built in ones,
48 from modules.builtin, count as loaded for --present and fail --absent
49 and --not-loadable, as they can not be kept out. On FreeBSD, kldstat -q -m is asked
50 about each, and --not-loadable is not supported. Other OSes are
51 UNKNOWN.
52  
53 =head1 OPTIONS
54  
55 - -p, --present :: A module that must be loaded. May be given more
56 than once.
57  
58 - -a, --absent :: A module that must not be loaded. May be given more
59 than once.
60  
61 - -n, --not-loadable :: A module that must not be loaded or loadable.
62 May be given more than once.
63  
64 One or more of --present, --absent, or --not-loadable must be given.
65  
66 =cut
67  
68 23 ▶sub option_spec { return ( 'present|p=s@', 'absent|a=s@', 'not-loadable|n=s@' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
69  
70 23 ▶sub validate {
Subroutine validate: called CC=6 SCAR=17.9
Pod documented
71 23 my ( $self, $options ) = @_;
72  
73 23 my @problems;
74 23 my $any = 0;
75 23 foreach my $name ( 'present', 'absent', 'not-loadable' ) {
76 69 ▶ foreach my $module ( @{ $options->{$name} // [] } ) {
Condition100%2/2
$options->{$name} // []
l!l
4326
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
Aresult
11
00
77 58 $any++;
78 58 ▶ push( @problems, '--' . $name . ' "' . $module . '" is not a module name' )
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ /\A[A-Za-z0-9_-]+\z/u 0 58 58
79 if $module !~ /\A[A-Za-z0-9_-]+\z/;
80 }
81 }
82 23 ▶ push( @problems, 'nothing to check, give --present, --absent, or --not-loadable' ) if !$any;
Branch: 1 branch
BranchTrueFalseTotal
unless $any 5 18 23
83  
84 23 return @problems;
85 } ## end sub validate
86  
87 =head1 METHODS
88  
89 =head2 check
90  
91 Does the check.
92  
93 =cut
94  
95 18 ▶sub check {
Subroutine check: called CC=23 SCAR=31.5
Pod documented
96 18 my ($self) = @_;
97  
98 18 my ( $loaded, $builtin, $blocked );
99 18 ▶ if ( $self->os eq 'linux' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->os eq 'linux') { } 10 8 18
elsif ($self->os eq 'freebsd') { } 8 0 8
100 10 $loaded = { map { _normal($_) => 1 } $self->proc_file('modules') =~ /^(\S+)/mg };
101 10 $builtin = $self->builtin;
102 10 $blocked = $self->blocked;
103 } elsif ( $self->os eq 'freebsd' ) {
104 8 ▶ die("--not-loadable is not supported on freebsd\n") if defined( $self->opt('not-loadable') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("not-loadable") 3 5 8
105 5 $loaded = {};
106 5 ▶ foreach my $module ( @{ $self->opt('present') // [] }, @{ $self->opt('absent') // [] } ) {
Condition50%2/4
$self->opt("present") // []
l!l
50
$self->opt("absent") // []
l!l
50
MC/DC0%0/1
$self->opt("present") // []
$self->opt("present")
MC/DC0%0/1
$self->opt("absent") // []
$self->opt("absent")
Truth table50%1/2
A
A= $self->opt("present")
Aresult
11
00
Truth table50%1/2
A
A= $self->opt("absent")
Aresult
11
00
107 9 my ($exit) = $self->run_command( $KLDSTAT, '-q', '-m', $module );
108 7 ▶ $loaded->{ _normal($module) } = 1 if !$exit;
Branch: 1 branch
BranchTrueFalseTotal
unless $exit 4 3 7
109 }
110 3 ( $builtin, $blocked ) = ( {}, {} );
111 } else {
112 0 die( 'not supported on ' . $self->os . "\n" );
113 }
114  
115 13 my @problems;
116 my @fine;
117 13 ▶ foreach my $module ( @{ $self->opt('present') // [] } ) {
Condition50%1/2
$self->opt("present") // []
l!l
130
MC/DC0%0/1
$self->opt("present") // []
$self->opt("present")
Truth table50%1/2
A
A= $self->opt("present")
Aresult
11
00
118 18 my $normal = _normal($module);
119 18 ▶ if ( $loaded->{$normal} || $builtin->{$normal} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loaded->{$normal} or $builtin->{$normal}) { } 13 5 18
Condition100%3/3
$loaded->{$normal} or $builtin->{$normal}
l!l&&r!l&&!r
855
MC/DC100%2/2
$loaded->{$normal} or $builtin->{$normal}
$loaded->{$normal} $builtin->{$normal}
Truth table100%3/3
A or B
A= $loaded->{$normal}B= $builtin->{$normal}
ABresult
1X1
011
000
120 13 push( @fine, $module . ' loaded' );
121 } else {
122 5 push( @problems, $module . ' is not loaded' );
123 }
124 }
125 13 foreach my $kind ( 'absent', 'not-loadable' ) {
126 26 ▶ foreach my $module ( @{ $self->opt($kind) // [] } ) {
Condition100%2/2
$self->opt($kind) // []
l!l
233
MC/DC100%1/1
$self->opt($kind) // []
$self->opt($kind)
Truth table100%2/2
A
A= $self->opt($kind)
Aresult
11
00
127 33 my $normal = _normal($module);
128 33 my @wrong;
129 33 ▶ push( @wrong, 'is built in' ) if $builtin->{$normal};
Branch: 1 branch
BranchTrueFalseTotal
if $builtin->{$normal} 5 28 33
130 33 ▶ push( @wrong, 'is loaded' ) if $loaded->{$normal};
Branch: 1 branch
BranchTrueFalseTotal
if $loaded->{$normal} 10 23 33
131 push( @wrong, 'is not kept from loading by a install line in modprobe.d' )
132 33 ▶ if $kind eq 'not-loadable' && !$blocked->{$normal} && !$builtin->{$normal};
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "not-loadable" and not $blocked->{$normal} and not $builtin->{$normal} 5 28 33
Condition83%5/6
$kind eq "not-loadable" and not $blocked->{$normal} and not $builtin->{$normal}
!ll&&!rl&&r
2805
$kind eq "not-loadable" and not $blocked->{$normal}
!ll&&!rl&&r
18105
MC/DC66%2/3
$kind eq "not-loadable" and not $blocked->{$normal} and not $builtin->{$normal}
$kind eq "not-loadable" not $blocked->{$normal} not $builtin->{$normal}
Truth table75%3/4
A and not(B) and not(C)
A= $kind eq "not-loadable"B= not $blocked->{$normal}C= not $builtin->{$normal}
ABCresult
0XX0
10X0
1100
1111
133 33 ▶ if ( defined( $wrong[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wrong[0]) { } 15 18 33
134 15 push( @problems, $module . ' ' . join( ' and ', @wrong ) );
135 } else {
136 18 ▶ push( @fine, $module . ( $kind eq 'absent' ? ' not loaded' : ' not loadable' ) );
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'absent' ? : 8 10 18
137 }
138 } ## end foreach my $module ( @{ $self->opt($kind) // []...})
139 } ## end foreach my $kind ( 'absent', 'not-loadable' )
140  
141 13 ▶ return $self->finish( $self->CRITICAL, join( ', ', @problems ) ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 5 8 13
142 8 return $self->finish( $self->OK, join( ', ', @fine ) );
143 } ## end sub check
144  
145 =head2 builtin
146  
147 Returns a hash ref of the modules built into the running Linux kernel,
148 from modules.builtin, empty if it is not there.
149  
150 =cut
151  
152 10 ▶sub builtin {
Subroutine builtin: called CC=3 SCAR=11.8
Pod documented
153 10 my ($self) = @_;
154  
155 10 ▶ my $release = $RELEASE // ( POSIX::uname() )[2];
Condition33%1/3
$Monitoring::Sneck::Checks::kernel_modules::RELEASE // (POSIX::uname())[2]
l!l&&r!l&&!r
1000
MC/DC0%0/2
$Monitoring::Sneck::Checks::kernel_modules::RELEASE // (POSIX::uname())[2]
$Monitoring::Sneck::Checks::kernel_modules::RELEASE (POSIX::uname())[2]
Truth table33%1/3
A // B
A= $Monitoring::Sneck::Checks::kernel_modules::RELEASEB= (POSIX::uname())[2]
ABresult
1X1
011
000
156 10 ▶ my $content = read_file( $MODULES . '/' . $release . '/modules.builtin', err_mode => 'quiet' ) // '';
Condition50%1/2
read_file($Monitoring::Sneck::Checks::kernel_modules::MODULES . '/' . $release . '/modules.builtin', "err_mode", "quiet") // ""
l!l
100
MC/DC0%0/1
read_file($Monitoring::Sneck::Checks::kernel_modules::MODULES . '/' . $release . '/modules.builtin', "err_mode", "quiet") // ""
read_file($Monitoring::Sneck::Checks::kernel_modules::MODULES . '/' . $release . '/modules.builtin', "err_mode", "quiet")
Truth table50%1/2
A
A= read_file($Monitoring::Sneck::Checks::kernel_modules::MODULES . '/' . $release . '/modules.builtin', "err_mode", "quiet")
Aresult
11
00
157  
158 10 return { map { _normal($_) => 1 } $content =~ m{(?:\A|/)([^/\s]+)\.ko\s*$}mg };
159 }
160  
161 =head2 blocked
162  
163 Returns a hash ref of the modules modprobe.d has a "install name
164 /bin/false" or "/bin/true" line for.
165  
166 =cut
167  
168 10 ▶sub blocked {
Subroutine blocked: called CC=5 SCAR=16.1
Pod documented
169 10 my ($self) = @_;
170  
171 10 my %blocked;
172 10 foreach my $dir (@MODPROBE_DIRS) {
173 20 foreach my $file ( glob( $dir . '/*.conf' ) ) {
174 10 ▶ my $content = read_file( $file, err_mode => 'quiet' ) // next;
Condition50%1/2
read_file($file, "err_mode", "quiet") // (next)
l!l
100
MC/DC0%0/1
read_file($file, "err_mode", "quiet") // (next)
read_file($file, "err_mode", "quiet")
Truth table50%1/2
A
A= read_file($file, "err_mode", "quiet")
Aresult
11
00
175 10 ▶ $blocked{ _normal($1) } = 1 while $content =~ m{^\s*install\s+(\S+)\s+(?:/usr)?/bin/(?:false|true)\b}mg;
Branch: 1 branch
BranchTrueFalseTotal
$content =~ m[^\s*install\s+(\S+)\s+(?:/usr)?/bin/(?:false|true)\b]gmu and do { $blocked{_normal($1)} = 1 } 20 10 30
176 }
177 }
178  
179 10 return \%blocked;
180 } ## end sub blocked
181  
182 # Normalizes a module name, as the kernel takes - and _ as the same.
183 #
184 # Args...
185 #
186 # - name :: The module name.
187 #
188 # Returns it with - turned into _.
189 #
190 # Example...
191 #
192 # _normal('usb-storage'); # 'usb_storage'
193 125 ▶sub _normal {
Subroutine _normal: called CC=1 SCAR=0.0
194 125 return $_[0] =~ tr/-/_/r;
195 }
196  
197 7 1;
« blib/lib/Monitoring/Sneck/Checks/sensors.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/interface.pm »