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

filter: Statementstmt 98.8% 83 / 84 Branchbran 85.4% 41 / 48 Conditioncond 84.0% 21 / 25 MC/DCmcdc 71.4% 10 / 14 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 91.3% 168 / 184 CC 39 SCAR 29.1
CC
39
Cov
92%
check
27.1
configured
11.2
validate
4.0
CRAP 18.4
« blib/lib/Monitoring/Sneck/Checks/smtp.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/cciss.pm »
1 package Monitoring::Sneck::Checks::jails;
2  
3 12 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::jails - checks FreeBSD jails and bhyve VMs are running
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 12 our $VERSION = '0.0.1';
20  
21 # Where things are. Set by tests.
22 12 our $JLS = '/usr/sbin/jls';
23 12 our $VM = '/usr/local/sbin/vm';
24 12 our $JAIL_CONF = '/etc/jail.conf';
25 12 our $JAIL_CONF_D = '/etc/jail.conf.d';
26  
27 =head1 SYNOPSIS
28  
29 jails|perl:jails -a
30 web_jails|perl:jails -j www -j db
31 vms|perl:jails -v router -v nas
32  
33 =head1 DESCRIPTION
34  
35 Checks each jail given is running, through jls, and with
36 --all-configured, every jail defined in /etc/jail.conf and
37 /etc/jail.conf.d/*.conf. Each VM given with --vm must be running too,
38 through vm-bhyve's vm list. Any that is not is CRITICAL.
39 --all-configured finding no jails, or every one of them excluded, is
40 UNKNOWN, as there is nothing to check then.
41  
42 Only FreeBSD has these, others are UNKNOWN.
43  
44 =head1 OPTIONS
45  
46 - -j, --jail :: A jail that must be running, by name. May be given more
47 than once.
48  
49 - -a, --all-configured :: Every jail in jail.conf must be running.
50  
51 - -x, --exclude :: A jail for --all-configured to leave out. May be
52 given more than once.
53  
54 - -v, --vm :: A vm-bhyve VM that must be running. May be given more
55 than once.
56  
57 One or more of --jail, --all-configured, or --vm must be given.
58  
59 =cut
60  
61 32 ▶sub option_spec { return ( 'jail|j=s@', 'all-configured|a', 'exclude|x=s@', 'vm|v=s@' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
62  
63 32 ▶sub validate {
Subroutine validate: called CC=4 SCAR=13.9
Pod documented
64 32 my ( $self, $options ) = @_;
65  
66 return ('nothing to check, give --jail, --all-configured, or --vm')
67 32 ▶ if !defined( $options->{jail} ) && !$options->{'all-configured'} && !defined( $options->{vm} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"jail"} or $options->{"all-configured"} or defined $options->{"vm"} 1 31 32
Condition100%6/6
defined $options->{"jail"} or $options->{"all-configured"} or defined $options->{"vm"}
l!l&&r!l&&!r
2831
defined $options->{"jail"} or $options->{"all-configured"}
l!l&&r!l&&!r
7214
MC/DC100%3/3
defined $options->{"jail"} or $options->{"all-configured"} or defined $options->{"vm"}
defined $options->{"jail"} $options->{"all-configured"} defined $options->{"vm"}
Truth table100%4/4
A or B or C
A= defined $options->{"jail"}B= $options->{"all-configured"}C= defined $options->{"vm"}
ABCresult
1XX1
01X1
0011
0000
68  
69 31 return ();
70 }
71  
72 =head1 METHODS
73  
74 =head2 check
75  
76 Does the check.
77  
78 =cut
79  
80 25 ▶sub check {
Subroutine check: called CC=27 SCAR=33.0
Pod documented
81 25 my ($self) = @_;
82  
83 25 ▶ die( 'not supported on ' . $self->os . "\n" ) if $self->os ne 'freebsd';
Branch: 1 branch
BranchTrueFalseTotal
if $self->os ne "freebsd" 1 24 25
84 24 my @problems;
85 my @unknown;
86 24 ▶ my %wanted = map { $_ => 1 } @{ $self->opt('jail') // [] };
Condition100%2/2
$self->opt("jail") // []
l!l
618
MC/DC100%1/1
$self->opt("jail") // []
$self->opt("jail")
Truth table100%2/2
A
A= $self->opt("jail")
Aresult
11
00
87 24 ▶ if ( $self->opt('all-configured') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("all-configured")) 15 9 24
88 15 my @configured = $self->configured;
89 15 ▶ push( @unknown, 'no jails configured in ' . $JAIL_CONF . ' or ' . $JAIL_CONF_D ) if !defined( $configured[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $configured[0] 4 11 15
90 15 $wanted{$_} = 1 foreach @configured;
91 15 ▶ delete( @wanted{ @{ $self->opt('exclude') // [] } } );
Condition100%2/2
$self->opt("exclude") // []
l!l
69
MC/DC100%1/1
$self->opt("exclude") // []
$self->opt("exclude")
Truth table100%2/2
A
A= $self->opt("exclude")
Aresult
11
00
92 15 ▶ push( @unknown, 'every configured jail is excluded' ) if defined( $configured[0] ) && !%wanted;
Branch: 1 branch
BranchTrueFalseTotal
if defined $configured[0] and not %wanted 2 13 15
Condition100%3/3
defined $configured[0] and not %wanted
!ll&&!rl&&r
492
MC/DC100%2/2
defined $configured[0] and not %wanted
defined $configured[0] not %wanted
Truth table100%3/3
A and not(B)
A= defined $configured[0]B= not %wanted
ABresult
0X0
100
111
93 }
94 24 my $running_jails = 0;
95 24 ▶ if (%wanted) {
Branch: 1 branch
BranchTrueFalseTotal
if (%wanted) 15 9 24
96 15 my ( $exit, $output ) = $self->run_command( $JLS, '-n', 'name' );
97 12 ▶ return $self->finish( $self->UNKNOWN, 'jls exited ' . $exit ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 12 12
98 12 my %running = map { $_ => 1 } $output =~ /(?:^|\s)name=(\S+)/mg;
99 12 $running_jails = scalar( keys(%running) );
100 12 push( @problems, map { 'jail ' . $_ . ' is not running' } grep { !$running{$_} } sort( keys(%wanted) ) );
101 }
102  
103 21 my $running_vms = 0;
104 21 ▶ if ( defined( $self->opt('vm') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("vm")) 5 16 21
105 5 ▶ return $self->finish( $self->UNKNOWN, 'vm-bhyve is not there, ' . $VM ) if !-x $VM;
Branch: 1 branch
BranchTrueFalseTotal
unless -x $Monitoring::Sneck::Checks::jails::VM 0 5 5
106 5 my ( $exit, $output ) = $self->run_command( $VM, 'list' );
107 3 ▶ return $self->finish( $self->UNKNOWN, 'vm list exited ' . $exit ) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 3 3
108  
109 # NAME DATASTORE LOADER CPU MEMORY VNC AUTO STATE, STATE being Running (PID) or Stopped
110 3 my %states;
111 3 foreach my $line ( split( /\n/, $output ) ) {
112 9 my ($name) = $line =~ /\A(\S+)/;
113 9 ▶ next if !defined($name) || $name eq 'NAME';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or $name eq "NAME" 3 6 9
Condition66%2/3
not defined $name or $name eq "NAME"
l!l&&r!l&&!r
036
MC/DC50%1/2
not defined $name or $name eq "NAME"
not defined $name $name eq "NAME"
Truth table66%2/3
not(A) or B
A= not defined $nameB= $name eq "NAME"
ABresult
1X1
011
000
114 6 ▶ $states{$name} = $line =~ /\bRunning\b/ ? 'running' : $line =~ /\bLocked\b/ ? 'locked' : 'stopped';
Branch: 2 branches
BranchTrueFalseTotal
$line =~ /\bRunning\b/u ? : 3 3 6
$line =~ /\bLocked\b/u ? : 0 3 3
115 }
116 3 $running_vms = grep { $_ eq 'running' } values(%states);
117 3 foreach my $vm ( @{ $self->opt('vm') } ) {
118 7 ▶ next if ( $states{$vm} // '' ) eq 'running';
Branch: 1 branch
BranchTrueFalseTotal
if ($states{$vm} // "") eq "running" 3 4 7
Condition100%2/2
$states{$vm} // ""
l!l
52
MC/DC100%1/1
$states{$vm} // ""
$states{$vm}
Truth table100%2/2
A
A= $states{$vm}
Aresult
11
00
119 4 ▶ push( @problems, 'VM ' . $vm . ' is ' . ( $states{$vm} // 'not there' ) );
Condition100%2/2
$states{$vm} // "not there"
l!l
22
MC/DC100%1/1
$states{$vm} // "not there"
$states{$vm}
Truth table100%2/2
A
A= $states{$vm}
Aresult
11
00
120 }
121 } ## end if ( defined( $self->opt('vm') ) )
122  
123 19 my @parts;
124 19 ▶ push( @parts, $running_jails . ' jails running' ) if %wanted;
Branch: 1 branch
BranchTrueFalseTotal
if %wanted 12 7 19
125 19 ▶ push( @parts, $running_vms . ' VMs running' ) if defined( $self->opt('vm') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("vm") 3 16 19
126 19 push( @parts, @problems, @unknown );
127 19 ▶ my @codes = ( defined( $problems[0] ) ? $self->CRITICAL : $self->OK );
Branch: 1 branch
BranchTrueFalseTotal
defined $problems[0] ? : 6 13 19
128 19 ▶ push( @codes, $self->UNKNOWN ) if defined( $unknown[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown[0] 5 14 19
129  
130 19 return $self->finish( $self->worst(@codes), join( ', ', @parts ) );
131 } ## end sub check
132  
133 =head2 configured
134  
135 Returns the names of the jails defined in /etc/jail.conf and
136 /etc/jail.conf.d/*.conf.
137  
138 =cut
139  
140 21 ▶sub configured {
Subroutine configured: called CC=10 SCAR=24.1
Pod documented
141 21 my ($self) = @_;
142  
143 21 my %names;
144 21 foreach my $file ( $JAIL_CONF, glob( $JAIL_CONF_D . '/*.conf' ) ) {
145 38 ▶ my $content = read_file( $file, err_mode => 'quiet' ) // next;
Condition50%1/2
read_file($file, "err_mode", "quiet") // (next)
l!l
380
MC/DC0%0/1
read_file($file, "err_mode", "quiet") // (next)
read_file($file, "err_mode", "quiet")
Truth table50%1/2
A
A= read_file($file, "err_mode", "quiet")
Aresult
11
00
146 38 $content =~ s/#[^\n]*//g;
147 38 $content =~ s{/\*.*?\*/}{}gs;
148  
149 # "name {" at the top level, as parameters for all jails are set outside of any
150 38 my $depth = 0;
151 # one token at a time, a name before a {, a brace, a quoted string, which may have braces in it, or anything else
152 38 ▶ while ( $content
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\G(?:([A-Za-z0-9_.*-]+)\s*\{|(\{)|(\})|"(?:[^"\\]|\\.)*"|[A-Za-z0-9_.*-]+|[^{}A-Za-z0-9_.*"-]+)/cgsu) 650 38 688
153 =~ /\G(?:([A-Za-z0-9_.*-]+)\s*\{|(\{)|(\})|"(?:[^"\\]|\\.)*"|[A-Za-z0-9_.*-]+|[^{}A-Za-z0-9_.*"-]+)/gcs
154 )
155 {
156 650 ▶ if ( defined($1) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $1) { } 51 599 650
elsif (defined $2) { } 0 599 599
elsif (defined $3) { } 51 548 599
157 51 ▶ $names{$1} = 1 if $depth == 0 && $1 ne '*';
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 0 and $1 ne "*" 51 0 51
Condition33%1/3
$depth == 0 and $1 ne "*"
!ll&&!rl&&r
0051
MC/DC0%0/2
$depth == 0 and $1 ne "*"
$depth == 0 $1 ne "*"
Truth table33%1/3
A and B
A= $depth == 0B= $1 ne "*"
ABresult
0X0
100
111
158 51 $depth++;
159 } elsif ( defined($2) ) {
160 0 $depth++;
161 } elsif ( defined($3) ) {
162 51 ▶ $depth-- if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 51 0 51
163 }
164 } ## end while ( $content =~ ...)
165 } ## end foreach my $file ( $JAIL_CONF, glob( $JAIL_CONF_D...))
166  
167 21 return sort( keys(%names) );
168 } ## end sub configured
169  
170 12 1;
« blib/lib/Monitoring/Sneck/Checks/smtp.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/cciss.pm »