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

filter: Statementstmt 100% 54 / 54 Branchbran 96.1% 25 / 26 Conditioncond 81.8% 9 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 94.6% 106 / 112 CC 20 SCAR 18.2
CC
20
Cov
97%
check
9.0
value
7.0
validate
6.0
CRAP 6.2
« blib/lib/Monitoring/Sneck/Checks/uptime.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/swap.pm »
1 package Monitoring::Sneck::Checks::sysctl;
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  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::sysctl - checks sysctl values
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 7 our $VERSION = '0.0.1';
19  
20 =head1 SYNOPSIS
21  
22 !aslr|control=CIS-1.5.3 framework=CIS|perl:sysctl -e kernel.randomize_va_space=2
23 !no_forwarding||perl:sysctl -e net.ipv4.ip_forward=0 -e net.ipv6.conf.all.forwarding=0
24 !securelevel||perl:sysctl -e 'kern.securelevel>=1'
25 !ptrace||perl:sysctl -e 'kernel.yama.ptrace_scope=1|2|3' -m
26  
27 =head1 DESCRIPTION
28  
29 Checks each sysctl given is as wanted. One that is not, or is not
30 there, is CRITICAL, unless --missing-ok is given for those not there.
31  
32 Each --expect is a name, a comparison, and a value. = and != take
33 values split by |, any of which is enough for =, and none of which may
34 match for !=. <, <=, >, and >= compare as numbers. Values are compared
35 with runs of spaces squashed, as some, such as net.ipv4.ip_local_port_range,
36 have tabs.
37  
38 On Linux they are read from /proc/sys. A name with a / in it is taken
39 as the path under /proc/sys, for names with dots in parts of them,
40 such as net/ipv4/conf/eth0.100/forwarding. On FreeBSD they are read
41 through L<BSD::Sysctl>. Other OSes are UNKNOWN.
42  
43 =head1 OPTIONS
44  
45 - -e, --expect :: "name=value", or with !=, <, <=, >, or >=. May be
46 given more than once. Required.
47  
48 - -m, --missing-ok :: Sysctls that are not there are OK, as for those
49 from a module that is not loaded.
50  
51 =cut
52  
53 55 ▶sub option_spec { return ( 'expect|e=s@', 'missing-ok|m' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
54  
55 55 ▶sub required { return ('expect') }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
56  
57 55 ▶sub validate {
Subroutine validate: called CC=6 SCAR=17.9
Pod documented
58 55 my ( $self, $options ) = @_;
59  
60 55 my @problems;
61 55 ▶ foreach my $expect ( @{ $options->{expect} // [] } ) {
Condition100%2/2
$options->{"expect"} // []
l!l
505
MC/DC100%1/1
$options->{"expect"} // []
$options->{"expect"}
Truth table100%2/2
A
A= $options->{"expect"}
Aresult
11
00
62 75 my ( $name, $comparison, $value ) = $self->split_expect($expect);
63 75 ▶ if ( !defined($name) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $name) { } 5 70 75
elsif ($comparison =~ /[<>]/u and not $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/u) { } 5 120 125
64 5 push( @problems, '--expect "' . $expect . '" is not name=value' );
65 ▶ } elsif ( $comparison =~ /[<>]/ && $value !~ /\A-?[0-9]+(?:\.[0-9]+)?\z/ ) {
Condition100%3/3
$comparison =~ /[<>]/u and not $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/u
!ll&&!rl&&r
55105
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
ABresult
0X0
100
111
66 5 push( @problems, '--expect "' . $expect . '" needs a number for ' . $comparison );
67 }
68 }
69  
70 55 return @problems;
71 } ## end sub validate
72  
73 =head1 METHODS
74  
75 =head2 check
76  
77 Does the check.
78  
79 =cut
80  
81 40 ▶sub check {
Subroutine check: called CC=9 SCAR=22.0
Pod documented
82 40 my ($self) = @_;
83  
84 40 ▶ die( 'not supported on ' . $self->os . "\n" ) if $self->os ne 'linux' && $self->os ne 'freebsd';
Branch: 1 branch
BranchTrueFalseTotal
if $self->os ne "linux" and $self->os ne "freebsd" 0 40 40
Condition66%2/3
$self->os ne "linux" and $self->os ne "freebsd"
!ll&&!rl&&r
30100
MC/DC0%0/2
$self->os ne "linux" and $self->os ne "freebsd"
$self->os ne "linux" $self->os ne "freebsd"
Truth table66%2/3
A and B
A= $self->os ne "linux"B= $self->os ne "freebsd"
ABresult
0X0
100
111
85 40 my @problems;
86 my @seen;
87 40 foreach my $expect ( @{ $self->opt('expect') } ) {
88 65 my ( $name, $comparison, $wanted ) = $self->split_expect($expect);
89 65 my $value = $self->value($name);
90 65 ▶ if ( !defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 15 50 65
91 15 ▶ push( @problems, $name . ' is not there' ) if !$self->opt('missing-ok');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->opt("missing-ok") 10 5 15
92 15 next;
93 }
94 50 push( @seen, $name . ' ' . $self->shown($value) );
95 50 ▶ next if $self->expect_met( $value, $comparison, $wanted );
Branch: 1 branch
BranchTrueFalseTotal
if $self->expect_met($value, $comparison, $wanted) 25 25 50
96 25 push( @problems,
97 $name . ' is ' . $self->shown($value) . ', wanted ' . $self->expect_wanted( $comparison, $wanted ) );
98 } ## end foreach my $expect ( @{ $self->opt('expect') } )
99  
100 40 ▶ return $self->finish( $self->CRITICAL, join( ', ', @problems ) ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 20 20 40
101 20 ▶ return $self->finish( $self->OK, defined( $seen[0] ) ? join( ', ', @seen ) : 'none of them are there' );
Branch: 1 branch
BranchTrueFalseTotal
defined $seen[0] ? : 15 5 20
102 } ## end sub check
103  
104 =head2 value
105  
106 Returns the value of a sysctl with runs of space squashed, or undef if
107 it is not there, or on FreeBSD is a struct BSD::Sysctl can not give as
108 text.
109  
110 =cut
111  
112 65 ▶sub value {
Subroutine value: called CC=7 SCAR=19.5
Pod documented
113 65 my ( $self, $name ) = @_;
114  
115 65 my $value;
116 65 ▶ if ( $self->os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq 'linux') { } 55 10 65
117 55 ▶ my $path = $name =~ m{/} ? $name : $name =~ tr{.}{/}r;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ m[/]u ? : 10 45 55
118 55 ▶ return undef if $path =~ m{(?:\A|/)\.\.(?:/|\z)};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[(?:\A|/)\.\.(?:/|\z)]u 5 50 55
119 50 $value = eval { $self->proc_file( 'sys/' . $path ) };
120 } else {
121 10 $value = eval { $self->sysctl($name) };
122 10 ▶ $value = join( ' ', @{$value} ) if ref($value) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 5 5 10
123 }
124 60 ▶ return undef if !defined($value) || ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 10 50 60
Condition66%2/3
not defined $value or ref $value
l!l&&r!l&&!r
10050
MC/DC50%1/2
not defined $value or ref $value
not defined $value ref $value
Truth table66%2/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
125 50 $value =~ s/\s+/ /g;
126 50 $value =~ s/\A | \z//g;
127  
128 50 return $value;
129 } ## end sub value
130  
131 7 1;
« blib/lib/Monitoring/Sneck/Checks/uptime.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/swap.pm »