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

filter: Statementstmt 100% 54 / 54 Branchbran 86.3% 19 / 22 Conditioncond 83.3% 15 / 18 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 93.2% 110 / 118 CC 23 SCAR 22.6
CC
23
Cov
94%
check
14.0
validate
10.3
BEGIN
1.0
CRAP 9.6
« blib/lib/Monitoring/Sneck/Checks/checksums.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/tcp.pm »
1 package Monitoring::Sneck::Checks::file_content;
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 File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::file_content - checks a file being there, or not, and what is in it
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 3 our $VERSION = '0.0.1';
20  
21 =head1 SYNOPSIS
22  
23 hourly_logrotate|perl:file_content -f /etc/cron.hourly/logrotate -x -r /etc/logrotate\.conf
24 app_logrotate|perl:file_content -f /etc/logrotate.d/app
25 no_root_ssh|perl:file_content -f /etc/ssh/sshd_config -r '^PermitRootLogin\s+no'
26 no_nologin|perl:file_content -f /etc/nologin -A
27  
28 =head1 DESCRIPTION
29  
30 A file that is not there is CRITICAL, as is one missing a line for a
31 --regex, or with one for a --not-regex. Lines matching --comment are
32 skipped for both.
33  
34 =head1 OPTIONS
35  
36 - -f, --file :: The file. Required.
37  
38 - -r, --regex :: A regex at least one line must match. May be given
39 more than once.
40  
41 - -R, --not-regex :: A regex no line may match. May be given more than
42 once.
43  
44 - -x, --executable :: It must be executable.
45  
46 - -A, --absent :: It must not be there, in place of the others.
47  
48 - -C, --comment :: A regex for lines to skip.
49 Default :: ^\s*#
50  
51 =cut
52  
53 # The Getopt::Long specs of the options, see OPTIONS.
54 #
55 # Returns the list of specs.
56 #
57 # Example...
58 #
59 # my @specs = $check->option_spec;
60 # # ( 'file|f=s', 'regex|r=s@', 'not-regex|R=s@', 'executable|x', 'absent|A', 'comment|C=s' )
61 10 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
62 10 return ( 'file|f=s', 'regex|r=s@', 'not-regex|R=s@', 'executable|x', 'absent|A', 'comment|C=s' );
63 }
64  
65 # The option defaults, see OPTIONS.
66 #
67 # Returns a hash ref of option names to defaults.
68 #
69 # Example...
70 #
71 # my $defaults = $check->defaults;
72 # # { comment => '^\s*#' }
73 10 ▶sub defaults { return { comment => '^\s*#' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
74  
75 # The options that must be given.
76 #
77 # Returns the list of option names.
78 #
79 # Example...
80 #
81 # my @required = $check->required;
82 # # ( 'file' )
83 10 ▶sub required { return ('file') }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
84  
85 # Checks what Getopt::Long can not, every regex compiling and --absent not being mixed with what needs the file.
86 #
87 # Args...
88 #
89 # - options :: Hash ref of option names to values, defaults included. regex and not-regex are array refs.
90 #
91 # Returns a list of what is wrong, each a string, empty if nothing is.
92 #
93 # Example...
94 #
95 # my @problems = $check->validate( { file => '/etc/sudoers', regex => ['('], comment => '^\s*#' } );
96 # # ( '--regex "(" is not a valid regex' )
97 10 ▶sub validate {
Subroutine validate: called CC=10 SCAR=23.3
Pod documented
98 10 my ( $self, $options ) = @_;
99  
100 10 my @problems;
101 10 foreach my $name ( 'regex', 'not-regex' ) {
102 20 ▶ foreach my $regex ( @{ $options->{$name} // [] } ) {
Condition100%2/2
$options->{$name} // []
l!l
515
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
Aresult
11
00
103 5 ▶ push( @problems, '--' . $name . ' "' . $regex . '" is not a valid regex' ) if !eval { qr/$regex/ };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { qr/$regex/u } } 1 4 5
104 }
105 }
106 10 ▶ push( @problems, '--comment is not a valid regex' ) if !eval { qr/$options->{comment}/ };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { qr/$options->{"comment"}/u } } 0 10 10
107 10 ▶ if ( $options->{absent} && ( $options->{regex} || $options->{'not-regex'} || $options->{executable} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"absent"} and $options->{"regex"} || $options->{"not-regex"} || $options->{"executable"}) 1 9 10
Condition66%6/9
$options->{"absent"} and $options->{"regex"} || $options->{"not-regex"} || $options->{"executable"}
!ll&&!rl&&r
721
$options->{"regex"} or $options->{"not-regex"} or $options->{"executable"}
l!l&&r!l&&!r
012
$options->{"regex"} or $options->{"not-regex"}
l!l&&r!l&&!r
003
MC/DC50%2/4
$options->{"absent"} and $options->{"regex"} || $options->{"not-regex"} || $options->{"executable"}
$options->{"absent"} $options->{"regex"} $options->{"not-regex"} $options->{"executable"}
Truth table60%3/5
A and B or C or D
A= $options->{"absent"}B= $options->{"regex"}C= $options->{"not-regex"}D= $options->{"executable"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
108 1 push( @problems, '--absent can not be used with --regex, --not-regex, or --executable' );
109 }
110  
111 10 return @problems;
112 } ## end sub validate
113  
114 =head1 METHODS
115  
116 =head2 check
117  
118 Does the check.
119  
120 =cut
121  
122 8 ▶sub check {
Subroutine check: called CC=14 SCAR=26.4
Pod documented
123 8 my ($self) = @_;
124  
125 8 my $file = $self->opt('file');
126 8 ▶ if ( $self->opt('absent') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("absent")) 2 6 8
127 2 ▶ return $self->finish( $self->CRITICAL, $file . ' is there' ) if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 1 1 2
128 1 return $self->finish( $self->OK, $file . ' is not there' );
129 }
130  
131 6 ▶ return $self->finish( $self->CRITICAL, $file . ' is not a file' ) if !-f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 1 5 6
132 5 my $content = read_file( $file, err_mode => 'quiet' );
133 5 ▶ return $self->finish( $self->CRITICAL, 'can not read ' . $file . ', ' . $! ) if !defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 5 5
134  
135 5 my @problems;
136 5 ▶ push( @problems, 'is not executable' ) if $self->opt('executable') && !-x $file;
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt("executable") and not -x $file 1 4 5
Condition100%3/3
$self->opt("executable") and not -x $file
!ll&&!rl&&r
311
MC/DC100%2/2
$self->opt("executable") and not -x $file
$self->opt("executable") not -x $file
Truth table100%3/3
A and not(B)
A= $self->opt("executable")B= not -x $file
ABresult
0X0
100
111
137 5 my $comment = $self->opt('comment');
138 5 my @lines = grep { !/$comment/ } split( /\n/, $content );
139 5 ▶ foreach my $regex ( @{ $self->opt('regex') // [] } ) {
Condition100%2/2
$self->opt("regex") // []
l!l
32
MC/DC100%1/1
$self->opt("regex") // []
$self->opt("regex")
Truth table100%2/2
A
A= $self->opt("regex")
Aresult
11
00
140 3 ▶ push( @problems, 'has no line matching ' . $regex ) if !grep { /$regex/ } @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/$regex/u;} @lines 1 2 3
141 }
142 5 ▶ foreach my $regex ( @{ $self->opt('not-regex') // [] } ) {
Condition100%2/2
$self->opt("not-regex") // []
l!l
14
MC/DC100%1/1
$self->opt("not-regex") // []
$self->opt("not-regex")
Truth table100%2/2
A
A= $self->opt("not-regex")
Aresult
11
00
143 1 ▶ push( @problems, 'has a line matching ' . $regex ) if grep { /$regex/ } @lines;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/$regex/u;} @lines 1 0 1
144 }
145  
146 5 ▶ return $self->finish( $self->CRITICAL, $file . ' ' . join( ', ', @problems ) ) if @problems;
Branch: 1 branch
BranchTrueFalseTotal
if @problems 3 2 5
147  
148 2 return $self->finish( $self->OK, $file . ' looks good' );
149 } ## end sub check
150  
151 3 1;
« blib/lib/Monitoring/Sneck/Checks/checksums.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/tcp.pm »