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

filter: Statementstmt 98.3% 58 / 59 Branchbran 86.8% 33 / 38 Conditioncond 84.6% 11 / 13 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 11 / 11 Podpod 100% 7 / 7 totaltotal 91.9% 125 / 136 CC 26 SCAR 19.5
CC
26
Cov
93%
running
12.1
check
7.0
validate
6.6
CRAP 7.0
« blib/lib/Monitoring/Sneck/Checks/pid_file.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/base.pm »
1 package Monitoring::Sneck::Checks::service;
2  
3 32 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::service - checks services are running
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 32 our $VERSION = '0.0.1';
19  
20 # Where things are. Set by tests.
21 32 ▶our $SYSTEMCTL = -x '/usr/bin/systemctl' ? '/usr/bin/systemctl' : '/bin/systemctl';
Branch: 1 branch
BranchTrueFalseTotal
-x '/usr/bin/systemctl' ? : 0 32 32
22 32 our $SERVICE = '/usr/sbin/service';
23  
24 =head1 SYNOPSIS
25  
26 web|perl:service -s nginx -s php-fpm -e
27 www_jail|perl:service -s nginx -j www
28  
29 =head1 DESCRIPTION
30  
31 Checks each service given is running, and with --enabled, that it is
32 set to start at boot too. Any that is not is CRITICAL.
33  
34 On Linux, systemctl is-active and is-enabled are asked, and on FreeBSD,
35 service status and service enabled, in a jail too with --jail. Other
36 OSes, or Linux without systemd, are UNKNOWN.
37  
38 =head1 OPTIONS
39  
40 - -s, --service :: A service. May be given more than once. Required.
41  
42 - -e, --enabled :: They must also be enabled.
43  
44 - -j, --jail :: On FreeBSD, check the services in this jail. UNKNOWN on
45 other OSes.
46  
47 =cut
48  
49 72 ▶sub option_spec { return ( 'service|s=s@', 'enabled|e', 'jail|j=s' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
50  
51 72 ▶sub required { return ('service') }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
52  
53 72 ▶sub validate {
Subroutine validate: called CC=6 SCAR=18.8
Pod documented
54 72 my ( $self, $options ) = @_;
55  
56 72 my @problems;
57 72 ▶ foreach my $service ( @{ $options->{service} // [] } ) {
Condition50%1/2
$options->{"service"} // []
l!l
720
MC/DC0%0/1
$options->{"service"} // []
$options->{"service"}
Truth table50%1/2
A
A= $options->{"service"}
Aresult
11
00
58 130 ▶ push( @problems, '--service "' . $service . '" is not a service name' ) if $service !~ /\A[A-Za-z0-9_.\@:-]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $service =~ /\A[A-Za-z0-9_.\@:-]+\z/u 0 130 130
59 }
60 push( @problems, '--jail "' . $options->{jail} . '" is not a jail name or ID' )
61 72 ▶ if defined( $options->{jail} ) && $options->{jail} !~ /\A[A-Za-z0-9_.-]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"jail"} and not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u 0 72 72
Condition66%2/3
defined $options->{"jail"} and not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
!ll&&!rl&&r
6390
MC/DC0%0/2
defined $options->{"jail"} and not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
defined $options->{"jail"} not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
Truth table66%2/3
A and not(B)
A= defined $options->{"jail"}B= not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
ABresult
0X0
100
111
62  
63 72 return @problems;
64 } ## end sub validate
65  
66 =head1 METHODS
67  
68 =head2 check
69  
70 Does the check.
71  
72 =cut
73  
74 72 ▶sub check {
Subroutine check: called CC=7 SCAR=19.5
Pod documented
75 72 my ($self) = @_;
76  
77 72 my @problems;
78 my @fine;
79 72 foreach my $service ( @{ $self->opt('service') } ) {
80 122 my ( $running, $state ) = $self->running($service);
81 106 ▶ if ( !$running ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($running) 50 56 106
82 50 push( @problems, $service . ' is ' . $state );
83 50 next;
84 }
85 56 ▶ if ( $self->opt('enabled') && !$self->enabled($service) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("enabled") and not $self->enabled($service)) 9 44 53
Condition100%3/3
$self->opt("enabled") and not $self->enabled($service)
!ll&&!rl&&r
20279
MC/DC100%2/2
$self->opt("enabled") and not $self->enabled($service)
$self->opt("enabled") not $self->enabled($service)
Truth table100%3/3
A and not(B)
A= $self->opt("enabled")B= not $self->enabled($service)
ABresult
0X0
100
111
86 9 push( @problems, $service . ' is running but not enabled' );
87 9 next;
88 }
89 44 push( @fine, $service . ' ' . $state );
90 } ## end foreach my $service ( @{ $self->opt('service') ...})
91 53 ▶ my $where = defined( $self->opt('jail') ) ? ' in jail ' . $self->opt('jail') : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->opt('jail') ? : 4 49 53
92  
93 53 ▶ return $self->finish( $self->CRITICAL, join( ', ', @problems ) . $where ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 25 28 53
94 28 return $self->finish( $self->OK, join( ', ', @fine ) . $where );
95 } ## end sub check
96  
97 =head2 running
98  
99 Returns 1 and its state if a service is running, else 0 and its state,
100 such as inactive or failed. Dies if this OS is not supported.
101  
102 =cut
103  
104 122 ▶sub running {
Subroutine running: called CC=12 SCAR=24.9
Pod documented
105 122 my ( $self, $service ) = @_;
106  
107 122 ▶ if ( $self->os eq 'linux' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux") 95 27 122
108 95 ▶ die("--jail is only for freebsd\n") if defined( $self->opt('jail') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("jail") 4 91 95
109 91 ▶ die("systemctl is not there, so this host does not use systemd\n") if !-x $SYSTEMCTL;
Branch: 1 branch
BranchTrueFalseTotal
unless -x $Monitoring::Sneck::Checks::service::SYSTEMCTL 0 91 91
110 91 my ( $exit, $output ) = $self->run_command( $SYSTEMCTL, 'is-active', $service );
111 85 ▶ my $state = ( split( /\n/, $output ) )[0] // 'unknown';
Condition100%2/2
(split(/\n/u, $output, 0))[0] // "unknown"
l!l
7510
MC/DC100%1/1
(split(/\n/u, $output, 0))[0] // "unknown"
(split(/\n/u, $output, 0))[0]
Truth table100%2/2
A
A= (split(/\n/u, $output, 0))[0]
Aresult
11
00
112  
113 # a unit that is not there is only inactive to is-active
114 85 ▶ if ( $exit && $state eq 'inactive' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit and $state eq "inactive") 26 59 85
Condition100%3/3
$exit and $state eq "inactive"
!ll&&!rl&&r
441526
MC/DC100%2/2
$exit and $state eq "inactive"
$exit $state eq "inactive"
Truth table100%3/3
A and B
A= $exitB= $state eq "inactive"
ABresult
0X0
100
111
115 26 my ( undef, $load ) = $self->run_command( $SYSTEMCTL, 'show', '-p', 'LoadState', '--value', $service );
116 24 ▶ $state = 'not there' if $load =~ /\Anot-found/;
Branch: 1 branch
BranchTrueFalseTotal
if $load =~ /\Anot-found/u 11 13 24
117 }
118 83 ▶ return ( $exit ? 0 : 1, $state );
Branch: 1 branch
BranchTrueFalseTotal
$exit ? : 39 44 83
119 } ## end if ( $self->os eq 'linux' )
120 27 ▶ if ( $self->os eq 'freebsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "freebsd") 27 0 27
121 27 my ( $exit, $output ) = $self->run_command( $SERVICE, $self->jail_args, $service, 'status' );
122 23 ▶ return ( 1, 'running' ) if !$exit;
Branch: 1 branch
BranchTrueFalseTotal
unless $exit 12 11 23
123 11 ▶ return ( 0, $output =~ /does not exist|not found/i ? 'not there' : 'not running' );
Branch: 1 branch
BranchTrueFalseTotal
$output =~ /does not exist|not found/iu ? : 5 6 11
124 }
125  
126 0 die( 'not supported on ' . $self->os . "\n" );
127 } ## end sub running
128  
129 =head2 enabled
130  
131 Returns 1 if a service is enabled, else 0.
132  
133 =cut
134  
135 36 ▶sub enabled {
Subroutine enabled: called CC=3 SCAR=11.0
Pod documented
136 36 my ( $self, $service ) = @_;
137  
138 36 ▶ my ($exit)
Branch: 1 branch
BranchTrueFalseTotal
$self->os eq 'linux' ? : 28 8 36
139 = $self->os eq 'linux'
140 ? $self->run_command( $SYSTEMCTL, 'is-enabled', $service )
141 : $self->run_command( $SERVICE, $self->jail_args, $service, 'enabled' );
142  
143 33 ▶ return $exit ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$exit ? : 9 24 33
144 } ## end sub enabled
145  
146 =head2 jail_args
147  
148 Returns the args for service to run in --jail, if given.
149  
150 =cut
151  
152 35 ▶sub jail_args {
Subroutine jail_args: called CC=2 SCAR=6.9
Pod documented
153 35 my ($self) = @_;
154  
155 35 ▶ return defined( $self->opt('jail') ) ? ( '-j', $self->opt('jail') ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->opt('jail') ? : 5 30 35
156 }
157  
158 32 1;
« blib/lib/Monitoring/Sneck/Checks/pid_file.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/base.pm »