File Coverage

blib/lib/Device/RAID/Poller/Backends/FBSD_gmirror.pm
Criterion Covered Total %
statement 8 58 13.7
branch 0 24 0.0
condition 0 3 0.0
subroutine 3 6 50.0
pod 3 3 100.0
total 14 94 14.8


line stmt bran cond sub pod time code
1             package Device::RAID::Poller::Backends::FBSD_gmirror;
2              
3 1     1   72359 use 5.006;
  1         14  
4 1     1   5 use strict;
  1         2  
  1         22  
5 1     1   5 use warnings;
  1         2  
  1         627  
6              
7             =head1 NAME
8              
9             Device::RAID::Poller::Backends::FBSD_gmirror - FreeBSD GEOM mirror RAID backend.
10              
11             =head1 VERSION
12              
13             Version 0.0.0
14              
15             =cut
16              
17             our $VERSION = '0.0.0';
18              
19              
20             =head1 SYNOPSIS
21              
22             use Device::RAID::Poller::Backends::FBSD_gmirror;
23            
24             my $backend = Device::RAID::Poller::Backends::FBSD_gmirror->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::FBSD_gmirror->new;
42              
43             =cut
44              
45             sub new {
46 0     0 1   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             sub run {
67 0     0 1   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 0         if ( ! $self->{usable} ){
76 0           return %return_hash;
77             }
78              
79             # Fetch the raw gmirror status.
80 0           my $raw=`/sbin/gmirror status`;
81 0 0         if ( $? != 0 ){
82 0           return %return_hash;
83             }
84              
85 0           my @raw_split=split( /\n/, $raw );
86              
87             # The first line contains nothing of interest.
88 0           shift @raw_split;
89              
90 0           my $dev=undef;
91 0           foreach my $line (@raw_split){
92 0           my @line_split=split( /[\t ]+/, $line );
93              
94             # lines starting with mirror mean we have a new dev
95 0 0         if ( $line_split[0] =~ /^mirror/ ){
96 0           $dev=$line_split[0];
97              
98             #create the device if needed in the return hash
99 0 0         if ( ! defined( $return_hash{$dev} ) ){
100 0           $return_hash{devices}{$dev}={
101             'backend'=>'FBSD_gmirror',
102             'name'=>$dev,
103             'good'=>[],
104             'bad'=>[],
105             'spare'=>[],
106             'type'=>'mirror',
107             'BBUstatus'=>'na',
108             'status'=>'unknown',
109             };
110             }
111              
112             # Check known mirror status values.
113             # Values pulled from sys/geom/mirror/g_mirror.c
114 0 0         if ( $line_split[1] eq 'COMPLETE' ){
    0          
115 0           $return_hash{devices}{$dev}{status}='good';
116             }elsif( $line_split[1] eq "DEGRADED" ){
117 0           $return_hash{devices}{$dev}{status}='bad';
118             }
119              
120             # Check known disk status values.
121             # Values pulled from sys/geom/mirror/g_mirror.c
122 0 0         if ( $line_split[3] eq "(SYNCHRONIZING)" ){
    0          
123 0           $return_hash{devices}{$dev}{status}='rebuilding';
124 0           push( @{ $return_hash{devices}{$dev}{good} }, $line_split[2] );
  0            
125             }elsif( $line_split[3] eq "(ACTIVE)" ){
126 0           push( @{ $return_hash{devices}{$dev}{good} }, $line_split[2] );
  0            
127             }else{
128 0           push( @{ $return_hash{devices}{$dev}{bad} }, $line_split[2] );
  0            
129             }
130              
131             }else{
132             # Check known disk status values.
133             # Values pulled from sys/geom/mirror/g_mirror.c
134 0 0         if ( $line_split[2] eq "(SYNCHRONIZING)" ){
    0          
135 0           $return_hash{$dev}{status}='rebuilding';
136 0           push( @{ $return_hash{devices}{$dev}{good} }, $line_split[1] );
  0            
137             }elsif( $line_split[2] eq "(ACTIVE)" ){
138 0           push( @{ $return_hash{devices}{$dev}{good} }, $line_split[1] );
  0            
139             }else{
140 0           push( @{ $return_hash{devices}{$dev}{bad} }, $line_split[1] );
  0            
141             }
142             }
143             }
144              
145 0           $return_hash{status}=1;
146              
147 0           return %return_hash;
148             }
149              
150             =head2 usable
151              
152             Returns a perl boolean for if it is usable or not.
153              
154             my $usable=$backend->usable;
155             if ( ! $usable ){
156             print "This backend is not usable.\n";
157             }
158              
159             =cut
160              
161             sub usable {
162 0     0 1   my $self=$_[0];
163              
164 0 0 0       if (
165             ( $^O !~ 'freebsd' ) &&
166             ( ! -x '/sbin/gmirror' )
167             ){
168 0           $self->{usable}=0;
169 0           return 0;
170             }
171              
172             # Test for it via this instead of '/sbin/kldstat -q -n geom_mirror'
173             # as kldstat won't work if it is compiled in.
174 0           system('/sbin/sysctl -q kern.features.geom_mirror > /dev/null');
175 0 0         if ( $? != 0 ){
176 0           $self->{usable}=0;
177 0           return 0;
178             }
179              
180 0           $self->{usable}=1;
181 0           return 1;
182             }
183              
184             =head1 AUTHOR
185              
186             Zane C. Bowers-Hadley, C<< >>
187              
188             =head1 BUGS
189              
190             Please report any bugs or feature requests to C, or through
191             the web interface at L. I will be
192             notified, and then you'll automatically be notified of progress on your bug as I make changes.
193              
194              
195              
196              
197             =head1 SUPPORT
198              
199             You can find documentation for this module with the perldoc command.
200              
201             perldoc Device::RAID::Poller
202              
203              
204             You can also look for information at:
205              
206             =over 4
207              
208             =item * RT: CPAN's request tracker (report bugs here)
209              
210             L
211              
212             =item * AnnoCPAN: Annotated CPAN documentation
213              
214             L
215              
216             =item * CPAN Ratings
217              
218             L
219              
220             =item * Search CPAN
221              
222             L
223              
224             =back
225              
226              
227             =head1 ACKNOWLEDGEMENTS
228              
229              
230             =head1 LICENSE AND COPYRIGHT
231              
232             This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
233              
234             This is free software, licensed under:
235              
236             The Artistic License 2.0 (GPL Compatible)
237              
238              
239             =cut
240              
241             1; # End of Device::RAID::Poller