Skip to content

Commit

Permalink
Add SCSI SENSE information to detect if the drive is in a low power s…
Browse files Browse the repository at this point in the history
…tate, and avoid waking it
  • Loading branch information
nickcmaynard authored and steveschnepp committed Aug 11, 2022
1 parent ea54c42 commit 9e0a09f
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions plugins/node.d/hddtemp_smartctl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ if ($smartctl and `$smartctl --version` =~ / release (\d+\.\d+) /i) {
my $hdparm = `sh -c 'command -v hdparm'`;
chomp $hdparm;

# sdparm is used for checking SENSE data
my $sdparm = `sh -c 'command -v sdparm'`;
chomp $sdparm;

my @drives;

# Try to get a default set of drives
Expand Down Expand Up @@ -235,6 +239,9 @@ if (defined $ARGV[0]) {
foreach my $drive (@drives) {
warn "[DEBUG] Processing $drive\n" if $DEBUG;
my $fulldev = device_for_drive($drive);

# Don't wake up SCSI drives!
next if (check_scsi_low_power($fulldev));

# Fall back to using hdparm for detecting disks in stand-by only if nocheck
# isn't supported (hdparm isn't available on all platforms).
Expand Down Expand Up @@ -330,6 +337,9 @@ sub command_for_drive_device {
sub get_drive_id {
my ($drive, $fulldev, $use_nocheck) = @_;

# Don't wake up SCSI drives!
return '' if (check_scsi_low_power($fulldev));

my $cmd = $smartctl.' -i ';
$cmd .= '--nocheck=standby ' if $use_nocheck;
$cmd .= $fulldev;
Expand Down Expand Up @@ -358,3 +368,35 @@ sub get_drive_id {

return $id;
}


sub check_scsi_low_power {
my ($fulldev) = @_;

# If present, use sdparm to check the SENSE data to see if the drive is in low power mode
# This catches SCSI drives' low power modes, per https://www.t10.org/lists/asc-num.htm#ASC_5E

if ($sdparm) {
my @lines = `$sdparm --readonly --command=sense --hex $fulldev 2> /dev/null`;
# sdparm error cases (ie. ATA drives) should return 0, to be checked in other ways
return 0 if ($? != 0);

# get hex bytes
my @sensehexbytes = ();
foreach my $line(@lines) {
# strip off leading line/word indicators
$line =~ s/^\s+\d+\s+//;
# add hex bytes to our array
push(@sensehexbytes, $line =~ /([0-9a-f]{2})+/g);
}
# Check for 5e in the additional sense location (byte 13)
# https://www.t10.org/lists/asc-num.htm#ASC_5E
if (scalar @sensehexbytes >= 13 && lc($sensehexbytes[12]) eq "5e") {
warn "[DEBUG] Drive $fulldev is in SCSI low power mode\n"
if $DEBUG;
return 1;
}
}

return 0;
}

0 comments on commit 9e0a09f

Please sign in to comment.