File Coverage

blib/lib/FusionInventory/Agent/Task/Inventory/Generic/Storages/3ware.pm
Criterion Covered Total %
statement 57 73 78.0
branch 19 32 59.3
condition 0 3 0.0
subroutine 9 11 81.8
pod 0 2 0.0
total 85 121 70.2


line stmt bran cond sub pod time code
1             package FusionInventory::Agent::Task::Inventory::Generic::Storages::3ware;
2              
3 2     2   157242929 use strict;
  2         6  
  2         124  
4 2     2   13 use warnings;
  2         4  
  2         109  
5              
6 2     2   908 use FusionInventory::Agent::Tools;
  2         3  
  2         453  
7 2     2   1351 use FusionInventory::Agent::Tools::Linux;
  2         7  
  2         212  
8              
9 2     2   16 use English qw(-no_match_vars);
  2         4  
  2         17  
10              
11             # Tested on 2.6.* kernels
12             #
13             # Cards tested :
14             #
15             # 8006-2LP
16             # 9500S-4LP
17             # 9550SXU-4LP
18             # 9550SXU-8LP
19             # 9650SE-2LP
20             # 9650SE-4LPML
21             # 9650SE-8LPML
22             #
23             # AMCC/3ware CLI (version 2.00.0X.XXX)
24              
25             sub isEnabled {
26 0     0 0 0 return canRun('tw_cli');
27             }
28              
29             sub doInventory {
30 0     0 0 0 my (%params) = @_;
31              
32 0         0 my $inventory = $params{inventory};
33 0         0 my $logger = $params{logger};
34              
35 0         0 my @devices;
36              
37 0         0 foreach my $card (_getCards()) {
38 0         0 foreach my $unit (_getUnits($card)) {
39              
40             # Try do get unit's serial in order to compare it to what was found
41             # in udev db.
42             # Works only on newer cards.
43             # Allow us to associate a node to a drive : sda -> WD-WMANS1648590
44 0         0 my $sn = getFirstMatch(
45             logger => $logger,
46             command => "tw_cli info $card->{id} $unit->{id} serial",
47             pattern => qr/serial number\s=\s(\w+)/
48             );
49              
50 0         0 foreach my $port (_getPorts($card, $unit)) {
51             # Finally, getting drives' values.
52 0         0 my $storage = _getStorage($card, $port);
53              
54 0 0       0 if ($OSNAME eq 'Linux') {
55              
56 0 0       0 @devices = getDevicesFromUdev(logger => $logger) unless @devices;
57              
58 0         0 foreach my $device (@devices) {
59             # How does this work with multiple older cards
60             # where serial for units is not implemented ?
61             # Need to be tested on a system with multiple
62             # 3ware cards.
63 0 0 0     0 if (
64             $device->{SERIALNUMBER} eq 'AMCC_' . $sn ||
65             $device->{MODEL} eq 'Logical_Disk_' . $unit->{index}
66             ) {
67 0         0 $storage->{NAME} = $device->{NAME};
68             }
69             }
70             }
71              
72 0         0 $inventory->addEntry(section => 'STORAGES', entry => $storage);
73             }
74             }
75             }
76             }
77              
78              
79             sub _getCards {
80 1     1   8 my ($file) = @_;
81              
82 1         7 my $handle = getFileHandle(
83             file => $file,
84             command => "tw_cli info"
85             );
86 1 50       4 return unless $handle;
87              
88 1         2 my @cards;
89 1         13 while (my $line = <$handle>) {
90 3 100       16 next unless $line =~ /^(c\d+)\s+([\w-]+)/;
91 1         12 push @cards, { id => $1, model => $2 };
92             }
93 1         7 close $handle;
94              
95 1         10 return @cards;
96             }
97              
98             sub _getUnits {
99 1     1   8 my ($card, $file) = @_;
100              
101 1         18 my $handle = getFileHandle(
102             file => $file,
103             command => "tw_cli info $card->{id}"
104             );
105 1 50       11 return unless $handle;
106              
107 1         5 my @units;
108 1         61 while (my $line = <$handle>) {
109 3 100       31 next unless $line =~ /^(u(\d+))/;
110 1         24 push @units, { id => $1, index => $2 };
111             }
112 1         43 close $handle;
113              
114 1         16 return @units;
115             }
116              
117             sub _getPorts {
118 1     1   6 my ($card, $unit, $file) = @_;
119              
120 1         11 my $handle = getFileHandle(
121             file => $file,
122             command => "tw_cli info $card->{id} $unit->{id}"
123             );
124 1 50       9 return unless $handle;
125              
126 1         3 my @ports;
127 1         24 while (my $line = <$handle>) {
128 4 100       41 next unless $line =~ /(p\d+)/;
129 2         24 push @ports, { id => $1 };
130             }
131 1         14 close $handle;
132              
133 1         12 return @ports;
134             }
135              
136             sub _getStorage {
137 1     1   5 my ($card, $port, $file) = @_;
138              
139 1         13 my $handle = getFileHandle(
140             file => $file,
141             command =>
142             "tw_cli info $card->{id} $port->{id} model serial capacity firmware"
143             );
144 1 50       8 return unless $handle;
145              
146 1         4 my $storage;
147 1         35 while (my $line = <$handle>) {
148 4 100       95 if ($line =~ /Model\s=\s(.*)/) {
    100          
    100          
    50          
149 1         11 $storage->{MODEL} = $1;
150             } elsif ($line =~ /Serial\s=\s(.*)/) {
151 1         8 $storage->{SERIALNUMBER} = $1;
152             } elsif ($line =~ /Capacity\s=\s(\S+)\sGB.*/) {
153 1         10 $storage->{DISKSIZE} = 1024 * $1;
154             } elsif ($line =~ /Firmware Version\s=\s(.*)/) {
155 1         9 $storage->{FIRMWARE} = $1
156             }
157             }
158 1         7 close $handle;
159              
160             $storage->{MANUFACTURER} = getCanonicalManufacturer(
161             $storage->{MODEL}
162 1         7 );
163 1         3 $storage->{TYPE} = 'disk';
164              
165             # Getting description from card model, very basic
166             # and unreliable
167             # Assuming only IDE drives can be plugged in
168             # 5xxx/6xxx cards and
169             # SATA drives only to 7xxx/8xxx/9xxxx cards
170             $storage->{DESCRIPTION} =
171             $card->{model} =~ /^[56]/ ? 'IDE' :
172 1 50       8 $card->{model} =~ /^[789]/ ? 'SATA' :
    50          
173             undef;
174              
175 1         7 return $storage;
176             }
177              
178             1;