blib/lib/Parse/Arcconf.pm

filter: Statementstmt 16.4% 16 / 97 Branchbran 1.3% 1 / 72 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 4 / 6 Podpod 100% 4 / 4 totaltotal 13.5% 25 / 184 CC 38 SCAR 70.2
CC
38
Cov
10%
parse_config_fh
1332.0
parse_config
6.0
parse_config_file
2.4
CRAP 1115.6
↑ Summary
1 #!/usr/bin/perl -w
2  
3 # Copyright 2012 Mathieu Alorent.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of either: the GNU General Public License as published
7 # by the Free Software Foundation; or the Artistic License.
8 #
9 # See http://dev.perl.org/licenses/ for more information.
10  
11 package Parse::Arcconf;
12  
13 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Parse::Arcconf - Parse the output of arcconf utility.
19  
20 =head1 VERSION
21  
22 Version 0.01
23  
24 =cut
25  
26 3 our $VERSION = '0.03';
27  
28 =head1 SYNOPSIS
29  
30 Parse::Arcconf parses the output of C<arcconf> utility to allow
31 programmatic access to the RAID configuration information provided by the
32 arcconf utility on Adaptec RAID cards.
33  
34 use Parse::Arcconf;
35  
36 my $arcconf = Parse::Arcconf->new();
37  
38 Run the C<arcconf> tool directly to query the hardware (requires root):
39  
40 my $controllers = $arcconf->parse_config();
41
42 Parse a text file created already by running C<arcconf> tool:
43  
44 my $controllers = $arcconf->parse_config_file("foo.txt");
45  
46 Read from a file descriptor already opened by the program:
47  
48 my $controllers = $arcconf->parse_config_fh(\*STDIN);
49  
50 =head1 SUBROUTINES/METHODS
51  
52 =head2 new
53  
54 Return an instance of the Parse::Arcconf class that can be used to parse
55 input in one of several ways.
56  
57 =cut
58  
59 sub new
60 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
61 1 my ($class, $config, $plugin) = @_;
62 1 my $self = {};
63  
64 1 bless($self, $class);
65 1 return $self;
66 }
67  
68 =head2 parse_config
69  
70 Attempt to run the arcconf utility, and parse the output. This command
71 actually uses parse_config_fh() after opening a pipe to the relevant command.
72  
73 The command that is actually run is approximately:
74  
75 =over 4
76  
77 arcconf GETCONFIG 1
78  
79 =back
80  
81 This command requires root access, and Parse::Arcconf makes no attempt to
82 use sudo or any other method to gain root access. It is recommended to call
83 your script which uses this module as root.
84  
85 The parse_config_fh() and parse_config_file() will expect output equivalent
86 to that from the above command.
87  
88 =cut
89  
90 sub parse_config
91 0 ▶{
Subroutine parse_config: not called CC=2 SCAR=17.9
Pod documented
92 0 my ($self) = @_;
93  
94 0 my $arcconf = "/usr/sbin/arcconf";
95 0 my $argument = "GETCONFIG 1";
96 0 my $command = sprintf("%s %s|", $arcconf, $argument);
97  
98 0 my $fh;
99 0 ▶ if(open $fh, $command)
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, $command) 0 0 0
100 {
101 0 my $c = $self->parse_config_fh($fh);
102 0 close $fh;
103 0 return $c;
104 }
105 0 return undef;
106 }
107  
108 =head2 parse_config_file
109  
110 Open and parse a file containing the output from arcconf.
111  
112 =cut
113  
114 sub parse_config_file
115 1 ▶{
Subroutine parse_config_file: called CC=2 SCAR=8.5
Pod documented
116 1 my ($self, $file) = @_;
117  
118 1 my $fh;
119 1 ▶ if(open $fh, "<".$file)
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, '<' . $file) 0 1 1
120 {
121 0 my $c = $self->parse_config_fh($fh);
122 0 close $fh;
123 0 return $c;
124 }
125 1 return undef;
126  
127 }
128  
129 =head2 parse_config_fh
130  
131 Read from the file handle and parse it, returning a hash-of-hashes.
132  
133 =cut
134  
135 sub parse_config_fh
136 0 ▶{
Subroutine parse_config_fh: not called CC=36 SCAR=71.9
Pod documented
137 0 my ($self, $fh) = @_;
138  
139 0 my $controller = {};
140 0 my $total_controller = 0;
141 0 my $current_controller = 0;
142 0 my $current_logical_drive = undef;
143 0 my $current_physical_drive = undef;
144 0 my $ctrl = undef;
145 0 my $line = undef;
146  
147 0 ▶ LEVEL1: while($line = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
148 {
149 0 chomp $line;
150  
151 0 ▶ next if($line =~ /^$/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 0 0 0
152 0 ▶ next if($line =~ /^-+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^-+$/ 0 0 0
153  
154 0 ▶ if($line =~ /^Controllers found: (\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Controllers found: (\d+)$/) 0 0 0
155 0 $total_controller = $1;
156 }
157  
158 0 ▶ if($line =~ /^Controller information/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Controller information/) 0 0 0
159 0 $current_controller = $current_controller + 1;
160 0 $current_logical_drive = undef;
161 0 $current_physical_drive = undef;
162 0 $controller->{$current_controller} = {};
163 0 $ctrl = $controller->{$current_controller};
164  
165 0 ▶ while($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
166 0 chomp $line;
167  
168 0 ▶ if ($line =~ /^\s+(.*\w)\s+:\s+(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s+(.*\w)\s+:\s+(.*)$/) { } 0 0 0
elsif ($line =~ /^\s+-+$/) { } 0 0 0
169 0 $ctrl->{$1} = $2;
170 } elsif ($line =~ /^\s+-+$/) {
171 0 last;
172 }
173 }
174  
175 0 ▶ LEVEL2: while($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
176 0 ▶ if ($line =~ /^\s+-+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s+-+$/) 0 0 0
177 0 $line = <$fh>;
178 0 chomp $line;
179 }
180 0 ▶ if($line =~ /^\s+(.*\w)\s*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s+(.*\w)\s*/) 0 0 0
181 0 my $cat = $1;
182 0 $line = <$fh>;
183 0 ▶ LEVEL3: while($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
184 0 chomp $line;
185  
186 0 ▶ if ($line =~ /^\s+(.*\w)\s+:\s+(.*)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^\s+(.*\w)\s+:\s+(.*)$/) { } 0 0 0
elsif ($line =~ /^\s+-+$/) { } 0 0 0
elsif ($line =~ /^$/) { } 0 0 0
187 0 $ctrl->{$cat}{$1} = $2;
188 } elsif ($line =~ /^\s+-+$/) {
189 0 last LEVEL3;
190 } elsif ($line =~ /^$/) {
191 0 last LEVEL2;
192 }
193 }
194 }
195 }
196 }
197  
198 0 ▶ next if(!defined($current_controller));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current_controller 0 0 0
199  
200 0 ▶ if($line =~ /^Logical drive information/ or $line =~ /^Logical device information/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Logical drive information/ or $line =~ /^Logical device information/) 0 0 0
Condition0%0/3
$line =~ /^Logical drive information/ or $line =~ /^Logical device information/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^Logical drive information/ or $line =~ /^Logical device information/
$line =~ /^Logical drive information/ $line =~ /^Logical device information/
Truth table0%0/3
A or B
A= $line =~ /^Logical drive information/B= $line =~ /^Logical device information/
ABresult
1X1
011
000
201 0 ▶ LEVEL4: while($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
202 0 chomp $line;
203  
204 0 ▶ if ($line =~ /^\S+.*\w\s+(\d+)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^\S+.*\w\s+(\d+)$/) { } 0 0 0
elsif ($line =~ /^\s+(\S.*\S+)\s+:\s+(.*)$/) { } 0 0 0
elsif ($line =~ /^\s+-+$/) { } 0 0 0
205 0 $current_logical_drive = $1;
206 } elsif ($line =~ /^\s+(\S.*\S+)\s+:\s+(.*)$/) {
207 0 $ctrl->{'logical drive'}{$current_logical_drive}{$1} = $2;
208 } elsif ($line =~ /^\s+-+$/) {
209 0 my $cat = <$fh>;
210 0 $cat =~ s/^\s+(\S.*\S+)\s+/$1/;
211 0 chomp $cat;
212 0 ▶ LEVEL5: while($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
213 0 chomp $line;
214  
215 0 ▶ if ($line =~ /^\s+(\S.*\S+)\s+:\s+(.*)$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ /^\s+(\S.*\S+)\s+:\s+(.*)$/) { } 0 0 0
elsif ($line =~ /^\S+.*\w\s+(\d+)$/) { } 0 0 0
elsif ($line =~ /^-+$/) { } 0 0 0
elsif ($line =~ /^\s+-+$/) { } 0 0 0
216 0 $ctrl->{'logical drive'}{$current_logical_drive}{$cat}{$1} = $2;
217 } elsif ($line =~ /^\S+.*\w\s+(\d+)$/) {
218 0 $current_logical_drive = $1;
219 0 last LEVEL5;
220 } elsif ($line =~ /^-+$/) {
221 0 last LEVEL4;
222 } elsif ($line =~ /^\s+-+$/) {
223 0 next;
224 }
225 }
226 }
227 }
228 }
229  
230 0 ▶ if($line =~ /^Physical Device information/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Physical Device information/) 0 0 0
231  
232 0 ▶ LEVEL2: while($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
233 0 ▶ if ($line =~ /^\s+-+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s+-+$/) 0 0 0
234 0 $line = <$fh>;
235 0 chomp $line;
236 }
237 0 ▶ if ($line =~ /^\s+Device\s+#(\d+)$/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($line =~ /^\s+Device\s+#(\d+)$/) { } 0 0 0
elsif ($line =~ /^\s+Device is (.*\w)/) { } 0 0 0
elsif ($line =~ /^\s+(.*\w)\s+:\s+(.*)$/) { } 0 0 0
elsif ($line =~ /^\s+-+$/) { } 0 0 0
elsif ($line =~ /^$/) { } 0 0 0
238 0 $current_physical_drive = $1;
239 } elsif ($line =~ /^\s+Device is (.*\w)/) {
240 0 $ctrl->{'physical drive'}{$current_physical_drive}{'Device is'} = $1;
241 } elsif ($line =~ /^\s+(.*\w)\s+:\s+(.*)$/) {
242 0 $ctrl->{'physical drive'}{$current_physical_drive}{$1} = $2;
243 } elsif ($line =~ /^\s+-+$/) {
244 0 last LEVEL3;
245 } elsif ($line =~ /^$/) {
246 0 last LEVEL2;
247 }
248 }
249 }
250  
251 }
252
253 0 return $controller;
254 }
255  
256 =head1 AUTHOR
257  
258 Mathieu Alorent, C<< <kumy at cpan.org> >>
259  
260 =head1 BUGS
261  
262 Please report any bugs or feature requests to C<bug-parse-arcconf at rt.cpan.org>, or through
263 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Parse-Arcconf>. I will be notified, and then you'll
264 automatically be notified of progress on your bug as I make changes.
265  
266 =head1 SUPPORT
267  
268 You can find documentation for this module with the perldoc command.
269  
270 perldoc Parse::Arcconf
271  
272  
273 You can also look for information at:
274  
275 =over 4
276  
277 =item * Source code
278  
279 L<https://github.com/kumy/Parse-Arcconf>
280  
281 =item * RT: CPAN's request tracker
282  
283 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Parse-Arcconf>
284  
285 =item * AnnoCPAN: Annotated CPAN documentation
286  
287 L<http://annocpan.org/dist/Parse-Arcconf>
288  
289 =item * CPAN Ratings
290  
291 L<http://cpanratings.perl.org/d/Parse-Arcconf>
292  
293 =item * Search CPAN
294  
295 L<http://search.cpan.org/dist/Parse-Arcconf/>
296  
297 =back
298  
299  
300 =head1 ACKNOWLEDGEMENTS
301  
302  
303 =head1 LICENSE AND COPYRIGHT
304  
305 Copyright 2012 Mathieu Alorent.
306  
307 This program is free software; you can redistribute it and/or modify it
308 under the terms of either: the GNU General Public License as published
309 by the Free Software Foundation; or the Artistic License.
310  
311 See http://dev.perl.org/licenses/ for more information.
312  
313 This program is based on Parse::HP::ACU a work of Jeremy Cole.
314  
315  
316 =cut
317  
318 3 1; # End of Parse::Arcconf
↑ Summary