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

filter: Statementstmt 100% 70 / 70 Branchbran 95.8% 23 / 24 Conditioncond 91.6% 11 / 12 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 97.6% 125 / 128 CC 21 SCAR 23.5
CC
21
Cov
98%
check
17.0
validate
3.0
suite
3.0
CRAP 10.5
« blib/lib/Monitoring/Sneck/Checks/oom.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/dns.pm »
1 package Monitoring::Sneck::Checks::debsecan;
2  
3 19 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::debsecan - checks for Debian security fixes through debsecan
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 19 our $VERSION = '0.0.1';
20  
21 # Where debsecan is. Set by tests.
22 19 our $DEBSECAN = '/usr/bin/debsecan';
23  
24 # Where the suite is found if not given. Set by tests.
25 19 our $OS_RELEASE = '/etc/os-release';
26  
27 # What it counts, the option letters for them, and the defaults, as check_debsecan.
28 19 our @KINDS = (
29 { name => 'fixed', flag => 'fixed', letter => 'f', warning => 20, critical => 30 },
30 { name => 'obsolete', flag => 'obsolete', letter => 'o', warning => 1, critical => 1 },
31 { name => 'high', flag => 'high urgency', letter => 'u', warning => 1, critical => 1 },
32 { name => 'medium', flag => 'medium urgency', letter => 'm', warning => 5, critical => 10 },
33 { name => 'low', flag => 'low urgency', letter => 'l', warning => 10, critical => 20 },
34 );
35  
36 =head1 SYNOPSIS
37  
38 !debsecan|interval=3600 timeout=300|perl:debsecan
39 !debsecan_urgent|interval=3600 timeout=300|perl:debsecan -u 1 -U 1 -f 100 -F 200
40  
41 =head1 DESCRIPTION
42  
43 Runs debsecan and counts what it reports, the same as check_debsecan.
44 It is WARNING or CRITICAL once a count is at or over its limit.
45  
46 - fixed :: Vulnerabilities with a fix ready to install.
47  
48 - obsolete :: Installed packages Debian no longer has.
49  
50 - high, medium, low :: Vulnerabilities by urgency.
51  
52 By default only those with a fix are counted, as only those can be
53 acted on.
54  
55 debsecan downloads the security tracker's data each time it runs,
56 which takes a while and should not be done every few minutes. Run it
57 as a compliance check with a interval of a hour or more and a longer
58 timeout, as in the synopsis. Set http_proxy through a env line if it
59 needs a proxy.
60  
61 debsecan failing, such as not being able to reach the tracker, is
62 UNKNOWN. So is it not being there.
63  
64 =head1 OPTIONS
65  
66 - -s, --suite :: The Debian suite, such as trixie.
67 Default :: VERSION_CODENAME from /etc/os-release, else what debsecan picks
68  
69 - -a, --all :: Count vulnerabilities with no fix yet too.
70  
71 - -f, --fixed-warning and -F, --fixed-critical :: The WARNING and
72 CRITICAL limits for fixed.
73 Default :: 20, 30
74  
75 - -o, --obsolete-warning and -O, --obsolete-critical :: The same for
76 obsolete.
77 Default :: 1, 1
78  
79 - -u, --high-warning and -U, --high-critical :: The same for high
80 urgency.
81 Default :: 1, 1
82  
83 - -m, --medium-warning and -M, --medium-critical :: The same for
84 medium urgency.
85 Default :: 5, 10
86  
87 - -l, --low-warning and -L, --low-critical :: The same for low
88 urgency.
89 Default :: 10, 20
90  
91 - -n, --lines :: How many of what it found to list after the status
92 line, once it is not OK.
93 Default :: 20
94  
95 =cut
96  
97 108 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
98 return (
99 'suite|s=s',
100 'all|a',
101 'lines|n=i',
102 map {
103 108 (
104 $_->{name} . '-warning|' . $_->{letter} . '=i',
105 540 $_->{name} . '-critical|' . uc( $_->{letter} ) . '=i'
106 )
107 } @KINDS
108 );
109 } ## end sub option_spec
110  
111 108 ▶sub defaults {
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
112 return {
113 lines => 20,
114 108 map { ( $_->{name} . '-warning' => $_->{warning}, $_->{name} . '-critical' => $_->{critical} ) } @KINDS
115 };
116 }
117  
118 108 ▶sub validate {
Subroutine validate: called CC=3 SCAR=11.0
Pod documented
119 108 my ( $self, $options ) = @_;
120  
121 return ( '--suite "' . $options->{suite} . '" is not a suite' )
122 108 ▶ if defined( $options->{suite} ) && $options->{suite} !~ /\A[a-z][a-z0-9-]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"suite"} and not $options->{"suite"} =~ /\A[a-z][a-z0-9-]*\z/u 10 98 108
Condition100%3/3
defined $options->{"suite"} and not $options->{"suite"} =~ /\A[a-z][a-z0-9-]*\z/u
!ll&&!rl&&r
841410
MC/DC100%2/2
defined $options->{"suite"} and not $options->{"suite"} =~ /\A[a-z][a-z0-9-]*\z/u
defined $options->{"suite"} not $options->{"suite"} =~ /\A[a-z][a-z0-9-]*\z/u
Truth table100%3/3
A and not(B)
A= defined $options->{"suite"}B= not $options->{"suite"} =~ /\A[a-z][a-z0-9-]*\z/u
ABresult
0X0
100
111
123  
124 98 return ();
125 }
126  
127 =head1 METHODS
128  
129 =head2 check
130  
131 Does the check.
132  
133 =cut
134  
135 98 ▶sub check {
Subroutine check: called CC=17 SCAR=28.3
Pod documented
136 98 my ($self) = @_;
137  
138 98 my @command = ($DEBSECAN);
139 98 ▶ my $suite = $self->opt('suite') // $self->suite;
Condition100%3/3
$self->opt("suite") // $self->suite
l!l&&r!l&&!r
147113
MC/DC100%2/2
$self->opt("suite") // $self->suite
$self->opt("suite") $self->suite
Truth table100%3/3
A // B
A= $self->opt("suite")B= $self->suite
ABresult
1X1
011
000
140 98 ▶ push( @command, '--suite', $suite ) if defined($suite);
Branch: 1 branch
BranchTrueFalseTotal
if defined $suite 85 13 98
141 98 ▶ push( @command, '--only-fixed' ) if !$self->opt('all');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->opt("all") 84 14 98
142 98 my ( $exit, $output ) = $self->run_command(@command);
143 81 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 11 70 81
144 11 ▶ my $first = ( split( /\n/, $output ) )[0] // '';
Condition50%1/2
(split(/\n/u, $output, 0))[0] // ""
l!l
110
MC/DC0%0/1
(split(/\n/u, $output, 0))[0] // ""
(split(/\n/u, $output, 0))[0]
Truth table50%1/2
A
A= (split(/\n/u, $output, 0))[0]
Aresult
11
00
145 11 return $self->finish( $self->UNKNOWN, 'debsecan exited ' . $exit . ', "' . $first . '"' );
146 }
147  
148 # lines are "CVE-2025-1234 package (fixed, high urgency)", the flags in () being optional
149 70 my %counts = map { $_->{name} => 0 } @KINDS;
150 70 my @found;
151 my %packages;
152 70 foreach my $line ( split( /\n/, $output ) ) {
153 420 ▶ next if $line !~ /\A(\S+)\s+(\S+)(?:\s+\((.*)\))?\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A(\S+)\s+(\S+)(?:\s+\((.*)\))?\s*\z/u 0 420 420
154 420 ▶ my ( $vulnerability, $package, $flags ) = ( $1, $2, $3 // '' );
Condition100%2/2
$3 // ""
l!l
35070
MC/DC100%1/1
$3 // ""
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
155 420 my %has = map { $_ => 1 } split( /\s*,\s*/, $flags );
156 420 $counts{ $_->{name} }++ foreach grep { $has{ $_->{flag} } } @KINDS;
157 420 $packages{$package} = 1;
158 420 push( @found, $line );
159 }
160  
161 70 my $code = $self->OK;
162 70 my @over;
163 70 foreach my $kind (@KINDS) {
164 350 my $count = $counts{ $kind->{name} };
165 350 my $warning = $self->opt( $kind->{name} . '-warning' );
166 350 my $critical = $self->opt( $kind->{name} . '-critical' );
167 $self->perfdata(
168 label => $kind->{name},
169 350 value => $count,
170 warning => $warning,
171 critical => $critical,
172 min => 0
173 );
174 350 ▶ if ( $count >= $critical ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count >= $critical) { } 82 268 350
elsif ($count >= $warning) { } 14 254 268
175 82 $code = $self->CRITICAL;
176 82 push( @over, $kind->{name} );
177 } elsif ( $count >= $warning ) {
178 14 $code = $self->worst( $code, $self->WARNING );
179 14 push( @over, $kind->{name} );
180 }
181 } ## end foreach my $kind (@KINDS)
182  
183 my $message
184 70 ▶ = join( ', ', map { $_->{name} . ' ' . $counts{ $_->{name} } } @KINDS ) . ', in '
Branch: 1 branch
BranchTrueFalseTotal
defined $suite ? : 58 12 70
185 . scalar( keys(%packages) )
186 . ' packages'
187 . ( defined($suite) ? ', ' . $suite : '' );
188 70 ▶ $message = $message . ', too many ' . join( ', ', @over ) if defined( $over[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $over[0] 55 15 70
189 70 my @details;
190 70 ▶ if ( $code != $self->OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != $self->OK) 55 15 70
191 55 @details = splice( @found, 0, $self->opt('lines') );
192 55 ▶ push( @details, 'and ' . scalar(@found) . ' more' ) if defined( $found[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $found[0] 14 41 55
193 }
194  
195 70 return $self->finish( $code, $message, @details );
196 } ## end sub check
197  
198 =head2 suite
199  
200 Returns VERSION_CODENAME from /etc/os-release, or undef if it has none.
201  
202 =cut
203  
204 84 ▶sub suite {
Subroutine suite: called CC=3 SCAR=11.0
Pod documented
205 84 ▶ my $os_release = read_file( $OS_RELEASE, err_mode => 'quiet' ) // '';
Condition100%2/2
read_file($Monitoring::Sneck::Checks::debsecan::OS_RELEASE, "err_mode", "quiet") // ""
l!l
7113
MC/DC100%1/1
read_file($Monitoring::Sneck::Checks::debsecan::OS_RELEASE, "err_mode", "quiet") // ""
read_file($Monitoring::Sneck::Checks::debsecan::OS_RELEASE, "err_mode", "quiet")
Truth table100%2/2
A
A= read_file($Monitoring::Sneck::Checks::debsecan::OS_RELEASE, "err_mode", "quiet")
Aresult
11
00
206 84 ▶ return $1 if $os_release =~ /^VERSION_CODENAME="?([a-z][a-z0-9-]*)"?\s*$/m;
Branch: 1 branch
BranchTrueFalseTotal
if $os_release =~ /^VERSION_CODENAME="?([a-z][a-z0-9-]*)"?\s*$/mu 71 13 84
207  
208 13 return undef;
209 }
210  
211 19 1;
« blib/lib/Monitoring/Sneck/Checks/oom.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/dns.pm »