|
|
@@ -18,7 +18,6 @@ Disk::SMART - Provides an interface to smartctl
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
Disk::SMART is an object ooriented module that provides an interface to get SMART disk info from a device as well as initiate testing.
|
|
|
-
|
|
|
use Disk::SMART;
|
|
|
|
|
|
my $smart = Disk::SMART->new('/dev/sda');
|
|
|
@@ -32,31 +31,38 @@ Disk::SMART is an object ooriented module that provides an interface to get SMAR
|
|
|
|
|
|
Instantiates the Disk::SMART object
|
|
|
|
|
|
-C<DEVICE> - Device identifier of SSD / Hard Drive
|
|
|
+C<DEVICE> - Device identifier of SSD / Hard Drive. The constructor takes either a single device name, or an array of device names.
|
|
|
|
|
|
my $smart = Disk::SMART->new( 'dev/sda', '/dev/sdb' );
|
|
|
|
|
|
-Returns C<Disk::SMART> object if smartctl is available and can poll the given device.
|
|
|
+Returns C<Disk::SMART> object if smartctl is available and can poll the given device(s).
|
|
|
|
|
|
=cut
|
|
|
|
|
|
sub new {
|
|
|
my ( $class, @devices ) = @_;
|
|
|
my $self = bless {}, $class;
|
|
|
+ my $test_data;
|
|
|
|
|
|
- croak "Valid device identifier not supplied to constructor for $class.\n" if ( !@devices );
|
|
|
- croak "smartctl binary was not found on your system, are you running as root?\n" if !-f $smartctl;
|
|
|
+ if ( $ENV{'TEST_MOCK_DATA'} ) {
|
|
|
+ $test_data = pop @devices;
|
|
|
+ }
|
|
|
+
|
|
|
+ croak "Valid device identifier not supplied to constructor for $class.\n"
|
|
|
+ if !@devices && !$ENV{'TEST_MOCK_DATA'};
|
|
|
+ croak "smartctl binary was not found on your system, are you running as root?\n"
|
|
|
+ if !-f $smartctl && !$ENV{'TEST_MOCK_DATA'};
|
|
|
|
|
|
foreach my $device (@devices) {
|
|
|
- $self->update_data($device);
|
|
|
+ ($test_data) ? $self->update_data($device, $test_data) : $self->update_data($device);
|
|
|
}
|
|
|
+
|
|
|
return $self;
|
|
|
}
|
|
|
|
|
|
|
|
|
=head1 USER METHODS
|
|
|
|
|
|
-
|
|
|
=head2 B<get_disk_attributes(DEVICE)>
|
|
|
|
|
|
Returns hash of the SMART disk attributes and values
|
|
|
@@ -69,6 +75,7 @@ C<DEVICE> - Device identifier of SSD/ Hard Drive
|
|
|
|
|
|
sub get_disk_attributes {
|
|
|
my ( $self, $device ) = @_;
|
|
|
+ $self->_validate_param($device);
|
|
|
return $self->{'devices'}->{$device}->{'attributes'};
|
|
|
}
|
|
|
|
|
|
@@ -85,6 +92,7 @@ C<DEVICE> - Device identifier of SSD/ Hard Drive
|
|
|
|
|
|
sub get_disk_errors {
|
|
|
my ( $self, $device ) = @_;
|
|
|
+ $self->_validate_param($device);
|
|
|
return $self->{'devices'}->{$device}->{'errors'};
|
|
|
}
|
|
|
|
|
|
@@ -101,6 +109,7 @@ C<DEVICE> - Device identifier of SSD / Hard Drive
|
|
|
|
|
|
sub get_disk_health {
|
|
|
my ( $self, $device ) = @_;
|
|
|
+ $self->_validate_param($device);
|
|
|
return $self->{'devices'}->{$device}->{'health'};
|
|
|
}
|
|
|
|
|
|
@@ -117,6 +126,7 @@ C<DEVICE> - Device identifier of SSD / Hard Drive
|
|
|
|
|
|
sub get_disk_model {
|
|
|
my ( $self, $device ) = @_;
|
|
|
+ $self->_validate_param($device);
|
|
|
return $self->{'devices'}->{$device}->{'model'};
|
|
|
}
|
|
|
|
|
|
@@ -133,6 +143,7 @@ C<DEVICE> - Device identifier of SSD / Hard Drive
|
|
|
|
|
|
sub get_disk_temp {
|
|
|
my ( $self, $device ) = @_;
|
|
|
+ $self->_validate_param($device);
|
|
|
return @{ $self->{'devices'}->{$device}->{'temp'} };
|
|
|
}
|
|
|
|
|
|
@@ -148,16 +159,24 @@ C<DEVICE> - Device identifier of SSD/ Hard Drive
|
|
|
=cut
|
|
|
|
|
|
sub update_data {
|
|
|
- my ( $self, $device ) = @_;
|
|
|
+ my ( $self, $device, $test_data ) = @_;
|
|
|
+ my $out = $test_data // undef;
|
|
|
+
|
|
|
+ $out = qx($smartctl -a $device) if !defined $test_data;
|
|
|
+ my $retval = $?;
|
|
|
+
|
|
|
+ if ( !$test_data ) {
|
|
|
+ croak "Smartctl couldn't poll device $device\n"
|
|
|
+ if ( $out !~ /START OF INFORMATION SECTION/ );
|
|
|
+ }
|
|
|
|
|
|
- chomp( my $out = qx($smartctl -a $device) );
|
|
|
- croak "Smartctl couldn't poll device $device\n" if $out =~ /No such device/;
|
|
|
+ chomp($out);
|
|
|
$self->{'devices'}->{$device}->{'SMART_OUTPUT'} = $out;
|
|
|
|
|
|
# update_data() can be called at any time with a device name. Let's check
|
|
|
# the device name given to make sure it matches what was given during
|
|
|
# object construction.
|
|
|
- croak "$device not found in object, You probably didn't enter it right" if ( !exists $self->{'devices'}->{$device} );
|
|
|
+ $self->_validate_param($device);
|
|
|
|
|
|
$self->_process_disk_attributes($device);
|
|
|
$self->_process_disk_errors($device);
|
|
|
@@ -183,6 +202,7 @@ sub _process_disk_attributes {
|
|
|
$value =~ s/^\s+//g; # trim beginning and ending whitepace
|
|
|
$self->{'devices'}->{$device}->{'attributes'}->{$name} = $value;
|
|
|
}
|
|
|
+
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -192,74 +212,62 @@ sub _process_disk_errors {
|
|
|
|
|
|
my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
|
|
|
my ($errors) = $smart_output =~ /SMART Error Log Version: [1-9](.*)SMART Self-test log/s;
|
|
|
-
|
|
|
- if ( !defined $errors ) {
|
|
|
- $self->{'devices'}->{$device}->{'errors'} = 'N/A';
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
$errors =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
|
|
|
- $self->{'devices'}->{$device}->{'errors'} = $errors;
|
|
|
- return;
|
|
|
+ $errors = 'N/A' if !$errors;
|
|
|
+
|
|
|
+ return $self->{'devices'}->{$device}->{'errors'} = $errors;
|
|
|
}
|
|
|
|
|
|
sub _process_disk_health {
|
|
|
my ( $self, $device ) = @_;
|
|
|
$self->_validate_param($device);
|
|
|
- my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
|
|
|
- my ($health) = $smart_output =~ /(SMART overall-health self-assessment.*\n)/;
|
|
|
|
|
|
- if ( (!defined $health) or $health !~ /PASSED|FAILED/x ) {
|
|
|
- $self->{'devices'}->{$device}->{'health'} = 'N/A';
|
|
|
- return;
|
|
|
- }
|
|
|
+ my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
|
|
|
+ my ($health) = $smart_output =~ /SMART overall-health self-assessment test result:(.*)\n/;
|
|
|
+ $health =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
|
|
|
+ $health = 'N/A' if !$health or $health !~ /PASSED|FAILED/x;
|
|
|
|
|
|
- $health =~ s/.*: //;
|
|
|
- chomp $health;
|
|
|
- $self->{'devices'}->{$device}->{'health'} = $health;
|
|
|
- return;
|
|
|
+ return $self->{'devices'}->{$device}->{'health'} = $health;
|
|
|
}
|
|
|
|
|
|
sub _process_disk_model {
|
|
|
my ( $self, $device ) = @_;
|
|
|
$self->_validate_param($device);
|
|
|
+
|
|
|
my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
|
|
|
- my ($model) = $smart_output =~ /(Device\ Model.*\n)/;
|
|
|
-
|
|
|
- if ( !defined $model ) {
|
|
|
- $self->{'devices'}->{$device}->{'model'} = 'N/A';
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- $model =~ s/.*:\ //;
|
|
|
+ my ($model) = $smart_output =~ /Device\ Model:(.*)\n/;
|
|
|
$model =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
|
|
|
- $self->{'devices'}->{$device}->{'model'} = $model;
|
|
|
- return;
|
|
|
+ $model = 'N/A' if !$model;
|
|
|
+
|
|
|
+ return $self->{'devices'}->{$device}->{'model'} = $model;
|
|
|
}
|
|
|
|
|
|
sub _process_disk_temp {
|
|
|
my ( $self, $device ) = @_;
|
|
|
$self->_validate_param($device);
|
|
|
- my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
|
|
|
- my ($temp_c) = $smart_output =~ /(Temperature_Celsius.*\n)/;
|
|
|
+ my ( $temp_c, $temp_f );
|
|
|
|
|
|
- if ( !defined $temp_c || $smart_output =~ qr/S.M.A.R.T. not available/x ) {
|
|
|
- $self->{'devices'}->{$device}->{'temp'} = [( 'N/A' )];
|
|
|
- return;
|
|
|
+ my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
|
|
|
+ ($temp_c) = $smart_output =~ /(Temperature_Celsius.*\n)/;
|
|
|
+
|
|
|
+ if ($temp_c) {
|
|
|
+ $temp_c = substr $temp_c, 83, +3;
|
|
|
+ $temp_c =~ s/^\s+|\s+$//g; #trim beginning and ending whitepace
|
|
|
+ $temp_f = round( ( $temp_c * 9 ) / 5 + 32 );
|
|
|
+ $temp_c = int $temp_c;
|
|
|
+ $temp_f = int $temp_f;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $temp_c = 'N/A';
|
|
|
+ $temp_f = 'N/A';
|
|
|
}
|
|
|
|
|
|
- chomp($temp_c);
|
|
|
- $temp_c = substr $temp_c, 83, +3;
|
|
|
- $temp_c =~ s/ //g;
|
|
|
-
|
|
|
- my $temp_f = round( ( $temp_c * 9 ) / 5 + 32 );
|
|
|
- $self->{'devices'}->{$device}->{'temp'} = [ ( int $temp_c, int $temp_f ) ];
|
|
|
- return;
|
|
|
+ return $self->{'devices'}->{$device}->{'temp'} = [ ( $temp_c, $temp_f ) ];
|
|
|
}
|
|
|
|
|
|
sub _validate_param {
|
|
|
my ( $self, $device ) = @_;
|
|
|
- croak "$device not found in object, You probably didn't enter it right" if ( !exists $self->{'devices'}->{$device} );
|
|
|
+ croak "$device not found in object, you probably didn't enter it right" if ( !exists $self->{'devices'}->{$device} );
|
|
|
return;
|
|
|
}
|
|
|
|