| 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
Condition100%6/6
defined $options->{"jail"} or $options->{"all-configured"} or defined $options->{"vm"}
defined $options->{"jail"} or $options->{"all-configured"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 24 | my @problems; | |||||||||||||||||||||||||||||||||||||||||
| 85 | my @unknown; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 24 | ▶ | my %wanted = map { $_ => 1 } @{ $self->opt('jail') // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("jail") // []
MC/DC100%1/1
$self->opt("jail") // []
$self->opt("jail")
Truth table100%2/2
A
A= $self->opt("jail")
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 24 | ▶ | if ( $self->opt('all-configured') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 15 | $wanted{$_} = 1 foreach @configured; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 15 | ▶ | delete( @wanted{ @{ $self->opt('exclude') // [] } } ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("exclude") // []
MC/DC100%1/1
$self->opt("exclude") // []
$self->opt("exclude")
Truth table100%2/2
A
A= $self->opt("exclude")
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 15 | ▶ | push( @unknown, 'every configured jail is excluded' ) if defined( $configured[0] ) && !%wanted; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $configured[0] and not %wanted
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
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 24 | my $running_jails = 0; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 24 | ▶ | if (%wanted) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 5 | ▶ | return $self->finish( $self->UNKNOWN, 'vm-bhyve is not there, ' . $VM ) if !-x $VM; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $name or $name eq "NAME"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 6 | ▶ | $states{$name} = $line =~ /\bRunning\b/ ? 'running' : $line =~ /\bLocked\b/ ? 'locked' : 'stopped'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$states{$vm} // ""
MC/DC100%1/1
$states{$vm} // ""
$states{$vm}
Truth table100%2/2
A
A= $states{$vm}
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | ▶ | push( @problems, 'VM ' . $vm . ' is ' . ( $states{$vm} // 'not there' ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$states{$vm} // "not there"
MC/DC100%1/1
$states{$vm} // "not there"
$states{$vm}
Truth table100%2/2
A
A= $states{$vm}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 19 | ▶ | push( @parts, $running_vms . ' VMs running' ) if defined( $self->opt('vm') ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 19 | push( @parts, @problems, @unknown ); | |||||||||||||||||||||||||||||||||||||||||
| 127 | 19 | ▶ | my @codes = ( defined( $problems[0] ) ? $self->CRITICAL : $self->OK ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 19 | ▶ | push( @codes, $self->UNKNOWN ) if defined( $unknown[0] ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 51 | ▶ | $names{$1} = 1 if $depth == 0 && $1 ne '*'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$depth == 0 and $1 ne "*"
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 "*"
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 51 | $depth++; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } elsif ( defined($2) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | $depth++; | |||||||||||||||||||||||||||||||||||||||||
| 161 | } elsif ( defined($3) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 51 | ▶ | $depth-- if $depth; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||