StorScore/lib/SmartCtlParser.pm

174 строки
4.3 KiB
Perl

# StorScore
#
# Copyright (c) Microsoft Corporation
#
# All rights reserved.
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
package SmartCtlParser;
use strict;
use warnings;
use Moose;
use Util;
use DeviceDB;
my @extract_rules_smart_info =
(
{
match => qr/Model Family:\s+(.+)/,
store => 'Model Family'
},
{
match => qr/Device Model:\s+(.+)/,
store => 'Device Model'
},
{
match => qr/Serial Number:\s+(.+)/,
store => 'Serial Number'
},
{
match => qr/Firmware Version:\s+(.+)/,
store => 'Firmware Version'
},
{
match => qr/User Capacity:\s+([\d,]+) bytes/,
store => 'User Capacity (B)'
},
{
match => qr/SATA Version is:\s+(.+),/,
store => 'Protocol Version'
},
);
sub post_process($)
{
my $stats_ref = shift;
# Remove commas: 123,456,789 --> 123456789
$stats_ref->{'User Capacity (B)'} =~ s/,//g;
$stats_ref->{'User Capacity (GB)'} =
int( $stats_ref->{'User Capacity (B)'} / BYTES_PER_GB_BASE10 );
}
# Parse the "information section" as generated by smartctl.exe -i
sub parse_info($$)
{
my $self = shift;
my $stats_ref = shift;
my $file_name = shift;
open my $FILE, '<', $file_name
or die "Error opening $file_name";
#or die "Error opening smart.txt";
while( my $line = <$FILE> )
{
if( $line =~ /START OF INFORMATION SECTION/i )
{
<$FILE>; # skip header line
last;
}
}
while( my $line = <$FILE> )
{
do_simple_extract( $line, $stats_ref, \@extract_rules_smart_info );
last if( $line =~ /START OF ENABLE/i );
}
post_process( $stats_ref );
close $FILE;
}
# Parse the "attributes" generated by smartctl.exe -A
sub parse_attributes($$$)
{
my $self = shift;
my $file_name = shift;
my $suffix = shift;
my $stats_ref = shift;
return 0 unless -e $file_name;
open( my $LOG, '<', "$file_name" ) or do
{
warn "Error opening $file_name\n";
return;
};
my $ddb_ref = $device_db{ $stats_ref->{'Device Model'} };
unless( defined $ddb_ref )
{
warn "\tNo entry in DeviceDB. Will not parse SMART attributes.\n";
return 0;
}
my $host_writes = $ddb_ref->{'Host Writes'}{'Attribute'} // -1;
my $ctrl_writes = $ddb_ref->{'Controller Writes'}{'Attribute'} // -1;
my $wear_range = $ddb_ref->{'Wear Range Attribute'} // -1;
while( my $line = <$LOG> )
{
if( $line =~ /Vendor Specific SMART Attributes/ )
{
<$LOG>; # skip header line
last;
}
}
while( my $line = <$LOG> )
{
last if $line =~ /^$/;
my @fields = split( ' ', $line );
my $attribute_id = $fields[0];
my $raw_value = $fields[9];
if( $attribute_id == $host_writes )
{
$stats_ref->{"Host Writes$suffix"} = $raw_value;
}
elsif( $attribute_id == $ctrl_writes )
{
$stats_ref->{"Controller Writes$suffix"} = $raw_value;
}
elsif( $attribute_id == $wear_range )
{
$stats_ref->{"Wear Range$suffix"} = $raw_value;
}
}
close $LOG;
}
no Moose;
__PACKAGE__->meta->make_immutable;
1;