Skip to content

Commit

Permalink
Updated FusionInventory::Agent::Tools::Linux::getInfoFromSmartctl() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
po1vo committed Oct 6, 2020
1 parent 41dfe66 commit e8ae273
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 32 deletions.
8 changes: 7 additions & 1 deletion lib/FusionInventory/Agent/Tools.pm
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,13 @@ sub getCanonicalSize {
return undef unless $size =~ /^([,.\d]+) (\S+)$/x;
my $value = $1;
my $unit = lc($2);
$value =~ s/,/\./;

# check if the number has groups separated by commas or spaces
if ($value =~ /^(0|[1-9](\d*|\d{0,2}([, ]\d{3})+))(\.\d+)?$/) {
$value =~ s/,| //g;
} else {
$value =~ s/,/\./;
}

return
$unit eq 'tb' ? $value * $base * $base :
Expand Down
80 changes: 49 additions & 31 deletions lib/FusionInventory/Agent/Tools/Linux.pm
Original file line number Diff line number Diff line change
Expand Up @@ -385,54 +385,72 @@ sub _readLinkFromSysFs {
sub getInfoFromSmartctl {
my (%params) = @_;

my $handle = getFileHandle(
my @lines = getAllLines(
%params,
command => $params{device} ? "smartctl -i $params{device}" : undef,
command => $params{device} ?
"smartctl -i $params{device} " . ($params{extra} // "") : undef,
);
return unless $handle;
return unless @lines;

my $info = {
TYPE => 'disk',
DESCRIPTION => 'SATA',
};

while (my $line = <$handle>) {
if ($line =~ /^Vendor: +(\S+)/i) {
$info->{MANUFACTURER} = getCanonicalManufacturer($1);
next;
my $attrs = {
MANUFACTURER => {
src => ['vendor', 'model family', 'add. product id', 'device model', 'product'],
func => \&getCanonicalManufacturer,
args => undef
},
MODEL => {
src => ['product', 'device model', 'model family']
},
FIRMWARE => {
src => ['revision', 'firmware version']
},
DISKSIZE => {
src => ['user capacity'],
func => \&getCanonicalSize,
args => [1024]
},
DESCRIPTION => {
src => ['transport protocol']
},
TYPE => {
src => ['device type']
},
SERIALNUMBER => {
src => ['serial number']
}
};

if ($line =~ /^Product: +(\S+)/i) {
$info->{MODEL} = $1;
next;
}
my $regexp = {
__default => qr/^(\w+)/,
__smartctl => qr/^([^:]+?)\s*:\s*(.+)\s*$/,
'user capacity' => qr/([\d\.\,\s]+(?:\w+)?)/,
'device model' => qr/([\w\s]+)/,
};

if ($line =~ /^Revision: +(\S+)/i) {
$info->{FIRMWARE} = $1;
next;
}
my %smartctl;

if ($line =~ /^User Capacity: +(\S.+\S)/i) {
$info->{DISKSIZE} = getCanonicalSize($1, 1024);
next;
}
for my $line (@lines) {
next unless $line =~ $regexp->{__smartctl};
$smartctl{lc $1} = $2;
}

if ($line =~ /^Transport protocol: +(\S+)/i) {
$info->{DESCRIPTION} = $1;
next;
}
while (my ($attr, $val) = each %$attrs) {
for my $s (@{$val->{src}}) {
next unless defined $smartctl{$s};

if ($line =~ /^Device type: +(\S+)/i) {
$info->{TYPE} = $1;
next;
}
my ($data) = ($smartctl{$s} =~ ($regexp->{$s} // $regexp->{__default}));

if ($line =~ /^Serial number: +(\S+)/i) {
$info->{SERIALNUMBER} = $1;
next;
$info->{$attr} = exists $val->{func} ?
&{$val->{func}}($data, @{$val->{args}}) : $data;

last;
}
}
close $handle;

return $info;
}
Expand Down
21 changes: 21 additions & 0 deletions resources/linux/smartctl/sample5
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
smartctl 7.0 2018-12-30 r4883 [x86_64-linux-4.14.83-0.1-default] (SUSE RPM)
Copyright (C) 2002-18, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===
Model Family: Micron 5100 Pro / 5200 SSDs
Device Model: MTFDDAK480TDC
Serial Number: 202028413FA9
LU WWN Device Id: 5 00a075 128413fa9
Add. Product Id: DELL(tm)
Firmware Version: D1DF003
User Capacity: 480,103,981,056 bytes [480 GB]
Sector Sizes: 512 bytes logical, 4096 bytes physical
Rotation Rate: Solid State Device
Form Factor: 2.5 inches
Device is: In smartctl database [for details use: -P show]
ATA Version is: ACS-3 T13/2161-D revision 5
SATA Version is: SATA 3.2, 6.0 Gb/s (current: 6.0 Gb/s)
Local Time is: Wed Sep 30 08:30:21 2020 UTC
SMART support is: Available - device has SMART capability.
SMART support is: Enabled

21 changes: 21 additions & 0 deletions resources/linux/smartctl/sample6
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
smartctl 7.0 2018-12-30 r4883 [x86_64-linux-4.14.83-0.1-default] (SUSE RPM)
Copyright (C) 2002-18, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===
Vendor: HP
Product: EG0600FBDSR
Revision: HPD2
Compliance: SPC-3
User Capacity: 600,127,266,816 bytes [600 GB]
Logical block size: 512 bytes
Rotation Rate: 10025 rpm
Form Factor: 2.5 inches
Logical Unit id: 0x5000039308183dd4
Serial number: EA01PB107H5K1103
Device type: disk
Transport protocol: SAS (SPL-3)
Local Time is: Wed Sep 30 08:31:22 2020 UTC
SMART support is: Available - device has SMART capability.
SMART support is: Enabled
Temperature Warning: Enabled

25 changes: 25 additions & 0 deletions resources/linux/smartctl/sample7
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
smartctl 7.0 2018-12-30 r4883 [x86_64-linux-4.19.126-0.1-default] (SUSE RPM)
Copyright (C) 2002-18, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===
Vendor: HGST
Product: HUH721212AL5200
Revision: NS07
Compliance: SPC-4
User Capacity: 11,756,399,230,976 bytes [11.7 TB]
Logical block size: 512 bytes
Physical block size: 4096 bytes
Formatted with type 2 protection
8 bytes of protection information per logical block
LU is fully provisioned
Rotation Rate: 7200 rpm
Form Factor: 3.5 inches
Logical Unit id: 0x5000cca2940e56b8
Serial number: D5G7WG8L
Device type: disk
Transport protocol: SAS (SPL-3)
Local Time is: Wed Sep 30 08:34:39 2020 UTC
SMART support is: Available - device has SMART capability.
SMART support is: Enabled
Temperature Warning: Disabled or Not Supported

8 changes: 8 additions & 0 deletions t/agent/tools.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ my @size_tests_ok = (
[ '1' , 1 ],
[ '1 mb' , 1 ],
[ '1.1 mb' , 1.1 ],
[ '11,11 mb' , 11.11 ],
[ '1.111 mb' , 1.111 ],
[ '1,111 mb' , 1111 ],
[ '1 MB' , 1 ],
[ '1,000 MB' , 1000 ],
[ '1,000.9 MB' , 1000.9 ],
[ '1,000,000.9 MB' , 1000000.9 ],
[ '1 gb' , 1000 ],
[ '1 GB' , 1000 ],
[ '1 tb' , 1000000 ],
[ '1 TB' , 1000000 ],
[ '128 035 676 160 bytes' , 128035 ],
[ '600,127,266,816 bytes' , 600127 ],
);

my @size_1024_tests_ok = (
Expand All @@ -37,6 +44,7 @@ my @size_1024_tests_ok = (
[ '1 tb' , 1048576 ],
[ '1 TB' , 1048576 ],
[ '128 035 676 160 bytes' , 122104 ],
[ '600,127,266,816 bytes' , 572325 ],
);

my @speed_tests_nok = (
Expand Down
30 changes: 30 additions & 0 deletions t/agent/tools/linux.t
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ my %smartctl_tests = (
DESCRIPTION => 'SATA',
SERIALNUMBER => 'DFW1W11002SE002B3117',
DISKSIZE => 122104,
MANUFACTURER => 'Samsung',
FIRMWARE => 'VBM24D1Q',
MODEL => 'SAMSUNG SSD PM800 TM 128GB',
},
sample2 => {
TYPE => 'disk',
Expand All @@ -587,6 +590,33 @@ my %smartctl_tests = (
TYPE => 'enclosure',
DESCRIPTION => 'SATA',
},
sample5 => {
DESCRIPTION => 'SATA',
TYPE => 'disk',
DISKSIZE => 457862,
MANUFACTURER => 'Micron',
FIRMWARE => 'D1DF003',
MODEL => 'MTFDDAK480TDC',
SERIALNUMBER => '202028413FA9'
},
sample6 => {
MANUFACTURER => 'Hewlett-Packard',
DESCRIPTION => 'SAS',
SERIALNUMBER => 'EA01PB107H5K1103',
MODEL => 'EG0600FBDSR',
DISKSIZE => 572325,
FIRMWARE => 'HPD2',
TYPE => 'disk'
},
sample7 => {
TYPE => 'disk',
DISKSIZE => 11211776,
MODEL => 'HUH721212AL5200',
SERIALNUMBER => 'D5G7WG8L',
DESCRIPTION => 'SAS',
FIRMWARE => 'NS07',
MANUFACTURER => 'Hitachi'
},
);

my %ifconfig_tests = (
Expand Down

0 comments on commit e8ae273

Please sign in to comment.