Check/supervisorctl.pm

filter: Statementstmt 90.0% 227 / 252 Branchbran 79.4% 81 / 102 Conditioncond 74.0% 20 / 27 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 85.7% 354 / 413 CC 72 SCAR 32.2
CC
72
Cov
86%
new
34.4
run
33.3
parse_mappings
8.0
CRAP 25.1
« Monitoring/Sneck/Checks/supervisorctl.pm ↑ Summary
1 package Check::supervisorctl;
2  
3 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use File::Slurp qw(read_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Check::supervisorctl - Check the status of supervisorctl to see if it is okay.
12  
13 =head1 VERSION
14  
15 Version 0.2.1
16  
17 =cut
18  
19 6 our $VERSION = '0.2.1';
20  
21 # the statuses supervisorctl knows of
22 6 my @STATUSES = ( 'stopped', 'starting', 'running', 'backoff', 'stopping', 'exited', 'fatal', 'unknown' );
23  
24 # the values that are not a status mapping, without the _val, that parse_mappings knows of
25 6 my @OTHER_VALS = ( 'not_running', 'config_missing', 'config_dir_missing', 'config_dir_nonreadable', 'status_error' );
26  
27 # the statuses that restart acts on and the supervisorctl action for each, backoff needing
28 # restart as start considers it already started
29 6 my %RESTART_ACTIONS = (
30 stopped => 'start',
31 exited => 'start',
32 fatal => 'start',
33 backoff => 'restart',
34 );
35  
36 =head1 SYNOPSIS
37  
38 use Check::supervisorctl;
39  
40 my $check_supervisorctl = Check::supervisorctl->new();
41 my $results = $check_supervisorctl->run;
42 print join( "\n", @{ $results->{results} } ) . "\n";
43 exit( $results->{exit} );
44  
45 =head1 SUBROUTINES/METHODS
46  
47 =head2 new
48  
49 Initiates the object.
50  
51 - status_mapping :: A hash of status mapping values.
52 default :: {
53 stopped => 2,
54 starting => 0,
55 running => 0,
56 backoff => 2,
57 stopping => 2,
58 exited => 2,
59 fatal => 2,
60 unknown => 2,
61 }
62  
63 - not_running_val :: Value for if it a config is not running.
64 default :: 2
65  
66 - config_missing_val :: Value for if it a config is missing for a running item.
67 default :: 2
68  
69 - config_dir_missing_val :: Value for if it the config dir is not present.
70 default :: 3
71  
72 - config_dir_nonreadable_val :: Value for if it the config dir is not readable.
73 default :: 3
74  
75 - status_error_val :: Value for if supervisorctl status could not be run or
76 returned a line that is not a item and status, such as when it can not
77 talk to supervisord.
78 default :: 3
79  
80 - config_check :: Boolean for if it should check the configs or not.
81 default :: 0
82  
83 - ignore :: A array of running items to ignore.
84 default :: []
85  
86 - config_ignore :: A array of configs to ignore.
87 default :: []
88  
89 - config_dir :: Config dir path.
90 default :: /usr/local/etc/supervisor/conf.d
91 default Linux :: /etc/supervisor/conf.d
92  
93 - restart :: Boolean for if items that are stopped, exited, fatal, or
94 backoff should be restarted. Ignored items are not touched.
95 default :: 0
96  
97 - add_configs :: Boolean for if configs that are not running should be
98 added via 'supervisorctl reread' and 'supervisorctl add'. Requires
99 config_check. Ignored configs and ignored items are not touched.
100 default :: 0
101  
102 Restarting or adding does not change the exit value. It is worked out from
103 how things were before, so a stopped item that was restarted is still
104 alerted on, letting what is monitoring it know it was restarted.
105  
106 =cut
107  
108 87 ▶sub new {
Subroutine new: called CC=27 SCAR=35.4
Pod documented
109 87 my ( $blank, %opts ) = @_;
110  
111 87 my $self = {
112 status_mapping => {
113 stopped => 2,
114 starting => 0,
115 running => 0,
116 backoff => 2,
117 stopping => 2,
118 exited => 2,
119 fatal => 2,
120 unknown => 2,
121 },
122 val_to_string => {
123 0 => 'OK',
124 1 => 'WARNING',
125 2 => 'ALERT',
126 3 => 'UNKNOWN'
127 },
128 not_running_val => 2,
129 config_missing_val => 2,
130 config_dir_missing_val => 3,
131 config_dir_nonreadable_val => 3,
132 status_error_val => 3,
133 config_check => 0,
134 restart => 0,
135 add_configs => 0,
136 ignore => {},
137 config_ignore => {},
138 config_dir => '/usr/local/etc/supervisor/conf.d',
139 };
140 87 bless $self;
141  
142 87 ▶ if ( $^O eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "linux") 87 0 87
143 87 $self->{config_dir} = '/etc/supervisor/conf.d';
144 }
145  
146 # read in ignore settings
147 87 ▶ if ( defined( $opts{ignore} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ignore"}) 43 44 87
148 43 ▶ if ( ref( $opts{ignore} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"ignore"} ne "ARRAY") 0 43 43
149 0 die( '$opts{ignore} not a ref type of ARRAY but "' . ref( $opts{ignore} ) . '"' );
150 }
151 43 foreach my $to_ignore ( @{ $opts{ignore} } ) {
152 31 ▶ if ( ref($to_ignore) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $to_ignore ne "") 0 31 31
153 0 die( 'array $opts{ignore} contains a item that is not of ref type "" but ' . ref($to_ignore) );
154 }
155 31 $self->{ignore}{$to_ignore} = 1;
156 }
157 } ## end if ( defined( $opts{ignore} ) )
158  
159 # read in config ignore settings
160 87 ▶ if ( defined( $opts{config_ignore} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"config_ignore"}) 35 52 87
161 35 ▶ if ( ref( $opts{config_ignore} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"config_ignore"} ne "ARRAY") 0 35 35
162 0 die( '$opts{config_ignore} not a ref type of ARRAY but "' . ref( $opts{config_ignore} ) . '"' );
163 }
164 35 foreach my $to_ignore ( @{ $opts{config_ignore} } ) {
165 33 ▶ if ( ref($to_ignore) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $to_ignore ne "") 0 33 33
166 0 die( 'array $opts{config_ignore} contains a item that is not of ref type "" but ' . ref($to_ignore) );
167 }
168 33 $self->{config_ignore}{$to_ignore} = 1;
169 }
170 } ## end if ( defined( $opts{config_ignore} ) )
171  
172 # read in other status settings
173 87 foreach my $to_read_in ( map { $_ . '_val' } @OTHER_VALS ) {
174 435 ▶ if ( defined( $opts{$to_read_in} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$to_read_in}) 9 426 435
175 9 ▶ if ( ref( $opts{$to_read_in} ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{$to_read_in} ne "") 0 9 9
176 0 die( '$opts{' . $to_read_in . '} not a ref type of "" not "' . ref( $opts{$to_read_in} ) . '"' );
177 }
178 9 ▶ if ( $opts{$to_read_in} !~ /^[0123]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{$to_read_in} =~ /^[0123]$/) 1 8 9
179 1 die( '$opts{' . $to_read_in . '} is not 0, 1, 2, or 3, but "' . $opts{$to_read_in} . '"' );
180 }
181 8 $self->{$to_read_in} = $opts{$to_read_in};
182 }
183 } ## end foreach my $to_read_in ( map { $_ . '_val' } @OTHER_VALS)
184  
185 # read in any specified status mappings and
186 86 ▶ if ( defined( $opts{status_mapping} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"status_mapping"}) 23 63 86
187 23 ▶ if ( ref( $opts{status_mapping} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"status_mapping"} ne "HASH") 0 23 23
188 0 die( '$opts{status_mapping} not a ref type of HASH but "' . ref( $opts{status_mapping} ) . '"' );
189 }
190 23 foreach my $status ( keys( %{ $opts{status_mapping} } ) ) {
191 10 my $lc_status = lc($status);
192 10 ▶ if ( ref( $opts{status_mapping}{$status} ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"status_mapping"}{$status} ne "") 0 10 10
193 die( '$opts{status_mapping}{'
194 . $status
195 . '} is not of ref type "" but "'
196 0 . ref( $opts{status_mapping}{$status} )
197 . '"' );
198 }
199 10 ▶ if ( !defined( $self->{status_mapping}{$lc_status} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"status_mapping"}{$lc_status}) 0 10 10
200 0 die( "'" . $status . "' is not a known status type... expected " . join( ', ', @STATUSES ) );
201 }
202 10 ▶ if ( $opts{status_mapping}{$status} !~ /^[0123]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"status_mapping"}{$status} =~ /^[0123]$/) 1 9 10
203 die( '$opts{status_mapping}{'
204 . $status
205 . '} is not 0, 1, 2, or 3, but "'
206 1 . $opts{status_mapping}{$status}
207 . '"' );
208 }
209 9 $self->{status_mapping}{$lc_status} = $opts{status_mapping}{$status};
210 } ## end foreach my $status ( keys( %{ $opts{status_mapping...}}))
211 } ## end if ( defined( $opts{status_mapping} ) )
212  
213 85 foreach my $boolean ( 'config_check', 'restart', 'add_configs' ) {
214 255 ▶ if ( defined( $opts{$boolean} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$boolean}) 97 158 255
215 97 ▶ if ( ref( $opts{$boolean} ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{$boolean} ne "") 0 97 97
216 0 die( '$opts{' . $boolean . '} is not of ref type "" but "' . ref( $opts{$boolean} ) . '"' );
217 }
218 97 $self->{$boolean} = $opts{$boolean};
219 }
220 }
221  
222 85 ▶ if ( $self->{add_configs} && !$self->{config_check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"add_configs"} and not $self->{"config_check"}) 4 81 85
Condition100%3/3
$self->{"add_configs"} and not $self->{"config_check"}
!ll&&!rl&&r
67144
MC/DC100%2/2
$self->{"add_configs"} and not $self->{"config_check"}
$self->{"add_configs"} not $self->{"config_check"}
Truth table100%3/3
A and not(B)
A= $self->{"add_configs"}B= not $self->{"config_check"}
ABresult
0X0
100
111
223 4 die('$opts{add_configs} requires $opts{config_check}');
224 }
225  
226 81 ▶ if ( defined( $opts{config_dir} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"config_dir"}) 29 52 81
227 29 ▶ if ( ref( $opts{config_dir} ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"config_dir"} ne "") 0 29 29
228 0 die( '$opts{config_dir} is not of ref type "" but "' . ref( $opts{config_dir} ) . '"' );
229 }
230 29 $self->{config_dir} = $opts{config_dir};
231 }
232  
233 81 return $self;
234 } ## end sub new
235  
236 =head2 parse_mappings
237  
238 Parses a list of status=value strings, as taken by -s for check_supervisorctl,
239 into a hash of options for L</new>. Whitespace is removed.
240  
241 The names may be any of the statuses for status_mapping, or not_running,
242 config_missing, config_dir_missing, config_dir_nonreadable, or status_error
243 for the matching *_val options.
244  
245 Dies if one is not in the form of name=value or the name is not known. The values
246 are checked by L</new>.
247  
248 my %mapping_opts = Check::supervisorctl->parse_mappings( 'stopped=1', 'config_missing=1' );
249 my $checker = Check::supervisorctl->new( config_check => 1, %mapping_opts );
250  
251 =cut
252  
253 19 ▶sub parse_mappings {
Subroutine parse_mappings: called CC=8 SCAR=20.8
Pod documented
254 19 my ( $blank, @to_parse ) = @_;
255  
256 19 my %other_vals = map { $_ => 1 } @OTHER_VALS;
257 19 my %statuses = map { $_ => 1 } @STATUSES;
258  
259 19 my %status_mapping;
260 my %to_return;
261 19 foreach my $mapping (@to_parse) {
262 8 my $cleaned = $mapping;
263 8 $cleaned =~ s/\s+//g;
264 8 my ( $name, $value ) = split( /\=/, $cleaned, 2 );
265 8 ▶ if ( !defined($name) || !defined($value) || $name eq '' || $value eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $name && defined $value or $name eq "" or $value eq "") 1 7 8
Condition66%6/9
not defined $name && defined $value or $name eq "" or $value eq ""
l!l&&r!l&&!r
107
not defined $name && defined $value or $name eq ""
l!l&&r!l&&!r
107
defined $name && defined $value
!ll&&!rl&&r
017
MC/DC25%1/4
not defined $name && defined $value or $name eq "" or $value eq ""
defined $name defined $value $name eq "" $value eq ""
Truth table40%2/5
not(A && B) or C or D
A= defined $nameB= defined $valueC= $name eq ""D= $value eq ""
ABCDresult
0XXX1
10XX1
111X1
11011
11000
266 1 die( 'status mapping "' . $mapping . '" is not in the form of name=value' );
267 }
268 7 $name = lc($name);
269 7 ▶ if ( $other_vals{$name} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($other_vals{$name}) { } 2 5 7
elsif ($statuses{$name}) { } 3 2 5
270 2 $to_return{ $name . '_val' } = $value;
271 } elsif ( $statuses{$name} ) {
272 3 $status_mapping{$name} = $value;
273 } else {
274 2 die( 'status mapping "'
275 . $mapping
276 . '" is not for a known name... expected '
277 . join( ', ', @STATUSES, @OTHER_VALS ) );
278 }
279 } ## end foreach my $mapping (@to_parse)
280 16 $to_return{status_mapping} = \%status_mapping;
281  
282 16 return %to_return;
283 } ## end sub parse_mappings
284  
285 =head2 run
286  
287 This runs it.
288  
289 my $results=$check_supervisorctl->run;
290  
291 use Data::Dumper;
292 print Dumper($results);
293 exit($results->{exit});
294  
295 It optionally takes the output of 'supervisorctl status', for when it has already
296 been ran by something else. If not given, 'supervisorctl status' is ran.
297  
298 - status_output :: The output of 'supervisorctl status'.
299  
300 - command :: A code ref for running supervisorctl for restart and
301 add_configs. It is passed the args for supervisorctl, such as
302 ( 'start', 'foo' ), and returns its exit code and its stdout and
303 stderr together. If it dies, run dies.
304 default :: runs supervisorctl from the path, without a shell
305  
306 my $results=$check_supervisorctl->run( status_output => $output );
307  
308 my $results = $check_supervisorctl->run(
309 command => sub { return $check->run_command( '/usr/local/bin/supervisorctl', @_ ) },
310 );
311  
312 A supervisorctl command is considered to have failed if it exits non-zero or
313 its output contains 'ERROR', as older versions of supervisorctl exit zero on
314 failure.
315  
316 The returned data is as below.
317  
318 - .configs[] :: A array of configs found.
319  
320 - .config_not_running[] :: A array of configs present but not running.
321  
322 - .config_missing[] :: A array of running items was found, but no matchingly named config was found.
323  
324 - .config_check :: If it was told to check the configs or not for matching names.
325  
326 - .config_dir :: The config dir to check.
327  
328 - .config_ignored[] :: Array of configs ignored.
329  
330 - .config_ignore :: Configs asked to be ignored.
331  
332 - .exit :: Nagios style exit value.
333  
334 - .status.$name :: Status of each item.
335  
336 - .total :: Number of items found in the status output, not counting ignored ones.
337  
338 - .ignored[] :: A array of ignored items.
339  
340 - .ignore :: A array of items asked to be ignored.
341  
342 - .config_dir_missing :: If the config dir is missing.
343  
344 - .config_dir_readable :: If the config dir is readable.
345  
346 - .status_list.$status :: A hash of the various statuses with keys being arrays of items for that status.
347  
348 - .status_errors[] :: Lines from 'supervisorctl status' that were not a item and status,
349 such as when it can not talk to supervisord.
350  
351 - .restart :: If it was told to restart items.
352  
353 - .restarted[] :: A array of items restarted.
354  
355 - .restart_failed[] :: A array of items that restarting failed for.
356  
357 - .add_configs :: If it was told to add configs that are not running.
358  
359 - .added[] :: A array of configs added.
360  
361 - .add_failed[] :: A array of configs that adding failed for, including
362 all of them if 'supervisorctl reread' failed.
363  
364 - .results[] :: A descriptive a array of the results of the check.
365  
366 =cut
367  
368 80 ▶sub run {
Subroutine run: called CC=32 SCAR=35.1
Pod documented
369 80 my ( $self, %opts ) = @_;
370  
371 my $to_return = {
372 configs => [],
373 config_not_running => [],
374 config_missing => [],
375 config_check => $self->{config_check},
376 config_dir => $self->{config_dir},
377 config_ignored => [],
378 80 config_ignore => [ sort( keys( %{ $self->{config_ignore} } ) ) ],
379 exit => 0,
380 status => {},
381 total => 0,
382 ignored => [],
383 80 ignore => [ sort( keys( %{ $self->{ignore} } ) ) ],
384 config_dir_missing => 0,
385 config_dir_readable => 1,
386 640 status_list => { map { $_ => [] } @STATUSES },
387 status_errors => [],
388 restart => $self->{restart},
389 restarted => [],
390 restart_failed => [],
391 add_configs => $self->{add_configs},
392 80 added => [],
393 add_failed => [],
394 results => []
395 };
396  
397 80 my $command = $opts{command};
398 80 ▶ if ( !defined($command) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command) 43 37 80
399 43 $command = \&_run_supervisorctl;
400 }
401  
402 80 my $output = $opts{status_output};
403 80 ▶ if ( !defined($output) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output) 0 80 80
404 0 $output = `supervisorctl status 2> /dev/null`;
405 # -1 is it could not be ran at all and 127 is the shell not finding it
406 0 ▶ if ( $? == -1 || ( $? >> 8 ) == 127 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == -1 or $? >> 8 == 127) 0 0 0
Condition0%0/3
$? == -1 or $? >> 8 == 127
l!l&&r!l&&!r
000
MC/DC0%0/2
$? == -1 or $? >> 8 == 127
$? == -1 $? >> 8 == 127
Truth table0%0/3
A or B
A= $? == -1B= $? >> 8 == 127
ABresult
1X1
011
000
407 0 $self->_status_error( $to_return, 'could not run "supervisorctl status"' );
408 0 $output = '';
409 }
410 }
411  
412 80 foreach my $line ( split( /\n/, $output ) ) {
413 # blank lines are not a problem
414 262 ▶ if ( $line =~ /^\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$/) 0 262 262
415 0 next;
416 }
417 262 my ( $name, $status ) = split( /\s+/, $line );
418 262 ▶ if ( defined($status) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $status) 258 4 262
419 258 $status = lc($status);
420 }
421 # anything not a item followed by a known status is a problem, such as
422 # "unix:///var/run/supervisor.sock no such file" for when supervisord is down
423 262 ▶ if ( !defined($status) || !defined( $self->{status_mapping}{$status} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $status and defined $self->{"status_mapping"}{$status}) 9 253 262
Condition100%3/3
defined $status and defined $self->{"status_mapping"}{$status}
!ll&&!rl&&r
45253
MC/DC100%2/2
defined $status and defined $self->{"status_mapping"}{$status}
defined $status defined $self->{"status_mapping"}{$status}
Truth table100%3/3
A and B
A= defined $statusB= defined $self->{"status_mapping"}{$status}
ABresult
0X0
100
111
424 9 $self->_status_error( $to_return, 'supervisorctl status returned "' . $line . '"' );
425 9 next;
426 }
427 253 ▶ if ( $self->{ignore}{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ignore"}{$name}) 31 222 253
428 31 push( @{ $to_return->{ignored} }, $name );
429 31 push( @{ $to_return->{results} }, 'IGNORED - ' . $name . ', ' . $status );
430 31 next;
431 }
432 222 $self->_raise_exit( $to_return, $self->{status_mapping}{$status} );
433 222 $to_return->{status}{$name} = $status;
434 222 $to_return->{total}++;
435 222 push( @{ $to_return->{status_list}{$status} }, $name );
436 push(
437 222 @{ $to_return->{results} },
438 222 $self->{val_to_string}{ $self->{status_mapping}{$status} } . ' - ' . $name . ', ' . $status
439 );
440 } ## end foreach my $line ( split( /\n/, $output ) )
441  
442 # restart anything not running, ignored items never made it into status
443 80 ▶ if ( $self->{restart} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"restart"}) 17 63 80
444 17 foreach my $name ( sort( keys( %{ $to_return->{status} } ) ) ) {
445 47 my $status = $to_return->{status}{$name};
446 47 my $action = $RESTART_ACTIONS{$status};
447 47 ▶ if ( !defined($action) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $action) 12 35 47
448 12 next;
449 }
450 35 my ( $worked, $action_output ) = _supervisorctl_action( $command, $action, $name );
451 34 ▶ if ($worked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($worked) { } 20 14 34
452 20 push( @{ $to_return->{restarted} }, $name );
453 20 push( @{ $to_return->{results} }, 'RESTARTED - ' . $name . ', ' . $status );
454 } else {
455 14 push( @{ $to_return->{restart_failed} }, $name );
456 14 push( @{ $to_return->{results} },
457 'RESTART FAILED - ' . $name . ', ' . $status . ': ' . $action_output );
458 }
459 } ## end foreach my $name ( sort( keys( %{ $to_return->{...}})))
460 } ## end if ( $self->{restart} )
461  
462 # check the config dir only if asked to
463 79 ▶ if ( $self->{config_check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config_check"}) 29 50 79
464 # handling for if it does not exist
465 29 ▶ if ( -d $self->{config_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{'config_dir'}) { } 24 5 29
466 24 my @dir_entries;
467 24 eval { @dir_entries = read_dir( $self->{config_dir} ); };
468 24 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
469 0 $to_return->{config_dir_readable} = 0;
470 0 $self->_raise_exit( $to_return, $self->{config_dir_nonreadable_val} );
471 push(
472 0 @{ $to_return->{results} },
473 $self->{val_to_string}{ $self->{config_dir_nonreadable_val} }
474 . ' - config dir, "'
475 . $self->{config_dir}
476 0 . '", not readable'
477 );
478 } ## end if ($@)
479 # if it was readable, process it
480 24 ▶ if ( $to_return->{config_dir_readable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_return->{"config_dir_readable"}) 24 0 24
481 # a lookup hash of found configs, including ignored ones
482 24 my %configs;
483 24 my %ignored = map { $_ => 1 } @{ $to_return->{ignored} };
484 # process each dir entry
485 24 foreach my $entry ( sort(@dir_entries) ) {
486 # only process items ending in .conf and that are a file.
487 132 ▶ if ( $entry =~ /\.conf$/ && -f $self->{config_dir} . '/' . $entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\.conf$/ and -f $self->{"config_dir"} . '/' . $entry) 112 20 132
Condition66%2/3
$entry =~ /\.conf$/ and -f $self->{"config_dir"} . '/' . $entry
!ll&&!rl&&r
200112
MC/DC50%1/2
$entry =~ /\.conf$/ and -f $self->{"config_dir"} . '/' . $entry
$entry =~ /\.conf$/ -f $self->{"config_dir"} . '/' . $entry
Truth table66%2/3
A and B
A= $entry =~ /\.conf$/B= -f $self->{"config_dir"} . '/' . $entry
ABresult
0X0
100
111
488 112 $entry =~ s/\.conf$//;
489 112 $configs{$entry} = 1;
490 112 ▶ if ( $self->{config_ignore}{$entry} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'config_ignore'}{$entry}) { } 33 79 112
elsif ($ignored{$entry}) { } 16 63 79
491 33 push( @{ $to_return->{config_ignored} }, $entry );
492 33 push( @{ $to_return->{results} }, 'IGNORED - config ' . $entry );
493 } elsif ( $ignored{$entry} ) {
494 # the item is ignored, so if it is running or not does not matter
495 16 push( @{ $to_return->{configs} }, $entry );
496 } else {
497 63 push( @{ $to_return->{configs} }, $entry );
498 63 ▶ if ( !defined( $to_return->{status}{$entry} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $to_return->{'status'}{$entry}) { } 15 48 63
499 15 push( @{ $to_return->{config_not_running} }, $entry );
500 push(
501 15 @{ $to_return->{results} },
502 $self->{val_to_string}{ $self->{not_running_val} }
503 15 . ' - non-running config "'
504 . $entry . '"'
505 );
506 15 $self->_raise_exit( $to_return, $self->{not_running_val} );
507 } else {
508 48 push( @{ $to_return->{results} }, 'OK - config present for ' . $entry );
509 }
510 } ## end else [ if ( $self->{config_ignore}{$entry} ) ]
511 } ## end if ( $entry =~ /\.conf$/ && -f $self->{config_dir...})
512 } ## end foreach my $entry ( sort(@dir_entries) )
513 24 foreach my $running ( sort( keys( %{ $to_return->{status} } ) ) ) {
514 # only check if it is missing as we already check if a running item exists for a config previously
515 71 ▶ if ( !$configs{$running} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($configs{$running}) 23 48 71
516 23 push( @{ $to_return->{config_missing} }, $running );
517 push(
518 23 @{ $to_return->{results} },
519 23 $self->{val_to_string}{ $self->{config_missing_val} } . ' - missing config ' . $running
520 );
521 23 $self->_raise_exit( $to_return, $self->{config_missing_val} );
522 }
523 } ## end foreach my $running ( sort( keys( %{ $to_return...})))
524 } ## end if ( $to_return->{config_dir_readable} )
525 } else {
526 5 $to_return->{config_dir_missing} = 1;
527 5 $self->_raise_exit( $to_return, $self->{config_dir_missing_val} );
528 push(
529 5 @{ $to_return->{results} },
530 $self->{val_to_string}{ $self->{config_dir_missing_val} }
531 . ' - config dir, "'
532 . $self->{config_dir}
533 5 . '", missing'
534 );
535 } ## end else [ if ( -d $self->{config_dir} ) ]
536 } ## end if ( $self->{config_check} )
537  
538 # add configs that are not running, ignored ones never made it into config_not_running
539 79 ▶ if ( $self->{add_configs} && defined( $to_return->{config_not_running}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"add_configs"} and defined $to_return->{"config_not_running"}[0]) 10 69 79
Condition100%3/3
$self->{"add_configs"} and defined $to_return->{"config_not_running"}[0]
!ll&&!rl&&r
65410
MC/DC100%2/2
$self->{"add_configs"} and defined $to_return->{"config_not_running"}[0]
$self->{"add_configs"} defined $to_return->{"config_not_running"}[0]
Truth table100%3/3
A and B
A= $self->{"add_configs"}B= defined $to_return->{"config_not_running"}[0]
ABresult
0X0
100
111
540 # supervisord has to reread the configs before it knows of new ones
541 10 my ( $reread_worked, $reread_output ) = _supervisorctl_action( $command, 'reread' );
542 10 foreach my $config ( @{ $to_return->{config_not_running} } ) {
543 10 my $worked;
544 my $add_output;
545 10 ▶ if ($reread_worked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reread_worked) { } 6 4 10
546 6 ( $worked, $add_output ) = _supervisorctl_action( $command, 'add', $config );
547 } else {
548 4 $add_output = 'reread failed: ' . $reread_output;
549 }
550 10 ▶ if ($worked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($worked) { } 6 4 10
551 6 push( @{ $to_return->{added} }, $config );
552 6 push( @{ $to_return->{results} }, 'ADDED - config ' . $config );
553 } else {
554 4 push( @{ $to_return->{add_failed} }, $config );
555 4 push( @{ $to_return->{results} }, 'ADD FAILED - config ' . $config . ': ' . $add_output );
556 }
557 } ## end foreach my $config ( @{ $to_return->{config_not_running...}})
558 } ## end if ( $self->{add_configs} && defined( $to_return...))
559  
560 79 return $to_return;
561 } ## end sub run
562  
563 # The default command for run, for running supervisorctl for restart and
564 # add_configs. Runs supervisorctl from the path without a shell, so names
565 # taken from config file names can not do anything funny, with stdout and
566 # stderr together.
567 #
568 # Args...
569 #
570 # - @args :: The args for supervisorctl, such as ( 'start', 'foo' ).
571 #
572 # Returns the exit code and the output. If supervisorctl can not be ran,
573 # the exit is 127 and the output says why. Killed by a signal is 128 plus
574 # the signal. A fork failing is a exit of -1.
575 #
576 # Example...
577 #
578 # my ( $exit, $output ) = _run_supervisorctl( 'start', 'foo' );
579 # # ( 0, "foo: started\n" )
580 9 ▶sub _run_supervisorctl {
Subroutine _run_supervisorctl: called CC=5 SCAR=16.9
581 9 my (@args) = @_;
582  
583 9 my $pid = open( my $output_fh, '-|' );
584 9 ▶ if ( !defined($pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 9 9
585 0 return ( -1, 'can not fork for supervisorctl, ' . $! );
586 }
587 9 ▶ if ( !$pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 3 6 9
588 3 open( STDERR, '>&', \*STDOUT );
589 6 ▶ { no warnings 'exec'; exec( {'supervisorctl'} 'supervisorctl', @args ); }
Subroutine BEGIN: called CC=1 SCAR=0.0
590 # syswrite and _exit, as the buffers and END blocks are the parent's
591 0 syswrite( STDOUT, 'can not run supervisorctl, ' . $! );
592 0 POSIX::_exit(127);
593 }
594  
595 6 my $output = do { local $/; <$output_fh> };
596 6 close($output_fh);
597 6 ▶ if ( !defined($output) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output) 0 6 6
598 0 $output = '';
599 }
600  
601 # killed by a signal is 128 plus it, as the shell does, so it is not mistaken for 0
602 6 ▶ if ( $? & 127 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? & 127) 0 6 6
603 0 return ( 128 + ( $? & 127 ), $output );
604 }
605  
606 6 return ( $? >> 8, $output );
607 } ## end sub _run_supervisorctl
608  
609 # Runs a supervisorctl action through the command code ref given to run and
610 # works out if it worked. Older versions of supervisorctl exit zero even on
611 # failure, so output containing ERROR is a failure as well.
612 #
613 # Args...
614 #
615 # - command :: The code ref to run it via, as taken by run.
616 #
617 # - @args :: The args for supervisorctl, such as ( 'add', 'foo' ).
618 #
619 # Returns a boolean for if it worked and the output with all whitespace,
620 # newlines included, squashed to single spaces and trimmed, for use in a
621 # result line.
622 #
623 # Example...
624 #
625 # my ( $worked, $output ) = _supervisorctl_action( $command, 'start', 'foo' );
626 # # ( 0, 'foo: ERROR (spawn error)' )
627 51 ▶sub _supervisorctl_action {
Subroutine _supervisorctl_action: called CC=3 SCAR=11.1
628 51 my ( $command, @args ) = @_;
629  
630 51 my ( $exit, $output ) = $command->(@args);
631 50 ▶ if ( !defined($output) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output) 0 50 50
632 0 $output = '';
633 }
634 50 $output =~ s/\s+/ /g;
635 50 $output =~ s/^ | $//g;
636  
637 50 ▶ return ( ( $exit == 0 && $output !~ /ERROR/ ), $output );
Condition100%3/3
$exit == 0 && !($output =~ /ERROR/)
!ll&&!rl&&r
10832
MC/DC100%2/2
$exit == 0 && !($output =~ /ERROR/)
$exit == 0 !($output =~ /ERROR/)
Truth table100%3/3
A && not(B)
A= $exit == 0B= !($output =~ /ERROR/)
ABresult
0X0
100
111
638 } ## end sub _supervisorctl_action
639  
640 # raises the exit value in the passed results hash if the new one is higher
641 274 ▶sub _raise_exit {
Subroutine _raise_exit: called CC=2 SCAR=6.9
642 274 my ( $self, $to_return, $value ) = @_;
643  
644 274 ▶ if ( $to_return->{exit} < $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_return->{"exit"} < $value) 63 211 274
645 63 $to_return->{exit} = $value;
646 }
647  
648 274 return;
649 }
650  
651 # records a problem with supervisorctl status in the passed results hash
652 9 ▶sub _status_error {
Subroutine _status_error: called CC=1 SCAR=0.0
653 9 my ( $self, $to_return, $error ) = @_;
654  
655 9 push( @{ $to_return->{status_errors} }, $error );
656 9 push( @{ $to_return->{results} }, $self->{val_to_string}{ $self->{status_error_val} } . ' - ' . $error );
657 9 $self->_raise_exit( $to_return, $self->{status_error_val} );
658  
659 9 return;
660 }
661  
662 =pod
663  
664 =head1 AUTHOR
665  
666 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
667  
668 =head1 BUGS
669  
670 Please report any bugs or feature requests to C<bug-check-supervisorctl at rt.cpan.org>, or through
671 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Check-supervisorctl>. I will be notified, and then you'll
672 automatically be notified of progress on your bug as I make changes.
673  
674  
675  
676  
677 =head1 SUPPORT
678  
679 You can find documentation for this module with the perldoc command.
680  
681 perldoc Check::supervisorctl
682  
683  
684 You can also look for information at:
685  
686 =over 4
687  
688 =item * RT: CPAN's request tracker (report bugs here)
689  
690 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Check-supervisorctl>
691  
692 =item * Github issue tracker (report bugs here)
693  
694 L<https://github.com/VVelox/Check-supervisorctl/issues>
695  
696 =item * CPAN Ratings
697  
698 L<https://cpanratings.perl.org/d/Check-supervisorctl>
699  
700 =item * Search CPAN
701  
702 L<https://metacpan.org/release/Check-supervisorctl>
703  
704 =back
705  
706  
707 =head1 ACKNOWLEDGEMENTS
708  
709  
710 =head1 LICENSE AND COPYRIGHT
711  
712 This software is Copyright (c) 2025 by Zane C. Bowers-Hadley.
713  
714 This is free software, licensed under:
715  
716 The GNU General Public License, Version 3, June 2007
717  
718  
719 =cut
720  
721 6 1; # End of Check::supervisorctl
« Monitoring/Sneck/Checks/supervisorctl.pm ↑ Summary