Monitoring/Sneck/Checks/supervisorctl.pm

filter: Statementstmt 97.5% 80 / 82 Branchbran 80.4% 37 / 46 Conditioncond 100% 18 / 18 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 93.4% 157 / 168 CC 32 SCAR 32.7
CC
32
Cov
92%
check
32.6
BEGIN
1.0
BEGIN
1.0
CRAP 26.3
↑ Summary Check/supervisorctl.pm »
1 package Monitoring::Sneck::Checks::supervisorctl;
2  
3 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Check::supervisorctl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::supervisorctl - checks supervisorctl status, and optionally the configs, through Check::supervisorctl
12  
13 =head1 VERSION
14  
15 Version 0.2.0
16  
17 =cut
18  
19 1 our $VERSION = '0.2.0';
20  
21 =head1 SYNOPSIS
22  
23 supervisor|perl:supervisorctl
24 supervisor|perl:supervisorctl -c
25 supervisor|perl:supervisorctl -c -f /etc/supervisor/conf.d -i foo -d bar -s stopped=1
26 supervisor|perl:supervisorctl -c -R -l
27  
28 =head1 DESCRIPTION
29  
30 A L<Monitoring::Sneck::Checks> check, for running L<Check::supervisorctl>
31 in process from sneck, or through sneck_check. It is only of use if
32 L<Monitoring::Sneck::Checks> is installed, which this distribution
33 does not require.
34  
35 It takes the same options as check_supervisorctl and works out the
36 status the same way, see L<Check::supervisorctl>. 'supervisorctl status'
37 is ran through run_command, so it is stopped if the check times out,
38 as are the commands for -R, -l, and -L.
39 If it can not be ran, or returns anything that is not a item and its
40 status, such as when it can not talk to supervisord, that is UNKNOWN
41 by default, changeable via -s status_error=$value.
42  
43 The status line has the counts of the items in each status, followed by
44 the same lines check_supervisorctl prints. Perfdata is the count for
45 each status, the count of ignored items, and if -c is given, the counts
46 of configs not running and running items missing a config. With -R,
47 the counts of items restarted and failed to restart are added, and with
48 -l or -L, the counts of configs added and failed to add.
49  
50 =head1 OPTIONS
51  
52 - -c :: Check the configs as well.
53  
54 - -f <config_dir> :: The directory to look for configs under if -c is set.
55 Default :: /usr/local/etc/supervisor/conf.d
56 Default Linux :: /etc/supervisor/conf.d
57  
58 - -d <ignore_config> :: A config to ignore. May be given more than once.
59  
60 - -i <ignore> :: A item from status to ignore. May be given more than once.
61  
62 - -s <status=value> :: Maps a status to a exit value. May be given more
63 than once. See check_supervisorctl for what may be mapped.
64  
65 - -R :: Restart items that are stopped, exited, fatal, or backoff. See
66 check_supervisorctl.
67  
68 - -l :: Add configs that are not running, the same as -L, for use with -R.
69 Requires -R and -c.
70  
71 - -L :: Add configs that are not running via reread and add. Requires -c.
72  
73 - -p <supervisorctl> :: The supervisorctl to run.
74 Default :: supervisorctl
75  
76 =cut
77  
78 20 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
79 20 return ( 'c', 'f=s', 'd=s@', 'i=s@', 's=s@', 'p=s', 'R', 'l', 'L' );
80 }
81  
82 20 ▶sub defaults { return { p => 'supervisorctl' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
83  
84 =head1 METHODS
85  
86 =head2 check
87  
88 Does the check.
89  
90 =cut
91  
92 19 ▶sub check {
Subroutine check: called CC=32 SCAR=34.9
Pod documented
93 19 my ($self) = @_;
94  
95 # all of these die on bad options, which is UNKNOWN
96 19 ▶ if ( $self->opt('l') && !$self->opt('R') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("l") and not $self->opt("R")) 1 18 19
Condition100%3/3
$self->opt("l") and not $self->opt("R")
!ll&&!rl&&r
1621
MC/DC100%2/2
$self->opt("l") and not $self->opt("R")
$self->opt("l") not $self->opt("R")
Truth table100%3/3
A and not(B)
A= $self->opt("l")B= not $self->opt("R")
ABresult
0X0
100
111
97 1 die("-l requires -R\n");
98 }
99 18 ▶ if ( ( $self->opt('l') || $self->opt('L') ) && !$self->opt('c') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("l") || $self->opt("L") and not $self->opt("c")) 2 16 18
Condition100%6/6
$self->opt("l") || $self->opt("L") and not $self->opt("c")
!ll&&!rl&&r
1422
$self->opt("l") or $self->opt("L")
l!l&&r!l&&!r
2214
MC/DC100%3/3
$self->opt("l") || $self->opt("L") and not $self->opt("c")
$self->opt("l") $self->opt("L") not $self->opt("c")
Truth table100%5/5
A or B and not(C)
A= $self->opt("l")B= $self->opt("L")C= not $self->opt("c")
ABCresult
00X0
1X00
0100
1X11
0111
100 2 die("-l and -L require -c\n");
101 }
102 16 ▶ my $add_configs = $self->opt('l') || $self->opt('L') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('l') || $self->opt('L') ? : 2 14 16
Condition100%3/3
$self->opt("l") or $self->opt("L")
l!l&&r!l&&!r
1114
MC/DC100%2/2
$self->opt("l") or $self->opt("L")
$self->opt("l") $self->opt("L")
Truth table100%3/3
A or B
A= $self->opt("l")B= $self->opt("L")
ABresult
1X1
011
000
103 16 ▶ my %mapping_opts = Check::supervisorctl->parse_mappings( @{ $self->opt('s') // [] } );
Condition100%2/2
$self->opt("s") // []
l!l
313
MC/DC100%1/1
$self->opt("s") // []
$self->opt("s")
Truth table100%2/2
A
A= $self->opt("s")
Aresult
11
00
104 15 ▶ my $checker = Check::supervisorctl->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->opt('c') ? : 5 10 15
$self->opt('R') ? : 2 13 15
Condition100%4/4
$self->opt("d") // []
l!l
114
$self->opt("i") // []
l!l
213
MC/DC100%1/1
$self->opt("d") // []
$self->opt("d")
MC/DC100%1/1
$self->opt("i") // []
$self->opt("i")
Truth table100%2/2
A
A= $self->opt("d")
Aresult
11
00
Truth table100%2/2
A
A= $self->opt("i")
Aresult
11
00
105 config_check => $self->opt('c') ? 1 : 0,
106 config_dir => $self->opt('f'),
107 config_ignore => $self->opt('d') // [],
108 ignore => $self->opt('i') // [],
109 restart => $self->opt('R') ? 1 : 0,
110 add_configs => $add_configs,
111 %mapping_opts,
112 );
113  
114 # the exit is not of use, as it is non-zero if anything is not running, which the output says anyways
115 14 my ( $exit, $output ) = $self->run_command( $self->opt('p'), 'status' );
116 # restarts and adds go through run_command as well, so they are stopped if the check times out
117 my $results = $checker->run(
118 status_output => $output,
119 8 ▶ command => sub { return $self->run_command( $self->opt('p'), @_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
120 13 );
121  
122 13 my @statuses = ( 'running', 'starting', 'backoff', 'stopping', 'stopped', 'exited', 'fatal', 'unknown' );
123 13 my @summary;
124 13 foreach my $status (@statuses) {
125 104 my $count = scalar( @{ $results->{status_list}{$status} } );
126 104 $self->perfdata( label => $status, value => $count, min => 0 );
127 104 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 26 78 104
128 26 push( @summary, $count . ' ' . $status );
129 }
130 }
131 13 $self->perfdata( label => 'ignored', value => scalar( @{ $results->{ignored} } ), min => 0 );
132 13 ▶ if ( $results->{config_check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($results->{"config_check"}) 5 8 13
133 5 my $not_running = scalar( @{ $results->{config_not_running} } );
134 5 my $missing = scalar( @{ $results->{config_missing} } );
135 5 $self->perfdata( label => 'config_not_running', value => $not_running, min => 0 );
136 5 $self->perfdata( label => 'config_missing', value => $missing, min => 0 );
137 5 ▶ if ($not_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_running) 3 2 5
138 3 ▶ push( @summary, $not_running . ' config' . ( $not_running == 1 ? '' : 's' ) . ' not running' );
Branch: 1 branch
BranchTrueFalseTotal
$not_running == 1 ? : 3 0 3
139 }
140 5 ▶ if ($missing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($missing) 2 3 5
141 2 ▶ push( @summary, $missing . ' missing config' . ( $missing == 1 ? '' : 's' ) );
Branch: 1 branch
BranchTrueFalseTotal
$missing == 1 ? : 1 1 2
142 }
143 5 ▶ if ( $results->{config_dir_missing} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($results->{'config_dir_missing'}) { } 1 4 5
elsif (not $results->{'config_dir_readable'}) { } 0 4 4
144 1 push( @summary, 'config dir missing' );
145 } elsif ( !$results->{config_dir_readable} ) {
146 0 push( @summary, 'config dir not readable' );
147 }
148 } ## end if ( $results->{config_check} )
149 13 ▶ if ( $results->{restart} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($results->{"restart"}) 2 11 13
150 2 my $restarted = scalar( @{ $results->{restarted} } );
151 2 my $restart_failed = scalar( @{ $results->{restart_failed} } );
152 2 $self->perfdata( label => 'restarted', value => $restarted, min => 0 );
153 2 $self->perfdata( label => 'restart_failed', value => $restart_failed, min => 0 );
154 2 ▶ if ($restarted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restarted) 2 0 2
155 2 push( @summary, $restarted . ' restarted' );
156 }
157 2 ▶ if ($restart_failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restart_failed) 2 0 2
158 2 push( @summary, $restart_failed . ' failed to restart' );
159 }
160 } ## end if ( $results->{restart} )
161 13 ▶ if ( $results->{add_configs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($results->{"add_configs"}) 2 11 13
162 2 my $added = scalar( @{ $results->{added} } );
163 2 my $add_failed = scalar( @{ $results->{add_failed} } );
164 2 $self->perfdata( label => 'added', value => $added, min => 0 );
165 2 $self->perfdata( label => 'add_failed', value => $add_failed, min => 0 );
166 2 ▶ if ($added) {
Branch: 1 branch
BranchTrueFalseTotal
if ($added) 2 0 2
167 2 ▶ push( @summary, $added . ' config' . ( $added == 1 ? '' : 's' ) . ' added' );
Branch: 1 branch
BranchTrueFalseTotal
$added == 1 ? : 2 0 2
168 }
169 2 ▶ if ($add_failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_failed) 0 2 2
170 0 ▶ push( @summary, $add_failed . ' config' . ( $add_failed == 1 ? '' : 's' ) . ' failed to add' );
Branch: 1 branch
BranchTrueFalseTotal
$add_failed == 1 ? : 0 0 0
171 }
172 } ## end if ( $results->{add_configs} )
173 13 ▶ if ( defined( $results->{status_errors}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $results->{"status_errors"}[0]) 1 12 13
174 1 push( @summary, $results->{status_errors}[0] );
175 }
176 13 ▶ if ( !defined( $summary[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $summary[0]) 2 11 13
177 2 push( @summary, 'no items' );
178 }
179  
180 13 return $self->finish( $results->{exit}, join( ', ', @summary ), @{ $results->{results} } );
181 } ## end sub check
182  
183 =head1 AUTHOR
184  
185 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
186  
187 =head1 LICENSE AND COPYRIGHT
188  
189 This software is Copyright (c) 2026 by Zane C. Bowers-Hadley.
190  
191 This is free software, licensed under:
192  
193 The GNU General Public License, Version 3, June 2007
194  
195 =cut
196  
197 1 1;
↑ Summary Check/supervisorctl.pm »