Poller/Backends/Linux_mdadm.pm

filter: Statementstmt 12.8% 10 / 78 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 3 / 6 Podpod 100% 3 / 3 totaltotal 11.1% 16 / 143 CC 26 SCAR 60.2
CC
26
Cov
8%
run
552.0
usable
20.0
new
2.0
CRAP 412.3
« Poller/Backends/Avago_tw_cli.pm ↑ Summary Poller/Backends/Adaptec_arcconf.pm »
1 package Device::RAID::Poller::Backends::Linux_mdadm;
2  
3 1 ▶use 5.006;
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  
7 =head1 NAME
8  
9 Device::RAID::Poller::Backends::Linux_mdadm - Handles Linux mdadm software RAID.
10  
11 =head1 VERSION
12  
13 Version 0.0.0
14  
15 =cut
16  
17 1 our $VERSION = '0.0.0';
18  
19  
20 =head1 SYNOPSIS
21  
22 use Device::RAID::Poller::Backends::Linux_mdadm;
23
24 my $backend = Device::RAID::Poller::Backends::Linux_mdadm->new;
25
26 my $usable=$backend->usable;
27 my %return_hash;
28 if ( $usable ){
29 %return_hash=$backend->run;
30 my %status=$backend->run;
31 use Data::Dumper;
32 print Dumper( \%status );
33 }
34  
35 =head1 METHODS
36  
37 =head2 new
38  
39 Initiates the backend object.
40  
41 my $backend = Device::RAID::Poller::Backends::Linux_mdadm->new;
42  
43 =cut
44  
45 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
46 0 my $self = {
47 usable=>0,
48 };
49 0 bless $self;
50  
51 0 return $self;
52 }
53  
54 =head2 run
55  
56 Runs the poller backend and report the results.
57  
58 If nothing is nothing is loaded, load will be called.
59  
60 my %status=$backend->run;
61 use Data::Dumper;
62 print Dumper( \%status );
63  
64 =cut
65  
66 0 ▶sub run {
Subroutine run: not called CC=23 SCAR=63.1
Pod documented
67 0 my $self=$_[0];
68  
69 0 my %return_hash=(
70 'status'=>0,
71 'devices'=>{},
72 );
73  
74 # if not usable, no point in continuing
75 0 ▶ if ( ! $self->{usable} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"usable"}) 0 0 0
76 0 return %return_hash;
77 }
78  
79 0 $return_hash{status}=1;
80  
81 # get a list of devices
82 0 my $raw=`mdadm --detail --scan`;
83 0 my @raw_split=split(/\n/, $raw);
84 0 my @devs;
85 0 foreach my $line (@raw_split){
86 0 ▶ if ( $line =~ /^ARRAY/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^ARRAY/) 0 0 0
87 0 my @line_split=split(/[\t ]+/, $line);
88 0 push(@devs, $line_split[1]);
89 }
90 }
91  
92 # Process each md device that exists.
93 0 foreach my $dev (@devs){
94 0 $return_hash{devices}{$dev}={
95 'backend'=>'Linux_mdadm',
96 'name'=>$dev,
97 'good'=>[],
98 'bad'=>[],
99 'spare'=>[],
100 'type'=>'mdadm',
101 'BBUstatus'=>'na',
102 'status'=>'unknown',
103 };
104  
105 # check device and break it appart
106 0 $raw=`mdadm --detail $dev`;
107 0 @raw_split=split(/\n/, $raw);
108 0 my $number_found=0;
109 0 my $process=0;
110 0 foreach my $line (@raw_split){
111 0 chomp($line);
112  
113 0 ▶ if ( $line =~ /[\t ]*Number/ ){
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /[\t ]*Number/) { } 0 0 0
elsif ($number_found) { } 0 0 0
114 0 $number_found=1;
115 }elsif( $number_found ){
116 0 $process=1;
117 }
118  
119 0 ▶ if ( $process ){
Branch: 1 branch
BranchTrueFalseTotal
if ($process) 0 0 0
120 # good disk...
121 # active
122 # rebuilding
123 # bad disk...
124 # removed
125 # spare disks...
126 # spare
127  
128 # spare rebuilding = good, rebuilding... in the process of becoming not a spare
129  
130 0 my $disk_status='good';
131 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /active/ or $line =~ /rebuilding/) 0 0 0
Condition0%0/3
$line =~ /active/ or $line =~ /rebuilding/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /active/ or $line =~ /rebuilding/
$line =~ /active/ $line =~ /rebuilding/
Truth table0%0/3
A or B
A= $line =~ /active/B= $line =~ /rebuilding/
ABresult
1X1
011
000
132 ( $line =~ /active/ ) ||
133 ( $line =~ /rebuilding/ )
134 ){
135 0 $disk_status='good';
136 }
137  
138 0 ▶ if ( $line =~ /spare/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /spare/) 0 0 0
139 0 $disk_status='spare';
140 }
141  
142 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /spare/ and $line =~ /rebuilding/) 0 0 0
Condition0%0/3
$line =~ /spare/ and $line =~ /rebuilding/
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /spare/ and $line =~ /rebuilding/
$line =~ /spare/ $line =~ /rebuilding/
Truth table0%0/3
A and B
A= $line =~ /spare/B= $line =~ /rebuilding/
ABresult
0X0
100
111
143 ( $line =~ /spare/ ) &&
144 ( $line =~ /rebuilding/ )
145 ){
146 0 $disk_status='good';
147 }
148  
149 0 ▶ if ( $line =~ /removed/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /removed/) 0 0 0
150 0 $disk_status='bad';
151 }
152  
153 0 $line=~s/^.*[\t ]//;
154 0 push(@{ $return_hash{devices}{$dev}{$disk_status} }, $line);
155 }{
156 0 ▶ if ( $line =~ /^[\t ]*State/ ){
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^[\t ]*State/) { } 0 0 0
elsif ($line =~ /^[\t ]*Raid\ Level[\t ]*\:[\t ]*/) { } 0 0 0
157 # good states...
158 # clean
159 # active
160 # bad states...
161 # degraded
162 # inactive
163 # rebuilding states...
164 # resyncing
165 # recovering
166  
167 # clean, degraded = degraded
168 # clean, degraded, recovering = rebuilding
169  
170 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /clean/ or $line =~ /active/) 0 0 0
Condition0%0/3
$line =~ /clean/ or $line =~ /active/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /clean/ or $line =~ /active/
$line =~ /clean/ $line =~ /active/
Truth table0%0/3
A or B
A= $line =~ /clean/B= $line =~ /active/
ABresult
1X1
011
000
171 ( $line =~ /clean/ ) ||
172 ( $line =~ /active/ )
173 ){
174 0 $return_hash{devices}{$dev}{status}='good';
175 }
176  
177 0 ▶ if ( $line =~ /degraded/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /degraded/) 0 0 0
178 0 $return_hash{devices}{$dev}{status}='bad';
179 }
180  
181 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /recovering/ or $line =~ /resyncing/) 0 0 0
Condition0%0/3
$line =~ /recovering/ or $line =~ /resyncing/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /recovering/ or $line =~ /resyncing/
$line =~ /recovering/ $line =~ /resyncing/
Truth table0%0/3
A or B
A= $line =~ /recovering/B= $line =~ /resyncing/
ABresult
1X1
011
000
182 ( $line =~ /recovering/ ) ||
183 ( $line =~ /resyncing/ )
184 ){
185 0 $return_hash{devices}{$dev}{status}='rebuilding';
186 }
187  
188 0 ▶ if ( $line =~ /inactive/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /inactive/) 0 0 0
189 0 $return_hash{devices}{$dev}{status}='bad';
190 }
191  
192 }elsif( $line =~ /^[\t ]*Raid\ Level[\t ]*\:[\t ]*/ ){
193 0 $line=~s/^[\t ]*Raid\ Level[\t ]*\:[\t ]*//;
194 0 $return_hash{devices}{$dev}{type}=$line;
195 }
196 }
197  
198 }
199 }
200  
201 0 return %return_hash;
202 }
203  
204 =head2 usable
205  
206 Returns a perl boolean for if it is usable or not.
207  
208 my $usable=$backend->usable;
209 if ( ! $usable ){
210 print "This backend is not usable.\n";
211 }
212  
213 =cut
214  
215 0 ▶sub usable {
Subroutine usable: not called CC=4 SCAR=30.0
Pod documented
216 0 my $self=$_[0];
217  
218 0 ▶ if ( $^O !~ 'linux' ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /linux/) 0 0 0
219 0 $self->{usable}=0;
220 0 return 0;
221 }
222  
223 # make sure we can locate mdadm
224 0 my $mdadm_bin=`/bin/sh -c 'which mdadm 2> /dev/null'`;
225 0 ▶ if ( $? != 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
226 0 $self->{usable}=0;
227 0 return 0;
228 }
229 0 chomp($mdadm_bin);
230 0 $self->{mdadm_bin}=$mdadm_bin;
231  
232 # make sure we have atleast one device
233 0 my $raw=`mdadm --detail --scan`;
234 0 ▶ if ( $raw !~ /^ARRAY/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($raw =~ /^ARRAY/) 0 0 0
235 0 $self->{usable}=0;
236 0 return 0;
237 }
238  
239 0 $self->{usable}=1;
240 0 return 1;
241 }
242  
243 =head1 AUTHOR
244  
245 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
246  
247 =head1 BUGS
248  
249 Please report any bugs or feature requests to C<bug-device-raid-poller at rt.cpan.org>, or through
250 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Device-RAID-Poller>. I will be
251 notified, and then you'll automatically be notified of progress on your bug as I make changes.
252  
253 =head1 SUPPORT
254  
255 You can find documentation for this module with the perldoc command.
256  
257 perldoc Device::RAID::Poller
258  
259  
260 You can also look for information at:
261  
262 =over 4
263  
264 =item * RT: CPAN's request tracker (report bugs here)
265  
266 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Device-RAID-Poller>
267  
268 =item * AnnoCPAN: Annotated CPAN documentation
269  
270 L<http://annocpan.org/dist/Device-RAID-Poller>
271  
272 =item * CPAN Ratings
273  
274 L<https://cpanratings.perl.org/d/Device-RAID-Poller>
275  
276 =item * Search CPAN
277  
278 L<https://metacpan.org/release/Device-RAID-Poller>
279  
280 =back
281  
282  
283 =head1 ACKNOWLEDGEMENTS
284  
285  
286 =head1 LICENSE AND COPYRIGHT
287  
288 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
289  
290 This is free software, licensed under:
291  
292 The Artistic License 2.0 (GPL Compatible)
293  
294  
295 =cut
296  
297 1 1; # End of Device::RAID::Poller
« Poller/Backends/Avago_tw_cli.pm ↑ Summary Poller/Backends/Adaptec_arcconf.pm »