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

filter: Statementstmt 98.3% 61 / 62 Branchbran 75.0% 24 / 32 Conditioncond 73.3% 11 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 86.2% 113 / 131 CC 28 SCAR 22.3
CC
28
Cov
88%
installed
13.8
validate
9.2
check
9.0
CRAP 9.3
« blib/lib/Monitoring/Sneck/Checks/tcp.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/config_value.pm »
1 package Monitoring::Sneck::Checks::packages;
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  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::packages - checks packages are or are not installed
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 7 our $VERSION = '0.0.1';
20  
21 # Where things are. Set by tests.
22 7 our $DPKG_STATUS = '/var/lib/dpkg/status';
23 7 our $PKG = '/usr/sbin/pkg';
24  
25 =head1 SYNOPSIS
26  
27 !no_telnet|control=CIS-2.3.4 framework=CIS|perl:packages -a telnet -a rsh-client
28 !auditd||perl:packages -i auditd -i audispd-plugins
29 !www_no_compilers||perl:packages -j www -a gcc13 -a llvm18
30  
31 =head1 DESCRIPTION
32  
33 Checks packages are installed, or are not, as given. Anything not as
34 given is CRITICAL.
35  
36 On Debian and its kin, the dpkg status file is read, and a package
37 only counts as installed once fully installed, not if it was removed
38 with its config files left. On FreeBSD, pkg query is run, for a jail
39 too with --jail. Others are UNKNOWN.
40  
41 =head1 OPTIONS
42  
43 - -i, --installed :: A package that must be installed. May be given
44 more than once.
45  
46 - -a, --absent :: A package that must not be installed. May be given
47 more than once.
48  
49 - -j, --jail :: On FreeBSD, check the packages in this jail. UNKNOWN on
50 other OSes.
51  
52 One or more of --installed or --absent must be given.
53  
54 =cut
55  
56 15 ▶sub option_spec { return ( 'installed|i=s@', 'absent|a=s@', 'jail|j=s' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
57  
58 15 ▶sub validate {
Subroutine validate: called CC=9 SCAR=22.2
Pod documented
59 15 my ( $self, $options ) = @_;
60  
61 15 my @problems;
62 15 foreach my $name ( 'installed', 'absent' ) {
63 30 ▶ foreach my $package ( @{ $options->{$name} // [] } ) {
Condition100%2/2
$options->{$name} // []
l!l
237
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
Aresult
11
00
64 29 ▶ push( @problems, '--' . $name . ' "' . $package . '" is not a package name' )
Branch: 1 branch
BranchTrueFalseTotal
unless $package =~ /\A[A-Za-z0-9][A-Za-z0-9+._:-]*\z/u 0 29 29
65 if $package !~ /\A[A-Za-z0-9][A-Za-z0-9+._:-]*\z/;
66 }
67 }
68 push( @problems, 'nothing to check, give --installed or --absent' )
69 15 ▶ if !defined( $options->{installed} ) && !defined( $options->{absent} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"installed"} or defined $options->{"absent"} 1 14 15
Condition100%3/3
defined $options->{"installed"} or defined $options->{"absent"}
l!l&&r!l&&!r
1221
MC/DC100%2/2
defined $options->{"installed"} or defined $options->{"absent"}
defined $options->{"installed"} defined $options->{"absent"}
Truth table100%3/3
A or B
A= defined $options->{"installed"}B= defined $options->{"absent"}
ABresult
1X1
011
000
70 push( @problems, '--jail "' . $options->{jail} . '" is not a jail name or ID' )
71 15 ▶ if defined( $options->{jail} ) && $options->{jail} !~ /\A[A-Za-z0-9_.-]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"jail"} and not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u 0 15 15
Condition66%2/3
defined $options->{"jail"} and not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
!ll&&!rl&&r
1050
MC/DC0%0/2
defined $options->{"jail"} and not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
defined $options->{"jail"} not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
Truth table66%2/3
A and not(B)
A= defined $options->{"jail"}B= not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
ABresult
0X0
100
111
72  
73 15 return @problems;
74 } ## end sub validate
75  
76 =head1 METHODS
77  
78 =head2 check
79  
80 Does the check.
81  
82 =cut
83  
84 14 ▶sub check {
Subroutine check: called CC=9 SCAR=22.0
Pod documented
85 14 my ($self) = @_;
86  
87 14 my $installed = $self->installed;
88 9 my @problems;
89 my @fine;
90 9 ▶ foreach my $package ( @{ $self->opt('installed') // [] } ) {
Condition100%2/2
$self->opt("installed") // []
l!l
81
MC/DC100%1/1
$self->opt("installed") // []
$self->opt("installed")
Truth table100%2/2
A
A= $self->opt("installed")
Aresult
11
00
91 11 ▶ if ( $installed->{$package} ) { push( @fine, $package . ' installed' ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($installed->{$package}) { } 8 3 11
92 3 else { push( @problems, $package . ' is not installed' ) }
93 }
94 9 ▶ foreach my $package ( @{ $self->opt('absent') // [] } ) {
Condition50%1/2
$self->opt("absent") // []
l!l
90
MC/DC0%0/1
$self->opt("absent") // []
$self->opt("absent")
Truth table50%1/2
A
A= $self->opt("absent")
Aresult
11
00
95 12 ▶ if ( $installed->{$package} ) { push( @problems, $package . ' is installed' ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($installed->{$package}) { } 4 8 12
96 8 else { push( @fine, $package . ' not installed' ) }
97 }
98 9 ▶ my $where = defined( $self->opt('jail') ) ? ' in jail ' . $self->opt('jail') : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->opt('jail') ? : 1 8 9
99  
100 9 ▶ return $self->finish( $self->CRITICAL, join( ', ', @problems ) . $where ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 4 5 9
101 5 return $self->finish( $self->OK, join( ', ', @fine ) . $where );
102 } ## end sub check
103  
104 =head2 installed
105  
106 Returns a hash ref of the names of the installed packages. Dies if
107 they can not be found.
108  
109 =cut
110  
111 14 ▶sub installed {
Subroutine installed: called CC=12 SCAR=26.2
Pod documented
112 14 my ($self) = @_;
113  
114 14 ▶ if ( $self->os eq 'freebsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "freebsd") 5 9 14
115 5 my @command = ($PKG);
116 5 ▶ push( @command, '-j', $self->opt('jail') ) if defined( $self->opt('jail') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("jail") 2 3 5
117 5 my ( $exit, $output ) = $self->run_command( @command, 'query', '%n' );
118 3 ▶ die( 'pkg query exited ' . $exit . "\n" ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 3 3
119 3 return { map { $_ => 1 } split( /\n/, $output ) };
120 }
121 9 ▶ die("--jail is only for freebsd\n") if defined( $self->opt('jail') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("jail") 3 6 9
122 6 ▶ if ( $self->os eq 'linux' && -e $DPKG_STATUS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux" and -e $Monitoring::Sneck::Checks::packages::DPKG_STATUS) 6 0 6
Condition33%1/3
$self->os eq "linux" and -e $Monitoring::Sneck::Checks::packages::DPKG_STATUS
!ll&&!rl&&r
006
MC/DC0%0/2
$self->os eq "linux" and -e $Monitoring::Sneck::Checks::packages::DPKG_STATUS
$self->os eq "linux" -e $Monitoring::Sneck::Checks::packages::DPKG_STATUS
Truth table33%1/3
A and B
A= $self->os eq "linux"B= -e $Monitoring::Sneck::Checks::packages::DPKG_STATUS
ABresult
0X0
100
111
123 6 my $status = read_file( $DPKG_STATUS, err_mode => 'quiet' );
124 6 ▶ die( 'can not read ' . $DPKG_STATUS . ', ' . $! . "\n" ) if !defined($status);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status 0 6 6
125 6 my %installed;
126 6 foreach my $stanza ( split( /\n\n+/, $status ) ) {
127 18 ▶ next if $stanza !~ /^Status: \S+ \S+ installed$/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $stanza =~ /^Status: \S+ \S+ installed$/mu 6 12 18
128 12 ▶ $installed{$1} = 1 if $stanza =~ /^Package: (\S+)$/m;
Branch: 1 branch
BranchTrueFalseTotal
if $stanza =~ /^Package: (\S+)$/mu 12 0 12
129 }
130 6 return \%installed;
131 } ## end if ( $self->os eq 'linux' && -e $DPKG_STATUS)
132  
133 0 ▶ die( 'not supported on ' . $self->os . ( $self->os eq 'linux' ? ' without dpkg' : '' ) . "\n" );
Branch: 1 branch
BranchTrueFalseTotal
$self->os eq 'linux' ? : 0 0 0
134 } ## end sub installed
135  
136 7 1;
« blib/lib/Monitoring/Sneck/Checks/tcp.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/config_value.pm »