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

filter: Statementstmt 99.1% 115 / 116 Branchbran 76.7% 43 / 56 Conditioncond 52.7% 19 / 36 MC/DCmcdc 22.7% 5 / 22 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 81.1% 206 / 254 CC 46 SCAR 21.6
CC
46
Cov
85%
linux_reasons
16.3
replaced_libraries
10.5
_version_cmp
8.3
CRAP 8.7
« blib/lib/Monitoring/Sneck/Checks/mount.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/rbl.pm »
1 package Monitoring::Sneck::Checks::reboot_needed;
2  
3 5 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Monitoring::Sneck::Checks::reboot_needed - checks if a reboot or restarts are needed after updates
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 5 our $VERSION = '0.0.1';
21  
22 # Where things are looked for. Set by tests.
23 5 our $BOOT = '/boot';
24 5 our $MODULES = '/lib/modules';
25 5 our $DPKG_STATUS = '/var/lib/dpkg/status';
26 5 our $REBOOT_REQUIRED = '/var/run/reboot-required';
27 5 our $FREEBSD_VERSION = '/bin/freebsd-version';
28  
29 # The running kernel's release, as uname -r says, if set. Set by tests.
30 5 our $RELEASE;
31  
32 =head1 SYNOPSIS
33  
34 reboot|perl:reboot_needed
35 restarts|perl:reboot_needed -l
36  
37 =head1 DESCRIPTION
38  
39 WARNING, or CRITICAL with --critical, if updates are waiting on a
40 reboot.
41  
42 On Linux, that is any of...
43  
44 - /var/run/reboot-required :: Is there, as Debian and Ubuntu packages
45 leave it. What it says and the packages in reboot-required.pkgs are
46 added.
47  
48 - a newer kernel :: Is installed than the one running, going by the
49 /boot/vmlinuz-* and /lib/modules/*/vmlinuz there are.
50  
51 - the kernel package :: For Debian, the version of the installed
52 linux-image package for the running kernel is not the one running, as
53 for a update that kept the same ABI, and so the same release.
54  
55 On FreeBSD, it is freebsd-version -k, the installed kernel, not being
56 freebsd-version -r, the one running. Other OSes are UNKNOWN.
57  
58 With --libraries, on Linux, processes using libraries that were since
59 replaced are listed as well, as they need restarting to use the new
60 ones, the same as needrestart finds. This reads /proc/*/maps, so needs
61 root to see every process. On FreeBSD it does nothing.
62  
63 =head1 OPTIONS
64  
65 - -l, --libraries :: Also list processes using replaced libraries. Linux
66 only.
67  
68 - -n, --lines :: How many processes using replaced libraries to list.
69 Default :: 20
70  
71 - -c, --critical :: Make it CRITICAL in place of WARNING.
72  
73 =cut
74  
75 30 ▶sub option_spec { return ( 'libraries|l', 'lines|n=i', 'critical|c' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
76  
77 30 ▶sub defaults { return { lines => 20 } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
78  
79 =head1 METHODS
80  
81 =head2 check
82  
83 Does the check.
84  
85 =cut
86  
87 30 ▶sub check {
Subroutine check: called CC=6 SCAR=17.9
Pod documented
88 30 my ($self) = @_;
89  
90 30 my @reasons;
91 my @details;
92 30 ▶ if ( $self->os eq 'linux' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->os eq 'linux') { } 24 6 30
elsif ($self->os eq 'freebsd') { } 5 1 6
93 24 push( @reasons, $self->linux_reasons( \@details ) );
94 24 ▶ push( @reasons, $self->replaced_libraries( \@details ) ) if $self->opt('libraries');
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt("libraries") 6 18 24
95 } elsif ( $self->os eq 'freebsd' ) {
96 5 push( @reasons, $self->freebsd_reasons );
97 } else {
98 1 die( 'not supported on ' . $self->os . "\n" );
99 }
100  
101 27 ▶ if ( !defined( $reasons[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $reasons[0]) 8 19 27
102 8 return $self->finish( $self->OK, 'running ' . $self->release . ', nothing is waiting on a reboot' );
103 }
104  
105 19 ▶ return $self->finish( $self->opt('critical') ? $self->CRITICAL : $self->WARNING, join( ', ', @reasons ), @details );
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('critical') ? : 3 16 19
106 } ## end sub check
107  
108 =head2 release
109  
110 Returns the release of the running kernel, as uname -r says.
111  
112 =cut
113  
114 32 ▶sub release {
Subroutine release: called CC=2 SCAR=9.2
Pod documented
115 32 ▶ return $RELEASE // ( POSIX::uname() )[2];
Condition33%1/3
$Monitoring::Sneck::Checks::reboot_needed::RELEASE // (POSIX::uname())[2]
l!l&&r!l&&!r
3200
MC/DC0%0/2
$Monitoring::Sneck::Checks::reboot_needed::RELEASE // (POSIX::uname())[2]
$Monitoring::Sneck::Checks::reboot_needed::RELEASE (POSIX::uname())[2]
Truth table33%1/3
A // B
A= $Monitoring::Sneck::Checks::reboot_needed::RELEASEB= (POSIX::uname())[2]
ABresult
1X1
011
000
116 }
117  
118 =head2 linux_reasons
119  
120 Returns the reasons Linux needs a reboot, as said under
121 L</DESCRIPTION>, adding a line listing the packages in
122 reboot-required.pkgs to the array ref given.
123  
124 =cut
125  
126 24 ▶sub linux_reasons {
Subroutine linux_reasons: called CC=15 SCAR=27.9
Pod documented
127 24 my ( $self, $details ) = @_;
128  
129 24 my @reasons;
130 24 my $release = $self->release;
131 24 ▶ if ( -e $REBOOT_REQUIRED ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $Monitoring::Sneck::Checks::reboot_needed::REBOOT_REQUIRED) 3 21 24
132 3 ▶ my $says = read_file( $REBOOT_REQUIRED, err_mode => 'quiet' ) // '';
Condition50%1/2
read_file($Monitoring::Sneck::Checks::reboot_needed::REBOOT_REQUIRED, "err_mode", "quiet") // ""
l!l
30
MC/DC0%0/1
read_file($Monitoring::Sneck::Checks::reboot_needed::REBOOT_REQUIRED, "err_mode", "quiet") // ""
read_file($Monitoring::Sneck::Checks::reboot_needed::REBOOT_REQUIRED, "err_mode", "quiet")
Truth table50%1/2
A
A= read_file($Monitoring::Sneck::Checks::reboot_needed::REBOOT_REQUIRED, "err_mode", "quiet")
Aresult
11
00
133 3 $says =~ s/\A\s*\**\s*|\s*\**\s*\z//g;
134 3 ▶ push( @reasons, $REBOOT_REQUIRED . ' is there' . ( $says ne '' ? ', "' . $says . '"' : '' ) );
Branch: 1 branch
BranchTrueFalseTotal
$says ne '' ? : 3 0 3
135 3 my $packages = read_file( $REBOOT_REQUIRED . '.pkgs', err_mode => 'quiet' );
136 3 ▶ push( @{$details}, 'for ' . join( ', ', sort( _unique( split( /\s+/, $packages ) ) ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if defined $packages and $packages =~ /\S/u 3 0 3
Condition33%1/3
defined $packages and $packages =~ /\S/u
!ll&&!rl&&r
003
MC/DC0%0/2
defined $packages and $packages =~ /\S/u
defined $packages $packages =~ /\S/u
Truth table33%1/3
A and B
A= defined $packagesB= $packages =~ /\S/u
ABresult
0X0
100
111
137 if defined($packages) && $packages =~ /\S/;
138 }
139  
140 24 my %installed;
141 24 foreach my $vmlinuz ( glob( $BOOT . '/vmlinuz-*' ), glob( $MODULES . '/*/vmlinuz' ) ) {
142 54 my ($version) = $vmlinuz =~ m{/vmlinuz-([^/]+)\z};
143 54 ▶ ($version) = $vmlinuz =~ m{/([^/]+)/vmlinuz\z} if !defined($version);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 3 51 54
144 54 ▶ $installed{$version} = 1 if defined($version);
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 54 0 54
145 }
146 24 my ($newest) = sort { _version_cmp( $b, $a ) } keys(%installed);
147 24 ▶ if ( defined($newest) && _version_cmp( $newest, $release ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newest and _version_cmp($newest, $release) > 0) 6 18 24
Condition66%2/3
defined $newest and _version_cmp($newest, $release) > 0
!ll&&!rl&&r
0186
MC/DC50%1/2
defined $newest and _version_cmp($newest, $release) > 0
defined $newest _version_cmp($newest, $release) > 0
Truth table66%2/3
A and B
A= defined $newestB= _version_cmp($newest, $release) > 0
ABresult
0X0
100
111
148 6 push( @reasons, 'kernel ' . $newest . ' is installed, but ' . $release . ' is running' );
149 }
150  
151 # Debian puts the package version last in /proc/version, such as "Debian 6.12.48-1 (2025-09-20)",
152 # after the compiler's, which may say Debian too
153 24 ▶ my $proc_version = eval { $self->proc_file('version') } // '';
Condition50%1/2
eval { do { $self->proc_file("version") } } // ""
l!l
240
MC/DC0%0/1
eval { do { $self->proc_file("version") } } // ""
eval { do { $self->proc_file("version") } }
Truth table50%1/2
A
A= eval { do { $self->proc_file("version") } }
Aresult
11
00
154 24 my $running = ( $proc_version =~ /\bDebian ([0-9][^\s()]*)/g )[-1];
155 24 my $package = 'linux-image-' . $release;
156 24 ▶ my $packaged = defined($running) ? $self->dpkg_version($package) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $running ? : 24 0 24
157 24 ▶ if ( defined($packaged) && $packaged ne $running ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packaged and $packaged ne $running) 3 21 24
Condition66%2/3
defined $packaged and $packaged ne $running
!ll&&!rl&&r
0213
MC/DC50%1/2
defined $packaged and $packaged ne $running
defined $packaged $packaged ne $running
Truth table66%2/3
A and B
A= defined $packagedB= $packaged ne $running
ABresult
0X0
100
111
158 3 push( @reasons, $package . ' ' . $packaged . ' is installed, but ' . $running . ' is running' );
159 }
160  
161 24 return @reasons;
162 } ## end sub linux_reasons
163  
164 =head2 dpkg_version
165  
166 Returns the version of a installed package from dpkg's status file,
167 or undef if it is not installed.
168  
169 my $version = $check->dpkg_version('linux-image-6.12.48+deb13-amd64');
170  
171 =cut
172  
173 24 ▶sub dpkg_version {
Subroutine dpkg_version: called CC=6 SCAR=19.9
Pod documented
174 24 my ( $self, $package ) = @_;
175  
176 24 ▶ my $status = read_file( $DPKG_STATUS, err_mode => 'quiet' ) // '';
Condition50%1/2
read_file($Monitoring::Sneck::Checks::reboot_needed::DPKG_STATUS, "err_mode", "quiet") // ""
l!l
240
MC/DC0%0/1
read_file($Monitoring::Sneck::Checks::reboot_needed::DPKG_STATUS, "err_mode", "quiet") // ""
read_file($Monitoring::Sneck::Checks::reboot_needed::DPKG_STATUS, "err_mode", "quiet")
Truth table50%1/2
A
A= read_file($Monitoring::Sneck::Checks::reboot_needed::DPKG_STATUS, "err_mode", "quiet")
Aresult
11
00
177 24 foreach my $stanza ( split( /\n\n+/, $status ) ) {
178 24 ▶ next if $stanza !~ /^Package: \Q$package\E$/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $stanza =~ /^Package: \Q$package\E$/mu 0 24 24
179 24 ▶ next if $stanza !~ /^Status: \S+ \S+ installed$/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $stanza =~ /^Status: \S+ \S+ installed$/mu 0 24 24
180 24 ▶ return $1 if $stanza =~ /^Version: (\S+)$/m;
Branch: 1 branch
BranchTrueFalseTotal
if $stanza =~ /^Version: (\S+)$/mu 24 0 24
181 }
182  
183 0 return undef;
184 } ## end sub dpkg_version
185  
186 =head2 replaced_libraries
187  
188 Returns a reason if processes use libraries that were replaced, adding
189 a line for each process to the array ref given, with the libraries it
190 uses, up to --lines of them.
191  
192 =cut
193  
194 6 ▶sub replaced_libraries {
Subroutine replaced_libraries: called CC=10 SCAR=23.5
Pod documented
195 6 my ( $self, $details ) = @_;
196  
197 6 my %uses;
198 6 ▶ opendir( my $proc, $Monitoring::Sneck::Checks::base::PROC ) or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $proc, $Monitoring::Sneck::Checks::base::PROC or return () 6 0 6
199 6 foreach my $pid ( grep { /\A[0-9]+\z/ } readdir($proc) ) {
200 18 ▶ next if $pid == $$;
Branch: 1 branch
BranchTrueFalseTotal
if $pid == $$ 0 18 18
201 18 ▶ my $maps = eval { $self->proc_file( $pid . '/maps' ) } // next;
Condition50%1/2
eval { do { $self->proc_file($pid . '/maps') } } // (next)
l!l
180
MC/DC0%0/1
eval { do { $self->proc_file($pid . '/maps') } } // (next)
eval { do { $self->proc_file($pid . '/maps') } }
Truth table50%1/2
A
A= eval { do { $self->proc_file($pid . '/maps') } }
Aresult
11
00
202 18 my %libraries = map { ( split( m{/}, $_ ) )[-1] => 1 } $maps =~ m{\s(/\S+\.so(?:\.[0-9.]+)?) \(deleted\)$}mg;
203 18 ▶ next if !%libraries;
Branch: 1 branch
BranchTrueFalseTotal
unless %libraries 6 12 18
204 12 ▶ my $name = eval { $self->proc_file( $pid . '/comm' ) } // $pid;
Condition33%1/3
eval { do { $self->proc_file($pid . '/comm') } } // $pid
l!l&&r!l&&!r
1200
MC/DC0%0/2
eval { do { $self->proc_file($pid . '/comm') } } // $pid
eval { do { $self->proc_file($pid . '/comm') } } $pid
Truth table33%1/3
A // B
A= eval { do { $self->proc_file($pid . '/comm') } }B= $pid
ABresult
1X1
011
000
205 12 $name =~ s/\s+\z//;
206 12 $uses{ $name . '[' . $pid . ']' } = [ sort( keys(%libraries) ) ];
207 }
208 6 my @processes = sort( keys(%uses) );
209 6 ▶ return () if !defined( $processes[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $processes[0] 0 6 6
210  
211 6 my @lines = map { $_ . ' uses ' . join( ', ', @{ $uses{$_} } ) } @processes;
212 6 my @shown = splice( @lines, 0, $self->opt('lines') );
213 6 ▶ push( @shown, 'and ' . scalar(@lines) . ' more' ) if defined( $lines[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $lines[0] 3 3 6
214 6 push( @{$details}, @shown );
215  
216 6 ▶ return ( scalar(@processes) . ' process' . ( scalar(@processes) == 1 ? '' : 'es' ) . ' using replaced libraries' );
Branch: 1 branch
BranchTrueFalseTotal
scalar @processes == 1 ? : 0 6 6
217 } ## end sub replaced_libraries
218  
219 =head2 freebsd_reasons
220  
221 Returns the reasons FreeBSD needs a reboot, as said under
222 L</DESCRIPTION>.
223  
224 =cut
225  
226 5 ▶sub freebsd_reasons {
Subroutine freebsd_reasons: called CC=4 SCAR=14.0
Pod documented
227 5 my ($self) = @_;
228  
229 5 my ( $exit, $output ) = $self->run_command( $FREEBSD_VERSION, '-k', '-r' );
230 3 ▶ die( $FREEBSD_VERSION . ' exited ' . $exit . "\n" ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 3 3
231 3 my ( $installed, $running ) = split( /\n/, $output );
232 3 ▶ die( $FREEBSD_VERSION . ' did not say both versions' . "\n" ) if !defined($running);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $running 0 3 3
233 3 ▶ return () if $installed eq $running;
Branch: 1 branch
BranchTrueFalseTotal
if $installed eq $running 2 1 3
234  
235 1 return ( 'kernel ' . $installed . ' is installed, but ' . $running . ' is running' );
236 } ## end sub freebsd_reasons
237  
238 # Compares two versions, such as kernel releases, number by number and
239 # the rest as text, so 6.12.48 comes after 6.1.0 and 6.12.9.
240 #
241 # Args...
242 #
243 # - a, b :: The versions, such as '6.12.48+deb13-amd64'.
244 #
245 # Returns -1, 0, or 1, as cmp.
246 #
247 # Example...
248 #
249 # _version_cmp( '6.12.48+deb13-amd64', '6.1.0-28-amd64' ); # 1
250 60 ▶sub _version_cmp {
Subroutine _version_cmp: called CC=8 SCAR=21.1
251 60 my ( $first, $second ) = @_;
252  
253 60 my @first = $first =~ /([0-9]+|[^0-9]+)/g;
254 60 my @second = $second =~ /([0-9]+|[^0-9]+)/g;
255 60 ▶ while ( @first || @second ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@first or @second) 318 19 337
Condition66%2/3
@first or @second
l!l&&r!l&&!r
318019
MC/DC50%1/2
@first or @second
@first @second
Truth table66%2/3
A or B
A= @firstB= @second
ABresult
1X1
011
000
256 318 ▶ my $one = shift(@first) // '';
Condition50%1/2
shift @first // ""
l!l
3180
MC/DC0%0/1
shift @first // ""
shift @first
Truth table50%1/2
A
A= shift @first
Aresult
11
00
257 318 ▶ my $other = shift(@second) // '';
Condition50%1/2
shift @second // ""
l!l
3180
MC/DC0%0/1
shift @second // ""
shift @second
Truth table50%1/2
A
A= shift @second
Aresult
11
00
258 318 ▶ my $order
Branch: 1 branch
BranchTrueFalseTotal
$one =~ /\A[0-9]+\z/u && $other =~ /\A[0-9]+\z/u ? : 189 129 318
Condition66%2/3
$one =~ /\A[0-9]+\z/u and $other =~ /\A[0-9]+\z/u
!ll&&!rl&&r
1290189
MC/DC50%1/2
$one =~ /\A[0-9]+\z/u and $other =~ /\A[0-9]+\z/u
$one =~ /\A[0-9]+\z/u $other =~ /\A[0-9]+\z/u
Truth table66%2/3
A and B
A= $one =~ /\A[0-9]+\z/uB= $other =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
259 = ( $one =~ /\A[0-9]+\z/ && $other =~ /\A[0-9]+\z/ )
260 ? $one <=> $other
261 : $one cmp $other;
262 318 ▶ return $order if $order;
Branch: 1 branch
BranchTrueFalseTotal
if $order 41 277 318
263 }
264  
265 19 return 0;
266 } ## end sub _version_cmp
267  
268 # Returns the list given, each only once, in order, leaving out empty strings.
269 #
270 # Args...
271 #
272 # - list :: The items.
273 #
274 # Example...
275 #
276 # _unique( 'a', 'b', 'a' ); # ( 'a', 'b' )
277 3 ▶sub _unique {
Subroutine _unique: called CC=2 SCAR=7.0
278 3 my %seen;
279  
280 3 ▶ return grep { $_ ne '' && !$seen{$_}++ } @_;
Condition66%2/3
$_ ne '' && !$seen{$_}++
!ll&&!rl&&r
036
MC/DC50%1/2
$_ ne '' && !$seen{$_}++
$_ ne '' !$seen{$_}++
Truth table66%2/3
A && not(B)
A= $_ ne ''B= !$seen{$_}++
ABresult
0X0
100
111
281 }
282  
283 5 1;
« blib/lib/Monitoring/Sneck/Checks/mount.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/rbl.pm »