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

filter: Statementstmt 100% 41 / 41 Branchbran 75.0% 9 / 12 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 91.3% 74 / 81 CC 12 SCAR 16.6
CC
12
Cov
92%
check
9.1
validate
4.0
BEGIN
1.0
CRAP 5.2
« blib/lib/Monitoring/Sneck/Checks/disk.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/file_age.pm »
1 package Monitoring::Sneck::Checks::systemd_failed;
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::systemd_failed - checks for failed systemd units
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 32 our $VERSION = '0.0.1';
19  
20 # Where systemctl is. 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  
23 =head1 SYNOPSIS
24  
25 failed_units|perl:systemd_failed
26 failed_units_most|perl:systemd_failed -x '^user@' -x 'flush\.service$'
27  
28 =head1 DESCRIPTION
29  
30 Lists the failed units through systemctl --failed, as a service that
31 crashed and stayed down. By default any is CRITICAL.
32  
33 systemctl not being there, as on a host without systemd, or failing,
34 is UNKNOWN.
35  
36 =head1 OPTIONS
37  
38 - -x, --exclude :: A regex for units to leave out. May be given more
39 than once.
40  
41 - -w, --warning :: Range for the number of failed units.
42  
43 - -c, --critical :: Range for the number of failed units.
44 Default :: 0
45  
46 =cut
47  
48 63 ▶sub option_spec { return ( 'exclude|x=s@', 'warning|w=s', 'critical|c=s' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
49  
50 63 ▶sub defaults { return { critical => '0' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
51  
52 63 ▶sub ranges { return ( 'warning', 'critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
53  
54 63 ▶sub validate {
Subroutine validate: called CC=4 SCAR=13.9
Pod documented
55 63 my ( $self, $options ) = @_;
56  
57 63 my @problems;
58 63 ▶ foreach my $regex ( @{ $options->{exclude} // [] } ) {
Condition100%2/2
$options->{"exclude"} // []
l!l
2142
MC/DC100%1/1
$options->{"exclude"} // []
$options->{"exclude"}
Truth table100%2/2
A
A= $options->{"exclude"}
Aresult
11
00
59 42 ▶ 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 42 42
60 }
61  
62 63 return @problems;
63 } ## end sub validate
64  
65 =head1 METHODS
66  
67 =head2 check
68  
69 Does the check.
70  
71 =cut
72  
73 63 ▶sub check {
Subroutine check: called CC=9 SCAR=22.1
Pod documented
74 63 my ($self) = @_;
75  
76 63 my ( $exit, $output ) = $self->run_command( $SYSTEMCTL, '--failed', '--plain', '--no-legend', '--no-pager' );
77 60 ▶ return $self->finish( $self->UNKNOWN,
Branch: 1 branch
BranchTrueFalseTotal
if $exit 19 41 60
Condition50%1/2
(split(/\n/u, $output, 0))[0] // ""
l!l
190
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
78 'systemctl --failed exited ' . $exit . ', "' . ( ( split( /\n/, $output ) )[0] // '' ) . '"' )
79 if $exit;
80  
81 41 ▶ my @excludes = @{ $self->opt('exclude') // [] };
Condition100%2/2
$self->opt("exclude") // []
l!l
2021
MC/DC100%1/1
$self->opt("exclude") // []
$self->opt("exclude")
Truth table100%2/2
A
A= $self->opt("exclude")
Aresult
11
00
82 41 my @failed;
83 41 foreach my $line ( split( /\n/, $output ) ) {
84 82 my ($unit) = $line =~ /\A\s*\W?\s*(\S+\.\w+)\s/;
85 82 ▶ next if !defined($unit) || grep { $unit =~ /$_/ } @excludes;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $unit or grep {$unit =~ /$_/u;} @excludes 40 42 82
Condition66%2/3
not defined $unit or grep {$unit =~ /$_/u;} @excludes
l!l&&r!l&&!r
04042
MC/DC50%1/2
not defined $unit or grep {$unit =~ /$_/u;} @excludes
not defined $unit grep {$unit =~ /$_/u;} @excludes
Truth table66%2/3
not(A) or B
A= not defined $unitB= grep {$unit =~ /$_/u;} @excludes
ABresult
1X1
011
000
86 42 push( @failed, $unit );
87 }
88  
89 41 my $count = scalar(@failed);
90 41 $self->perfdata(
91 label => 'failed',
92 value => $count,
93 warning => $self->opt('warning'),
94 critical => $self->opt('critical'),
95 min => 0
96 );
97 41 ▶ my $message = $count . ' failed unit' . ( $count == 1 ? '' : 's' );
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 ? : 0 41 41
98 41 ▶ $message = $message . ', ' . join( ', ', @failed ) if defined( $failed[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $failed[0] 21 20 41
99  
100 41 return $self->finish( $self->threshold_status( $count, $self->opt('warning'), $self->opt('critical') ), $message );
101 } ## end sub check
102  
103 32 1;
« blib/lib/Monitoring/Sneck/Checks/disk.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/file_age.pm »