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

filter: Statementstmt 100% 61 / 61 Branchbran 83.3% 20 / 24 Conditioncond 76.1% 16 / 21 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 88.2% 113 / 128 CC 26 SCAR 27.2
CC
26
Cov
92%
check
21.2
validate
6.2
BEGIN
1.0
CRAP 15.3
« blib/lib/Monitoring/Sneck/Checks/thermal_throttle.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/ldap.pm »
1 package Monitoring::Sneck::Checks::containers;
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  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::containers - checks Docker or Podman containers
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 12 our $VERSION = '0.0.1';
19  
20 # Where the runtimes are. Set by tests.
21 12 our %RUNTIMES = ( docker => '/usr/bin/docker', podman => '/usr/bin/podman' );
22  
23 =head1 SYNOPSIS
24  
25 containers|perl:containers
26 web_containers|perl:containers -n web -n db -x '^buildkit'
27 podman|perl:containers -r podman -n app
28  
29 =head1 DESCRIPTION
30  
31 Lists the containers through docker ps -a, or podman. A container
32 given with --name that is not running, or any running one whose health
33 check says it is unhealthy, is CRITICAL. One stuck restarting is
34 WARNING.
35  
36 This needs access to the runtime, which for Docker means root or the
37 docker group. Not having it, or the runtime not being there, is
38 UNKNOWN.
39  
40 =head1 OPTIONS
41  
42 - -n, --name :: A container that must be running. May be given more
43 than once.
44  
45 - -x, --exclude :: A regex for containers to leave out of the health
46 and restarting checks. May be given more than once.
47  
48 - -r, --runtime :: docker or podman.
49 Default :: docker if it is there, else podman
50  
51 =cut
52  
53 31 ▶sub option_spec { return ( 'name|n=s@', 'exclude|x=s@', 'runtime|r=s' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
54  
55 31 ▶sub validate {
Subroutine validate: called CC=6 SCAR=18.2
Pod documented
56 31 my ( $self, $options ) = @_;
57  
58 31 my @problems;
59 push( @problems, '--runtime must be docker or podman' )
60 31 ▶ if defined( $options->{runtime} ) && !$RUNTIMES{ $options->{runtime} };
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"runtime"} and not $Monitoring::Sneck::Checks::containers::RUNTIMES{$options->{"runtime"}} 0 31 31
Condition66%2/3
defined $options->{"runtime"} and not $Monitoring::Sneck::Checks::containers::RUNTIMES{$options->{"runtime"}}
!ll&&!rl&&r
2470
MC/DC0%0/2
defined $options->{"runtime"} and not $Monitoring::Sneck::Checks::containers::RUNTIMES{$options->{"runtime"}}
defined $options->{"runtime"} not $Monitoring::Sneck::Checks::containers::RUNTIMES{$options->{"runtime"}}
Truth table66%2/3
A and not(B)
A= defined $options->{"runtime"}B= not $Monitoring::Sneck::Checks::containers::RUNTIMES{$options->{"runtime"}}
ABresult
0X0
100
111
61 31 ▶ foreach my $regex ( @{ $options->{exclude} // [] } ) {
Condition100%2/2
$options->{"exclude"} // []
l!l
1714
MC/DC100%1/1
$options->{"exclude"} // []
$options->{"exclude"}
Truth table100%2/2
A
A= $options->{"exclude"}
Aresult
11
00
62 17 ▶ push( @problems, '--exclude "' . $regex . '" is not a valid regex' ) if !eval { qr/$regex/; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { qr/$regex/u; 1 } } 0 17 17
63 }
64  
65 31 return @problems;
66 } ## end sub validate
67  
68 =head1 METHODS
69  
70 =head2 check
71  
72 Does the check.
73  
74 =cut
75  
76 31 ▶sub check {
Subroutine check: called CC=21 SCAR=30.6
Pod documented
77 31 my ($self) = @_;
78  
79 31 ▶ my $runtime = $self->opt('runtime') // ( -x $RUNTIMES{docker} ? 'docker' : 'podman' );
Branch: 1 branch
BranchTrueFalseTotal
-x $Monitoring::Sneck::Checks::containers::RUNTIMES{'docker'} ? : 24 0 24
Condition100%2/2
$self->opt("runtime") // (-x $Monitoring::Sneck::Checks::containers::RUNTIMES{"docker"} ? "docker" : "podman")
l!l
724
MC/DC100%1/1
$self->opt("runtime") // (-x $Monitoring::Sneck::Checks::containers::RUNTIMES{"docker"} ? "docker" : "podman")
$self->opt("runtime")
Truth table100%2/2
A
A= $self->opt("runtime")
Aresult
11
00
80 31 my $command = $RUNTIMES{$runtime};
81 31 ▶ return $self->finish( $self->UNKNOWN, $runtime . ' is not there' ) if !-x $command;
Branch: 1 branch
BranchTrueFalseTotal
unless -x $command 7 24 31
82 24 my ( $exit, $output ) = $self->run_command( $command, 'ps', '-a', '--format', '{{.Names}} {{.State}} {{.Status}}' );
83 21 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 6 15 21
84 6 ▶ return $self->finish( $self->UNKNOWN,
Condition50%1/2
(split(/\n/u, $output, 0))[0] // ""
l!l
60
MC/DC0%0/1
(split(/\n/u, $output, 0))[0] // ""
(split(/\n/u, $output, 0))[0]
Truth table50%1/2
A
A= (split(/\n/u, $output, 0))[0]
Aresult
11
00
85 $runtime . ' ps exited ' . $exit . ', "' . ( ( split( /\n/, $output ) )[0] // '' ) . '"' );
86 }
87  
88 15 my %containers;
89 15 foreach my $line ( split( /\n/, $output ) ) {
90 75 my ( $name, $state, $status ) = split( /\t/, $line );
91 75 ▶ next if !defined($state);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state 0 75 75
92 75 ▶ $containers{$name} = { state => lc($state), status => $status // '' };
Condition50%1/2
$status // ""
l!l
750
MC/DC0%0/1
$status // ""
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
93 }
94  
95 15 ▶ my @excludes = @{ $self->opt('exclude') // [] };
Condition50%1/2
$self->opt("exclude") // []
l!l
150
MC/DC0%0/1
$self->opt("exclude") // []
$self->opt("exclude")
Truth table50%1/2
A
A= $self->opt("exclude")
Aresult
11
00
96 15 my @codes = ( $self->OK );
97 15 my @problems;
98 15 ▶ foreach my $name ( @{ $self->opt('name') // [] } ) {
Condition50%1/2
$self->opt("name") // []
l!l
150
MC/DC0%0/1
$self->opt("name") // []
$self->opt("name")
Truth table50%1/2
A
A= $self->opt("name")
Aresult
11
00
99 31 my $container = $containers{$name};
100 31 ▶ next if defined($container) && $container->{state} eq 'running';
Branch: 1 branch
BranchTrueFalseTotal
if defined $container and $container->{"state"} eq "running" 15 16 31
Condition100%3/3
defined $container and $container->{"state"} eq "running"
!ll&&!rl&&r
8815
MC/DC100%2/2
defined $container and $container->{"state"} eq "running"
defined $container $container->{"state"} eq "running"
Truth table100%3/3
A and B
A= defined $containerB= $container->{"state"} eq "running"
ABresult
0X0
100
111
101 16 push( @codes, $self->CRITICAL );
102 16 ▶ push( @problems, $name . ' is ' . ( defined($container) ? $container->{state} : 'not there' ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $container ? : 8 8 16
103 }
104 15 foreach my $name ( sort( keys(%containers) ) ) {
105 75 ▶ next if grep { $name =~ /$_/ } @excludes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$name =~ /$_/u;} @excludes 29 46 75
106 46 my $container = $containers{$name};
107 46 ▶ if ( $container->{state} eq 'running' && $container->{status} =~ /\(unhealthy\)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($container->{'state'} eq 'running' and $container->{'status'} =~ /\(unhealthy\)/u) { } 8 38 46
elsif ($container->{'state'} eq 'restarting') { } 8 30 38
Condition100%3/3
$container->{"state"} eq "running" and $container->{"status"} =~ /\(unhealthy\)/u
!ll&&!rl&&r
23158
MC/DC100%2/2
$container->{"state"} eq "running" and $container->{"status"} =~ /\(unhealthy\)/u
$container->{"state"} eq "running" $container->{"status"} =~ /\(unhealthy\)/u
Truth table100%3/3
A and B
A= $container->{"state"} eq "running"B= $container->{"status"} =~ /\(unhealthy\)/u
ABresult
0X0
100
111
108 8 push( @codes, $self->CRITICAL );
109 8 push( @problems, $name . ' is unhealthy' );
110 } elsif ( $container->{state} eq 'restarting' ) {
111 8 push( @codes, $self->WARNING );
112 8 push( @problems, $name . ' is restarting' );
113 }
114 } ## end foreach my $name ( sort( keys(%containers) ) )
115  
116 15 my $running = grep { $_->{state} eq 'running' } values(%containers);
117 15 $self->perfdata( label => 'running', value => $running, min => 0 );
118 15 my $message = $running . ' of ' . scalar( keys(%containers) ) . ' containers running';
119 15 ▶ $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 8 7 15
120  
121 15 return $self->finish( $self->worst(@codes), $message );
122 } ## end sub check
123  
124 12 1;
« blib/lib/Monitoring/Sneck/Checks/thermal_throttle.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/ldap.pm »