Skip to content

Commit

Permalink
rework code for testability purpose
Browse files Browse the repository at this point in the history
  • Loading branch information
guillomovitch committed Aug 6, 2012
1 parent 680b541 commit 5828492
Showing 1 changed file with 91 additions and 84 deletions.
175 changes: 91 additions & 84 deletions lib/FusionInventory/Agent/Task/Inventory/Input/Generic/Storages/HP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -55,103 +55,110 @@ sub doInventory {
my $inventory = $params{inventory};
my $logger = $params{logger};

my ($serialnumber, $model, $capacity, $firmware, $description, $media, $manufacturer);

my $hpacuacliPath = canRun('hpacucli') ?
my $path = canRun('hpacucli') ?
"hpacucli":
_getHpacuacliFromWinRegistry($logger);

my $handle1 = getFileHandle(
logger => $logger,
command => "$hpacuacliPath ctrl all show"
);
foreach my $slot (_getSlots(path => $path)) {
foreach my $drive (_getDrives(path => $path, slot => $slot)) {

return unless $handle1;
$inventory->addEntry(
section => 'STORAGES',
entry => _getStorage(
path => $path, slot => $slot, drive => $drive
)
);
}
}
}

# Example output :
#
# Smart Array E200 in Slot 2 (sn: PA6C90K9SUH1ZA)
while (my $line1 = <$handle1>) {
next unless $line1 =~ /Slot\s(\d*)/;
sub _getSlots {
my %params = @_;

my $slot = $1;
my $handle2 = getFileHandle(
logger => $logger,
command => "$hpacuacliPath ctrl slot=$slot pd all show"
);
next unless $handle2;
my $command = $params{path} ?
"%params{path} ctrl all show" : undef;
my $handle = getFileHandle(%params, command => $command);
return unless $handle;

# Example output :
#
# Smart Array E200 in Slot 2
#
# array A
#
# physicaldrive 2I:1:1 (port 2I:box 1:bay 1, SATA, 74.3 GB, OK)
# physicaldrive 2I:1:2 (port 2I:box 1:bay 2, SATA, 74.3 GB, OK)
while (my $line2 = <$handle2>) {
next unless $line2 =~ /physicaldrive\s(\S*)/;

my $pd = $1;
my $handle3 = getFileHandle(
logger => $logger,
command => "$hpacuacliPath ctrl slot=$slot pd $pd show"
);
next unless $handle3;
my @slots;
while (my $line = <$handle>) {
next unless $line =~ /Slot\s(\d*)/;
push @slots, $1;
}
close $handle;

# Example output :
#
# Smart Array E200 in Slot 2
#
# array A
#
# physicaldrive 1:1
# Port: 2I
# Box: 1
# Bay: 1
# Status: OK
# Drive Type: Data Drive
# Interface Type: SATA
# Size: 74.3 GB
# Firmware Revision: 21.07QR4
# Serial Number: WD-WMANS1732855
# Model: ATA WDC WD740ADFD-00
# SATA NCQ Capable: False
# PHY Count: 1
while (my $line3 = <$handle3>) {
$model = $1 if $line3 =~ /Model:\s(.*)/;
$description = $1 if $line3 =~ /Interface Type:\s(.*)/;
$media = $1 if $line3 =~ /Drive Type:\s(.*)/;
$capacity = 1000*$1 if $line3 =~ /Size:\s(\S*)/;
$serialnumber = $1 if $line3 =~ /Serial Number:\s(.*)/;
$firmware = $1 if $line3 =~ /Firmware Revision:\s(.*)/;
}
close $handle3;
$serialnumber =~ s/^\s+//;
return @slots;
}

sub _getDrives {
my %params = @_;

my $command = $params{path} && $params{slot} ?
"%params{path} ctrl slot=$params{slot} pd all show" : undef;
my $handle = getFileHandle(%params, command => $command);
next unless $handle;

my @drives;
while (my $line = <$handle>) {
next unless $line =~ /physicaldrive\s(\S*)/;
push @drives, $1;
}
close $handle;

return @drives;
}

sub _getStorage {
my %params = @_;

my $command = $params{path} && $params{slot} && $params{drive} ?
"%params{path} ctrl slot=$params{slot} pd $params{drive} show" : undef;
my $handle = getFileHandle(%params, command => $command);
next unless $handle;

my $storage;
while (my $line = <$handle>) {
if ($line =~ /Model:\s(.*)/) {
my $model = $1;
$model =~ s/^ATA\s+//; # ex: ATA WDC WD740ADFD-00
$model =~ s/\s+/ /;
$manufacturer = getCanonicalManufacturer($model);
if ($media eq 'Data Drive') {
$media = 'disk';
}
$storage->{NAME} = $model;
$storage->{MODEL} = $model;
next;
}

$inventory->addEntry(
section => 'STORAGES',
entry => {
NAME => $model,
MANUFACTURER => $manufacturer,
MODEL => $model,
DESCRIPTION => $description,
TYPE => $media,
DISKSIZE => $capacity,
SERIALNUMBER => $serialnumber,
FIRMWARE => $firmware
}
);
if ($line =~ /Interface Type:\s(.*)/) {
$storage->{DESCRIPTION} = $1;
next;
}

if ($line =~ /Drive Type:\s(.*)/) {
$storage->{TYPE} = $1 eq 'Data Drive' ? 'disk' : $1;
next;
}

if ($line =~ /Size:\s(\S*)/) {
$storage->{DISKSIZE} = 1000 * $1;
next;
}

if ($line =~ /Serial Number:\s(.*)/) {
my $serialnumber = $1;
$serialnumber =~ s/^\s+//;
$storage->{SERIALNUMBER} = $serialnumber;
next;
}

if ($line =~ /Firmware Revision:\s(.*)/) {
$storage->{FIRMWARE} = $1;
next;
}
close $handle2;
}
close $handle1;
close $handle;

$storage->{MANUFACTURER} = getCanonicalManufacturer($storage->{MODEL});

return $storage;
}

1;

0 comments on commit 5828492

Please sign in to comment.