« blib/lib/Monitoring/Sneck/Checks/packages.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/cert_dir.pm »
| 1 | package Monitoring::Sneck::Checks::config_value; | ||||||||||||||||||||||||||||
| 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::config_value - checks values in config files | ||||||||||||||||||||||||||||
| 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 | !pass_max_days|control=CIS-5.5.1.1 framework=CIS|perl:config_value -f /etc/login.defs -e 'PASS_MAX_DAYS<=365' -e 'PASS_MIN_DAYS>=1' | ||||||||||||||||||||||||||||
| 24 | !no_forwarding||perl:config_value -f /etc/sysctl.conf -S = -e net.ipv4.ip_forward=0 -d net.ipv4.ip_forward=0 | ||||||||||||||||||||||||||||
| 25 | !sendmail_off||perl:config_value -f /etc/rc.conf -S = -e sendmail_enable=NO|NONE | ||||||||||||||||||||||||||||
| 26 | !umask||perl:config_value -f /etc/login.defs -e UMASK=027|077 | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Reads a file of settings, one per line, and checks each given with | ||||||||||||||||||||||||||||
| 31 | --expect, the same way as L<Monitoring::Sneck::Checks::sysctl>, with =, | ||||||||||||||||||||||||||||
| 32 | !=, <, <=, >, or >=. One that is not as wanted, or is not set and has | ||||||||||||||||||||||||||||
| 33 | no --default, is CRITICAL, as is the file not being readable. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | By default the name and value are split by spaces or a =, as for | ||||||||||||||||||||||||||||
| 36 | login.defs and the like. --separator sets what splits them. Spaces | ||||||||||||||||||||||||||||
| 37 | around it, and quotes around the value, are dropped, so 'x="YES"' is | ||||||||||||||||||||||||||||
| 38 | x YES. Lines starting with # are left out, as is anything after a # | ||||||||||||||||||||||||||||
| 39 | that has a space before it. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | The last value wins, as for rc.conf and sysctl.conf, unless --first | ||||||||||||||||||||||||||||
| 42 | is given. Names and values are compared without case, unless | ||||||||||||||||||||||||||||
| 43 | --case-sensitive is given. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | - -f, --file :: The file. Required. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | - -e, --expect :: "name=value", or with !=, <, <=, >, or >=. May be | ||||||||||||||||||||||||||||
| 50 | given more than once. Required. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | - -d, --default :: "name=value", what is used if it is not set. May be | ||||||||||||||||||||||||||||
| 53 | given more than once. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | - -S, --separator :: What splits a name and its value. | ||||||||||||||||||||||||||||
| 56 | Default :: spaces or a = | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | - -F, --first :: The first value wins. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | - -C, --case-sensitive :: Compare names and values with case. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 16 | ▶ | sub option_spec { return ( 'file|f=s', 'expect|e=s@', 'default|d=s@', 'separator|S=s', 'first|F', 'case-sensitive|C' ) } | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 16 | ▶ | sub required { return ( 'file', 'expect' ) } | ||||||||||||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 16 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 16 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 16 | my @problems; | |||||||||||||||||||||||||||
| 72 | 16 | ▶ | foreach my $expect ( @{ $options->{expect} // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"expect"} // []
MC/DC100%1/1
$options->{"expect"} // []
$options->{"expect"}
Truth table100%2/2
A
A= $options->{"expect"}
| |||||||||||||||||||||||||||||
| 73 | 23 | my ( $name, $comparison, $value ) = $self->split_expect($expect); | |||||||||||||||||||||||||||
| 74 | 23 | ▶ | if ( !defined($name) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 75 | 1 | push( @problems, '--expect "' . $expect . '" is not name=value' ); | |||||||||||||||||||||||||||
| 76 | ▶ | } elsif ( $comparison =~ /[<>]/ && $value !~ /\A-?[0-9]+(?:\.[0-9]+)?\z/ ) { | |||||||||||||||||||||||||||
|
Condition100%3/3
$comparison =~ /[<>]/u and not $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/u
MC/DC100%2/2
$comparison =~ /[<>]/u and not $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/u
$comparison =~ /[<>]/u
not $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/u
Truth table100%3/3
A and not(B)
A= $comparison =~ /[<>]/uB= not $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/u
| |||||||||||||||||||||||||||||
| 77 | 1 | push( @problems, '--expect "' . $expect . '" needs a number for ' . $comparison ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 16 | ▶ | foreach my $default ( @{ $options->{default} // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"default"} // []
MC/DC100%1/1
$options->{"default"} // []
$options->{"default"}
Truth table100%2/2
A
A= $options->{"default"}
| |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | push( @problems, '--default "' . $default . '" is not name=value' ) if $default !~ /\A[^=\s]+=/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | push( @problems, '--separator can not be empty' ) | ||||||||||||||||||||||||||||
| 84 | 16 | ▶ | if defined( $options->{separator} ) && $options->{separator} eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $options->{"separator"} and $options->{"separator"} eq ""
MC/DC100%2/2
defined $options->{"separator"} and $options->{"separator"} eq ""
defined $options->{"separator"}
$options->{"separator"} eq ""
Truth table100%3/3
A and B
A= defined $options->{"separator"}B= $options->{"separator"} eq ""
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 16 | return @problems; | |||||||||||||||||||||||||||
| 87 | } ## end sub validate | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head1 METHODS | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head2 check | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Does the check. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 12 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 12 | my $settings = $self->settings; | |||||||||||||||||||||||||||
| 101 | 12 | ▶ | return $self->finish( $self->CRITICAL, $settings ) if !ref($settings); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 11 | my $case = $self->opt('case-sensitive'); | |||||||||||||||||||||||||||
| 103 | 11 | my %defaults; | |||||||||||||||||||||||||||
| 104 | 11 | ▶ | foreach my $default ( @{ $self->opt('default') // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("default") // []
MC/DC100%1/1
$self->opt("default") // []
$self->opt("default")
Truth table100%2/2
A
A= $self->opt("default")
| |||||||||||||||||||||||||||||
| 105 | 1 | my ( $name, $value ) = split( /=/, $default, 2 ); | |||||||||||||||||||||||||||
| 106 | 1 | ▶ | $defaults{ $case ? $name : lc($name) } = $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 11 | my @problems; | |||||||||||||||||||||||||||
| 110 | my @seen; | ||||||||||||||||||||||||||||
| 111 | 11 | foreach my $expect ( @{ $self->opt('expect') } ) { | |||||||||||||||||||||||||||
| 112 | 19 | my ( $name, $comparison, $wanted ) = $self->split_expect($expect); | |||||||||||||||||||||||||||
| 113 | 19 | ▶ | my $key = $case ? $name : lc($name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 19 | my $value = $settings->{$key}; | |||||||||||||||||||||||||||
| 115 | 19 | my $source = ''; | |||||||||||||||||||||||||||
| 116 | 19 | ▶ | if ( !defined($value) && defined( $defaults{$key} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $value and defined $defaults{$key}
MC/DC100%2/2
not defined $value and defined $defaults{$key}
not defined $value
defined $defaults{$key}
Truth table100%3/3
not(A) and B
A= not defined $valueB= defined $defaults{$key}
| |||||||||||||||||||||||||||||
| 117 | 1 | $value = $defaults{$key}; | |||||||||||||||||||||||||||
| 118 | 1 | $source = ' by default'; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 19 | ▶ | if ( !defined($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 2 | push( @problems, $name . ' is not set' ); | |||||||||||||||||||||||||||
| 122 | 2 | next; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 17 | push( @seen, $name . ' ' . $self->shown($value) . $source ); | |||||||||||||||||||||||||||
| 125 | 17 | ▶ | next if $self->expect_met( $value, $comparison, $wanted, !$case ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 7 | push( @problems, | |||||||||||||||||||||||||||
| 127 | $name . ' is ' | ||||||||||||||||||||||||||||
| 128 | . $self->shown($value) | ||||||||||||||||||||||||||||
| 129 | . $source | ||||||||||||||||||||||||||||
| 130 | . ', wanted ' | ||||||||||||||||||||||||||||
| 131 | . $self->expect_wanted( $comparison, $wanted ) ); | ||||||||||||||||||||||||||||
| 132 | } ## end foreach my $expect ( @{ $self->opt('expect') } ) | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 11 | ▶ | return $self->finish( $self->CRITICAL, join( ', ', @problems ) ) if defined( $problems[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 5 | return $self->finish( $self->OK, join( ', ', @seen ) ); | |||||||||||||||||||||||||||
| 136 | } ## end sub check | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 settings | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Reads the file into a hash ref of names, lower cased unless | ||||||||||||||||||||||||||||
| 141 | --case-sensitive, to values. Returns why if it can not be read. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 12 | ▶ | sub settings { | ||||||||||||||||||||||||||
|
Subroutine
settings: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 12 | my $file = $self->opt('file'); | |||||||||||||||||||||||||||
| 149 | 12 | my $content = read_file( $file, err_mode => 'quiet' ); | |||||||||||||||||||||||||||
| 150 | 12 | ▶ | return 'can not read ' . $file . ', ' . $! if !defined($content); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 11 | ▶ | my $separator = defined( $self->opt('separator') ) ? qr/\s*\Q${\ $self->opt('separator')}\E\s*/ : qr/\s*=\s*|\s+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 11 | my %settings; | |||||||||||||||||||||||||||
| 154 | 11 | foreach my $line ( split( /\r?\n/, $content ) ) { | |||||||||||||||||||||||||||
| 155 | 52 | ▶ | next if $line =~ /\A\s*(?:#|\z)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 47 | $line =~ s/\s+#.*\z//; | |||||||||||||||||||||||||||
| 157 | 47 | $line =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||
| 158 | 47 | my ( $name, $value ) = split( $separator, $line, 2 ); | |||||||||||||||||||||||||||
| 159 | 47 | ▶ | next if !defined($value) || $name eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $value or $name eq ""
MC/DC0%0/2
not defined $value or $name eq ""
not defined $value
$name eq ""
Truth table33%1/3
not(A) or B
A= not defined $valueB= $name eq ""
| |||||||||||||||||||||||||||||
| 160 | 47 | $value =~ s/\A(["'])(.*)\1\z/$2/; | |||||||||||||||||||||||||||
| 161 | 47 | $value =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 162 | 47 | ▶ | my $key = $self->opt('case-sensitive') ? $name : lc($name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 47 | ▶ | next if $self->opt('first') && exists( $settings{$key} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->opt("first") and exists $settings{$key}
MC/DC100%2/2
$self->opt("first") and exists $settings{$key}
$self->opt("first")
exists $settings{$key}
Truth table100%3/3
A and B
A= $self->opt("first")B= exists $settings{$key}
| |||||||||||||||||||||||||||||
| 164 | 46 | $settings{$key} = $value; | |||||||||||||||||||||||||||
| 165 | } ## end foreach my $line ( split( /\r?\n/, $content ) ) | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 11 | return \%settings; | |||||||||||||||||||||||||||
| 168 | } ## end sub settings | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 3 | 1; | |||||||||||||||||||||||||||