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

filter: Statementstmt 100% 78 / 78 Branchbran 92.1% 35 / 38 Conditioncond 85.0% 17 / 20 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 94.3% 149 / 158 CC 34 SCAR 24.4
CC
34
Cov
96%
linux_arrays
16.0
freebsd_arrays
12.0
check
8.0
CRAP 11.5
« blib/lib/Monitoring/Sneck/Checks/https_cert.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/oom.pm »
1 package Monitoring::Sneck::Checks::swraid;
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::swraid - checks software RAID, md on Linux and gmirror and graid on FreeBSD
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 on FreeBSD. Set by tests.
21 32 our $GMIRROR = '/sbin/gmirror';
22 32 our $GRAID = '/sbin/graid';
23  
24 =head1 SYNOPSIS
25  
26 swraid|perl:swraid
27 swraid_boot|perl:swraid -a md0 -a md1
28 swraid_gm|perl:swraid -a mirror/gm0
29  
30 =head1 DESCRIPTION
31  
32 Checks every software RAID array. One that is degraded, has failed
33 devices, or is not running is CRITICAL. One rebuilding or resyncing is
34 WARNING, with how far along it is. A check or scrub running is fine.
35 Arrays given with --array must be there.
36  
37 On Linux, md arrays are read from /proc/mdstat. On FreeBSD,
38 gmirror status -s and graid status -s are run, those classes not being
39 loaded, or the commands not being there, counting as having no arrays. Other OSes are UNKNOWN.
40  
41 Having no arrays at all is OK, unless --array says one should be there.
42  
43 =head1 OPTIONS
44  
45 - -a, --array :: A array that must be there, such as md0, mirror/gm0, or
46 raid/r0. May be given more than once.
47  
48 =cut
49  
50 178 ▶sub option_spec { return ('array|a=s@') }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
51  
52 =head1 METHODS
53  
54 =head2 check
55  
56 Does the check.
57  
58 =cut
59  
60 178 ▶sub check {
Subroutine check: called CC=8 SCAR=20.8
Pod documented
61 178 my ($self) = @_;
62  
63 my @arrays
64 178 ▶ = $self->os eq 'linux' ? $self->linux_arrays
Branch: 2 branches
BranchTrueFalseTotal
$self->os eq 'linux' ? : 120 58 178
$self->os eq 'freebsd' ? : 58 0 58
65 : $self->os eq 'freebsd' ? $self->freebsd_arrays
66 : die( 'not supported on ' . $self->os . "\n" );
67 175 my %have = map { $_->{name} => 1 } @arrays;
68 175 my @codes;
69 my @parts;
70 175 ▶ foreach my $name ( @{ $self->opt('array') // [] } ) {
Condition100%2/2
$self->opt("array") // []
l!l
57118
MC/DC100%1/1
$self->opt("array") // []
$self->opt("array")
Truth table100%2/2
A
A= $self->opt("array")
Aresult
11
00
71 57 ▶ next if $have{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $have{$name} 27 30 57
72 30 push( @codes, $self->CRITICAL );
73 30 push( @parts, $name . ' is not there' );
74 }
75 175 foreach my $array (@arrays) {
76 351 push( @codes, $array->{code} );
77 351 push( @parts, $array->{name} . ' ' . $array->{says} );
78 }
79 175 ▶ return $self->finish( $self->OK, 'no software RAID arrays' ) if !defined( $parts[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parts[0] 30 145 175
80  
81 145 return $self->finish( $self->worst(@codes), join( ', ', @parts ) );
82 } ## end sub check
83  
84 =head2 linux_arrays
85  
86 Returns the md arrays from /proc/mdstat, as hash refs of name, code,
87 and says, what to say about it.
88  
89 =cut
90  
91 120 ▶sub linux_arrays {
Subroutine linux_arrays: called CC=16 SCAR=27.8
Pod documented
92 120 my ($self) = @_;
93  
94 120 ▶ my $mdstat = eval { $self->proc_file('mdstat') } // return ();
Condition100%2/2
eval { do { $self->proc_file("mdstat") } } // (return ())
l!l
9030
MC/DC100%1/1
eval { do { $self->proc_file("mdstat") } } // (return ())
eval { do { $self->proc_file("mdstat") } }
Truth table100%2/2
A
A= eval { do { $self->proc_file("mdstat") } }
Aresult
11
00
95 90 my @arrays;
96 90 foreach my $block ( split( /\n(?=md[0-9a-z_]+ :)/, $mdstat ) ) {
97 330 ▶ next if $block !~ /\A(md[0-9a-z_]+) : (\w+)(?: \([^)]*\))*(?: (\w+))?([^\n]*)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $block =~ /\A(md[0-9a-z_]+) : (\w+)(?: \([^)]*\))*(?: (\w+))?([^\n]*)/u 90 240 330
98 240 ▶ my ( $name, $state, $level, $devices ) = ( $1, $2, $3 // '', $4 );
Condition50%1/2
$3 // ""
l!l
2400
MC/DC0%0/1
$3 // ""
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
99 240 my @failed = $devices =~ /(\S+?)\[[0-9]+\]\(F\)/g;
100 240 my ( $wanted, $working ) = $block =~ /\[([0-9]+)\/([0-9]+)\]/;
101 240 my ( $doing, $percent ) = $block =~ /\b(recovery|resync|reshape|check|repair)\s*=\s*([0-9.]+)%/;
102  
103 240 my %array = ( name => $name );
104 240 ▶ if ( $state ne 'active' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($state ne 'active') { } 30 210 240
elsif (defined $failed[0] or defined $working and $working < $wanted) { } 60 150 210
elsif (defined $doing and $doing ne 'check' and $doing ne 'repair') { } 60 90 150
105 30 @array{ 'code', 'says' } = ( $self->CRITICAL, 'is ' . $state );
106 ▶ } elsif ( defined( $failed[0] ) || ( defined($working) && $working < $wanted ) ) {
Condition100%6/6
defined $failed[0] or defined $working and $working < $wanted
l!l&&r!l&&!r
3030150
defined $working and $working < $wanted
!ll&&!rl&&r
3012030
MC/DC100%3/3
defined $failed[0] or defined $working and $working < $wanted
defined $failed[0] defined $working $working < $wanted
Truth table100%4/4
A or B and C
A= defined $failed[0]B= defined $workingC= $working < $wanted
ABCresult
1XX1
0111
00X0
0100
107 60 ▶ my $says
Branch: 1 branch
BranchTrueFalseTotal
defined $working ? : 60 0 60
108 = $level . ' degraded' . ( defined($working) ? ', ' . $working . ' of ' . $wanted . ' working' : '' );
109 60 ▶ $says = $says . ', failed ' . join( ', ', @failed ) if defined( $failed[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $failed[0] 30 30 60
110 60 ▶ $says = $says . ', ' . $doing . ' ' . $percent . '%' if defined($doing);
Branch: 1 branch
BranchTrueFalseTotal
if defined $doing 30 30 60
111 60 @array{ 'code', 'says' } = ( $self->CRITICAL, $says );
112 ▶ } elsif ( defined($doing) && $doing ne 'check' && $doing ne 'repair' ) {
Condition83%5/6
defined $doing and $doing ne "check" and $doing ne "repair"
!ll&&!rl&&r
90060
defined $doing and $doing ne "check"
!ll&&!rl&&r
603060
MC/DC66%2/3
defined $doing and $doing ne "check" and $doing ne "repair"
defined $doing $doing ne "check" $doing ne "repair"
Truth table75%3/4
A and B and C
A= defined $doingB= $doing ne "check"C= $doing ne "repair"
ABCresult
0XX0
10X0
1100
1111
113 60 @array{ 'code', 'says' } = ( $self->WARNING, $level . ' ' . $doing . ' ' . $percent . '%' );
114 } else {
115 90 ▶ @array{ 'code', 'says' }
Branch: 1 branch
BranchTrueFalseTotal
defined $doing ? : 30 60 90
116 = ( $self->OK, $level . ' ok' . ( defined($doing) ? ', ' . $doing . ' ' . $percent . '%' : '' ) );
117 }
118 240 push( @arrays, \%array );
119 } ## end foreach my $block ( split( /\n(?=md[0-9a-z_]+ :)/...))
120  
121 90 return @arrays;
122 } ## end sub linux_arrays
123  
124 =head2 freebsd_arrays
125  
126 Returns the gmirror and graid arrays, as for L</linux_arrays>.
127  
128 =cut
129  
130 58 ▶sub freebsd_arrays {
Subroutine freebsd_arrays: called CC=12 SCAR=24.9
Pod documented
131 58 my ($self) = @_;
132  
133 58 my @arrays;
134 58 foreach my $class ( [ $GMIRROR, 'COMPLETE' ], [ $GRAID, 'OPTIMAL' ] ) {
135 115 my ( $command, $good ) = @{$class};
136 115 ▶ next if !-x $command;
Branch: 1 branch
BranchTrueFalseTotal
unless -x $command 28 87 115
137 87 my ( $exit, $output ) = $self->run_command( $command, 'status', '-s' );
138  
139 # the class not being loaded exits non zero, saying so
140 84 ▶ next if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 28 56 84
141  
142 # NAME STATUS COMPONENT (STATE), one line per component
143 56 my %by_name;
144 my @order;
145 56 foreach my $line ( split( /\n/, $output ) ) {
146 199 ▶ next if $line !~ /\A\s*(\S+)\s+(\S+)\s+(\S+)(?:\s+\((.*)\))?\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A\s*(\S+)\s+(\S+)\s+(\S+)(?:\s+\((.*)\))?\s*\z/u 0 199 199
147 199 ▶ my ( $name, $status, $component, $component_state ) = ( $1, $2, $3, $4 // '' );
Condition50%1/2
$4 // ""
l!l
1990
MC/DC0%0/1
$4 // ""
$4
Truth table50%1/2
A
A= $4
Aresult
11
00
148 199 ▶ push( @order, $name ) if !$by_name{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $by_name{$name} 114 85 199
149 199 $by_name{$name}{status} = $status;
150 199 ▶ push( @{ $by_name{$name}{busy} }, $component . ' ' . lc($component_state) )
Branch: 1 branch
BranchTrueFalseTotal
if $component_state =~ /SYNCHRONIZING|REBUILD|RESYNC/u 29 170 199
151 if $component_state =~ /SYNCHRONIZING|REBUILD|RESYNC/;
152 }
153 56 foreach my $name (@order) {
154 114 my $array = $by_name{$name};
155 114 my %result = ( name => $name );
156 114 ▶ if ( $array->{status} eq $good ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($array->{'status'} eq $good) { } 56 58 114
elsif (defined $array->{'busy'}) { } 29 29 58
157 56 @result{ 'code', 'says' } = ( $self->OK, lc( $array->{status} ) );
158 } elsif ( defined( $array->{busy} ) ) {
159 @result{ 'code', 'says' }
160 29 = ( $self->WARNING, lc( $array->{status} ) . ', ' . join( ', ', @{ $array->{busy} } ) );
161 } else {
162 29 @result{ 'code', 'says' } = ( $self->CRITICAL, lc( $array->{status} ) );
163 }
164 114 push( @arrays, \%result );
165 } ## end foreach my $name (@order)
166 } ## end foreach my $class ( [ $GMIRROR, 'COMPLETE' ], [...])
167  
168 55 return @arrays;
169 } ## end sub freebsd_arrays
170  
171 32 1;
« blib/lib/Monitoring/Sneck/Checks/https_cert.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/oom.pm »