Rev 157 | Rev 169 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#!/usr/bin/env perl
use warnings;
use strict;
use Data::Dumper;
# Description: Use smartctl to get disk information
our $VERSION = '2.0';
# R. W. Rodolico
# grabs smart readings from all drives in system
# note that in the case of some hardware RAID controller, will list the components of a "drive" and the drive itself,
# so you may have up to twice the number of entries than you have physical drives, for example, if you have a megaraid
# controller where each physical drive is also a single logical drive.
# find our location and use it for searching for libraries
BEGIN {
use FindBin;
use File::Spec;
use lib File::Spec->catdir($FindBin::Bin);
use library;
}
exit 0 unless checkDate( 'm' ); # run this only on first of month
my %ignoreDriveTypes = ( # a list of drive "types" used by virtuals that we just ignore.
'VBOX HARDDISK' => 1,
);
sub getSmartInformationReport {
my ($drive, $type) = @_;
$type = '' if ( $type =~ m/scsi/ );
my @report = `smartctl -i $drive $type`;
chomp @report;
my %reportHash;
for ( my $i = 0; $i < @report; $i++ ) {
if ( $report[$i] =~ m/^(.*):(.*)$/ ) {
$reportHash{$1} = trim($2);
}
}
return \%reportHash;
} # getSmartInformationReport
sub getSmartAttributeReport {
my ($drive, $type) = @_;
$type = '' if ( $type =~ m/scsi/ );
my @report = `smartctl -A $drive $type`;
chomp @report;
my %reportHash;
my %headers;
# bypass all the header information
my $i;
for ( $i = 0; $i < @report && $report[$i] !~ m/^ID#/; $i++ ) {}
if ( $i < @report ) { # did we get an actual report? some drives will not give us one
my $char = 0;
while ( $char < length($report[$i]) ) {
substr( $report[$i],$char ) =~ m/^([^ ]+\s*)/;
my $header = $1;
my $start = $char;
my $length = length($header);
if ( $header = &trim( $header ) ) {
$headers{$header}{'start'} = $start;
$headers{$header}{'length'} = $length-1;
}
$char += $length;
}
while ( ++$i < @report ) {
last unless $report[$i];
my $id = &trim(substr( $report[$i], $headers{'ID#'}{'start'}, $headers{'ID#'}{'length'} ));
my $name = &trim(substr( $report[$i], $headers{'ATTRIBUTE_NAME'}{'start'}, $headers{'ATTRIBUTE_NAME'}{'length'} ));
my $value = &trim(substr( $report[$i], $headers{'RAW_VALUE'}{'start'} ));
$reportHash{$id}{'value'} = $value;
$reportHash{$id}{'name'} = $name;
}
}
#print Dumper( \%reportHash ); die;
return \%reportHash;
}
sub getAttributes {
my ($report,$sectorSize) = @_;
my %reportHash;
# first let's get total disk writes
if ( defined( $report->{'241'} ) ) {
$sectorSize =~ m/^(\d+)/;
$reportHash{'writes'} = $report->{'241'} * $sectorSize;
}
# find total uptime
if ( defined( $report->{'9'} ) ) {
$report->{'9'}->{'value'} =~ m/^(\d+)/;
$reportHash{'uptime'} = $1;
}
return \%reportHash;
}
sub getInformation {
my $report = shift;
my %info;
my %keys = (
'Model Family' => {
'tag' => 'Make',
'regex' => '(.*)'
},
'Device Model' => {
'tag' => 'Model',
'regex' => '(.*)'
},
'Serial number' => {
'tag' => 'Serial',
'regex' => '(.*)'
},
'Serial Number' => {
'tag' => 'Serial',
'regex' => '(.*)'
},
'User Capacity' => {
'tag' => 'Capacity',
'regex' => '([0-9,]+)'
},
'Logical block size' =>{
'tag' => 'Sector Size',
'regex' => '(\d+)'
},
'Sector Size' => {
'tag' => 'Sector Size',
'regex' => '(\d+)'
},
'Rotation Rate' => {
'tag' => 'Rotation',
'regex' => '(.*)'
},
);
foreach my $key ( keys %keys ) {
if ( defined( $report->{$key} ) && $report->{$key} =~ m/$keys{$key}->{'regex'}/ ) {
$info{$keys{$key}->{'tag'}} = $1;
}
}
# remove comma's from capacity
$info{'Capacity'} =~ s/,//g if $info{'Capacity'};
return \%info;
}
# category we will use for all values found
# see sysinfo for a list of valid categories
my $CATEGORY = 'attributes';
# run the commands necessary to do whatever you want to do
# The library entered above has some helper routines
# validCommandOnSystem -- passed a name, returns the fully qualified path or '' if it does not exist
# cleanUp - passed a delimiter and a string, does the following (delimiter can be '')
# chomps the string (removes trailing newlines)
# removes all text BEFORE the delimiter, the delimiter, and any whitespace
# thus, the string 'xxI Am x a weird string' with a newline will become
# 'a weird string' with no newline
# now, return the tab delimited output (to STDOUT). $CATEGORY is the first
# item, name as recognized by sysinfo is the second and the value is
# the last one. For multiple entries, place on separate lines (ie, newline separated)
# check for commands we want to run
my %commands = (
'smartctl' => '',
);
# check the commands for validity
foreach my $command ( keys %commands ) {
$commands{$command} = &validCommandOnSystem( $command );
}
# bail if we don't have the commands we need
exit 1 unless $commands{'smartctl'};
# Get all the drives on the system
my %allDrives = map { $_ =~ '(^[a-z0-9/]+)\s+(.*)\#'; ($1,$2) } `smartctl --scan`;
#foreach my $key ( keys %allDrives ) { print "$key\t$allDrives{$key}\n"; } die;
# output the drives and information as tab delimited,
foreach my $thisDrive ( sort keys %allDrives ) {
#print "$thisDrive\n";
print "$CATEGORY\t$thisDrive Type\t$allDrives{$thisDrive}\n";
my $informationReport = &getSmartInformationReport( $thisDrive, $allDrives{$thisDrive} );
my $info = &getInformation( $informationReport );
# if this is not a virtual device, get smart attributes also
if ( ! defined( $ignoreDriveTypes{ $info->{'Model'} } ) ) {
#print Dumper( $info ); die;
my $attributesReport = &getSmartAttributeReport( $thisDrive, $allDrives{$thisDrive} );
my $attributes = &getAttributes( $attributesReport, $info->{'Sector Size'} );
#print Dumper( $attributes ); die;
$info = { %$info, %$attributes };
#print Dumper( $info ); die;
}
foreach my $key ( keys %$info ) {
print "$CATEGORY\t$thisDrive $key\t" . $info->{$key} . "\n";
}
}
exit 0;