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

filter: Statementstmt 95.0% 96 / 101 Branchbran 91.6% 33 / 36 Conditioncond 72.2% 13 / 18 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 89.7% 167 / 186 CC 30 SCAR 22.0
CC
30
Cov
92%
check
15.3
files
10.1
__ANON__
6.0
CRAP 9.0
« blib/lib/Monitoring/Sneck/Checks/config_value.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/mount.pm »
1 package Monitoring::Sneck::Checks::cert_dir;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Monitoring::Sneck::Checks::cert_dir - checks every certificate in directories
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 3 our $VERSION = '0.0.1';
21  
22 =head1 SYNOPSIS
23  
24 ssl_dir|perl:cert_dir -d /usr/local/etc/ssl
25 le_certs|perl:cert_dir -d /usr/local/etc/letsencrypt/live -r -m 'cert\.pem$' -w 21 -c 7
26  
27 =head1 DESCRIPTION
28  
29 Checks every certificate in the PEM files in each directory, without
30 having to name each file. Files with no certificates, such as keys
31 kept next to them, are skipped. A file reached more than once, such as
32 through a symlink, is only checked once, by its own name.
33  
34 A directory that can not be read is CRITICAL. So is a expired
35 certificate, or one that is not good yet. Otherwise each certificate is
36 WARNING or CRITICAL by the days it has left. A file that can not be
37 read or parsed is UNKNOWN, as is finding no certificates at all.
38  
39 The status line says how many were checked and which expires soonest.
40 Each one that is not OK gets its own line after it.
41  
42 =head1 OPTIONS
43  
44 - -d, --dir :: A directory. May be given more than once. Required.
45  
46 - -r, --recursive :: Look in the directories under them too.
47  
48 - -m, --match :: A regex the file names must match.
49 Default :: \.(?:pem|crt|cer)$
50  
51 - -w, --warning :: Days left at or under which it is WARNING.
52 Default :: 30
53  
54 - -c, --critical :: Days left at or under which it is CRITICAL.
55 Default :: 14
56  
57 =cut
58  
59 8 ▶sub option_spec { return ( 'dir|d=s@', 'recursive|r', 'match|m=s', 'warning|w=i', 'critical|c=i' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
60  
61 8 ▶sub defaults { return { match => '\.(?:pem|crt|cer)$', warning => 30, critical => 14 } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
62  
63 8 ▶sub required { return ('dir') }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
64  
65 8 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
66 8 my ( $self, $options ) = @_;
67  
68 8 ▶ return ('--match is not a valid regex') if !eval { qr/$options->{match}/; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { qr/$options->{"match"}/u; 1 } } 1 7 8
69  
70 7 return ();
71 }
72  
73 =head1 METHODS
74  
75 =head2 check
76  
77 Does the check.
78  
79 =cut
80  
81 7 ▶sub check {
Subroutine check: called CC=15 SCAR=27.3
Pod documented
82 7 my ($self) = @_;
83  
84 7 require IO::Socket::SSL::Utils;
85 7 my @codes;
86 my @problems;
87 7 my $count = 0;
88 7 my $soonest;
89 7 my ( $files, $unreadable ) = $self->files;
90 7 foreach my $dir ( @{$unreadable} ) {
91 1 push( @codes, $self->CRITICAL );
92 1 push( @problems, 'can not read ' . $dir );
93 }
94  
95 7 my $files_with_certs = 0;
96 7 foreach my $file ( @{$files} ) {
97 26 my $certs = eval { $self->pem_certs($file) };
98 26 ▶ if ( !defined($certs) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $certs) 0 26 26
99 0 my $why = $@;
100 0 $why =~ s/\s+\z//;
101 0 push( @codes, $self->UNKNOWN );
102 0 push( @problems, $why );
103 0 next;
104 }
105 26 ▶ $files_with_certs++ if scalar( @{$certs} );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$certs;} 24 2 26
106 26 my $number = 0;
107 26 foreach my $cert ( @{$certs} ) {
108 33 $number++;
109 33 ▶ my $where = $file . ( scalar( @{$certs} ) > 1 ? ' #' . $number : '' );
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$certs;} > 1 ? : 18 15 33
110 33 ▶ if ( !$cert ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cert) 1 32 33
111 1 push( @codes, $self->UNKNOWN );
112 1 push( @problems, $where . ' can not be parsed' );
113 1 next;
114 }
115 32 $count++;
116 32 my $details = $self->cert_details($cert);
117 32 IO::Socket::SSL::Utils::CERT_free($cert);
118 32 my ( $code, $message ) = $self->cert_status( $details, $self->opt('warning'), $self->opt('critical') );
119 32 push( @codes, $code );
120 32 ▶ push( @problems, $where . ' ' . $message ) if $code != $self->OK;
Branch: 1 branch
BranchTrueFalseTotal
if $code != $self->OK 4 28 32
121 32 ▶ if ( !defined($soonest) || $details->{not_after} < $soonest->{details}{not_after} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $soonest or $details->{"not_after"} < $soonest->{"details"}{"not_after"}) 12 20 32
Condition100%3/3
not defined $soonest or $details->{"not_after"} < $soonest->{"details"}{"not_after"}
l!l&&r!l&&!r
5720
MC/DC100%2/2
not defined $soonest or $details->{"not_after"} < $soonest->{"details"}{"not_after"}
not defined $soonest $details->{"not_after"} < $soonest->{"details"}{"not_after"}
Truth table100%3/3
not(A) or B
A= not defined $soonestB= $details->{"not_after"} < $soonest->{"details"}{"not_after"}
ABresult
1X1
011
000
122 12 $soonest = { details => $details, where => $where, message => $message };
123 }
124 } ## end foreach my $cert ( @{$certs} )
125 } ## end foreach my $file ( @{$files} )
126  
127 7 my $dirs = join( ', ', @{ $self->opt('dir') } );
128 7 ▶ if ( !defined($soonest) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $soonest) 2 5 7
129 2 push( @codes, $self->UNKNOWN );
130 2 return $self->finish( $self->worst(@codes), 'no certificates in ' . $dirs, @problems );
131 }
132  
133 $self->perfdata(
134 label => 'days',
135 value => $soonest->{details}{days_left},
136 5 warning => $self->opt('warning'),
137 critical => $self->opt('critical')
138 );
139 5 $self->perfdata( label => 'certificates', value => $count, min => 0 );
140 5 ▶ my $message
Branch: 2 branches
BranchTrueFalseTotal
$count == 1 ? : 0 5 5
$files_with_certs == 1 ? : 0 5 5
141 = $count
142 . ' certificate'
143 . ( $count == 1 ? '' : 's' ) . ' in '
144 . $files_with_certs . ' file'
145 . ( $files_with_certs == 1 ? '' : 's' ) . ' in '
146 . $dirs;
147 5 ▶ $message = $message . ', ' . scalar(@problems) . ' not OK' if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 3 2 5
148 5 $message = $message . ', soonest ' . $soonest->{where} . ' ' . $soonest->{message};
149  
150 5 return $self->finish( $self->worst(@codes), $message, @problems );
151 } ## end sub check
152  
153 =head2 files
154  
155 Finds the files to check.
156  
157 Returns a array ref of the files whose names match, each only once,
158 real files first and then symlinks, each sorted, and a array ref of the
159 directories that could not be read.
160  
161 my ( $files, $unreadable ) = $check->files;
162  
163 =cut
164  
165 7 ▶sub files {
Subroutine files: called CC=10 SCAR=23.1
Pod documented
166 7 my ($self) = @_;
167  
168 7 my $match = $self->opt('match');
169 7 my %seen;
170 my @files;
171 7 my @unreadable;
172 7 foreach my $dir ( @{ $self->opt('dir') } ) {
173 7 ▶ if ( !-d $dir || !opendir( my $handle, $dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir and opendir my $handle, $dir) 1 6 7
Condition66%2/3
-d $dir and opendir my $handle, $dir
!ll&&!rl&&r
106
MC/DC50%1/2
-d $dir and opendir my $handle, $dir
-d $dir opendir my $handle, $dir
Truth table66%2/3
A and B
A= -d $dirB= opendir my $handle, $dir
ABresult
0X0
100
111
174 1 push( @unreadable, $dir );
175 1 next;
176 }
177 6 my @found;
178 File::Find::find(
179 {
180 no_chdir => 1,
181 55 ▶ wanted => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
182 55 ▶ if ( -d $File::Find::name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $File::Find::name) 10 45 55
183 10 ▶ $File::Find::prune = 1 if !$self->opt('recursive') && $File::Find::name ne $dir;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->opt("recursive") and $File::Find::name ne $dir 3 7 10
Condition100%3/3
not $self->opt("recursive") and $File::Find::name ne $dir
!ll&&!rl&&r
253
MC/DC100%2/2
not $self->opt("recursive") and $File::Find::name ne $dir
not $self->opt("recursive") $File::Find::name ne $dir
Truth table100%3/3
not(A) and B
A= not $self->opt("recursive")B= $File::Find::name ne $dir
ABresult
0X0
100
111
184 10 return;
185 }
186 45 my $name = ( split( m{/}, $File::Find::name ) )[-1];
187 45 ▶ push( @found, $File::Find::name ) if -f $File::Find::name && $name =~ /$match/;
Branch: 1 branch
BranchTrueFalseTotal
if -f $File::Find::name and $name =~ /$match/u 29 16 45
Condition66%2/3
-f $File::Find::name and $name =~ /$match/u
!ll&&!rl&&r
01629
MC/DC50%1/2
-f $File::Find::name and $name =~ /$match/u
-f $File::Find::name $name =~ /$match/u
Truth table66%2/3
A and B
A= -f $File::Find::nameB= $name =~ /$match/u
ABresult
0X0
100
111
188 },
189 },
190 6 $dir
191 );
192 # real files before symlinks, so a file reached both ways goes by its own name
193 6 ▶ foreach my $file ( sort { ( -l $a ? 1 : 0 ) <=> ( -l $b ? 1 : 0 ) || $a cmp $b } @found ) {
Branch: 2 branches
BranchTrueFalseTotal
-l $a ? : 6 43 49
-l $b ? : 6 43 49
Condition66%2/3
(-l $a ? 1 : 0) <=> (-l $b ? 1 : 0) || $a cmp $b
l!l&&r!l&&!r
12370
MC/DC0%0/2
(-l $a ? 1 : 0) <=> (-l $b ? 1 : 0) || $a cmp $b
(-l $a ? 1 : 0) <=> (-l $b ? 1 : 0) $a cmp $b
Truth table66%2/3
A || B
A= (-l $a ? 1 : 0) <=> (-l $b ? 1 : 0)B= $a cmp $b
ABresult
1X1
011
000
194 29 ▶ my $real = Cwd::abs_path($file) // $file;
Condition33%1/3
Cwd::abs_path($file) // $file
l!l&&r!l&&!r
2900
MC/DC0%0/2
Cwd::abs_path($file) // $file
Cwd::abs_path($file) $file
Truth table33%1/3
A // B
A= Cwd::abs_path($file)B= $file
ABresult
1X1
011
000
195 29 ▶ next if $seen{$real}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$real}++ 3 26 29
196 26 push( @files, $file );
197 }
198 } ## end foreach my $dir ( @{ $self->opt('dir') } )
199  
200 7 return ( \@files, \@unreadable );
201 } ## end sub files
202  
203 3 1;
« blib/lib/Monitoring/Sneck/Checks/config_value.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/mount.pm »