#! /usr/bin/env perl

use warnings;
use strict;

use Data::Dumper;

my %allDrives;

# create a hash with the keys having values ada0, da0, etc
# comment/uncomment one of the three lines below

# manually enter the drives to look at
# %allDrives = map { $_ => 0 } qw/ada0 da0 da1 da2 da3 da4 da5 da6 da7 da8 da9/;
# automagically find the drives on a BSD system
# %allDrives = map { &trim($_) => 0 } `geom disk list | grep 'Geom name:' | cut -d':' -f 2`;
# automagically find the drives on a Linux system
#%allDrives = map { &trim($_) => 0 } `lsblk | grep disk | cut -d' ' -f1`;

# if getSmart.cfg exists, read it
# note, you can create this with smartctl --scan > getSmart.cfg, then edit
# to remove drives you don't want.
if -e 'getSmart.cfg' {
   %allDrives = read getSmart.cfg
} else {
   %allDrives = map { &trim($_) => 0 } `smartctl --scan`;
}

sub trim {
 my $value = shift;
 chomp $value;
 if ( $value =~ m/^([^#]*)/ ) {
   $value = $1;
 }
 $value =~ s/^\s+|\s+$//g;
 return $value;
}

sub getSmartReport {
   my $drive = shift;
   my @report = `smartctl -a /dev/$drive`;
   return @report;
}


sub getTotalWrites {
 my @report = @_;
 return -2 unless grep{ /(Solid State Device)|(SSD)/ } @report;
 my @temp = grep{ /^Sector Size:/ } @report;
 my $sectors = shift @temp;
#   print "The Value is [$sectors]\n"; die;
 if ( $sectors =~ m/^Sector Size\:\s+(\d+)\s+bytes/ ) {
    $sectors = $1;
    # print "Sectors is $sectors\n"; die;
    @temp =  grep{ /^241/ } @report;
    my $lbas = $temp[0];
    if ( $lbas =~ m/(\d+)\s*$/ ) {
       $lbas = $1;
       return $lbas * $sectors;
    } else {
       return -3;
    }
 } else { 
    return -4; 
 }
 return -1; # we could not find something
}

sub getInformation {
   my @report = @_;
   my %info;
   my %keys = ( 
                  'Model Family:'  => { 
                                          'tag' => 'Make',
                                          'regex' => '(.*)'
                                      },
                  'Device Model:'  => { 
                                          'tag' => 'Model',
                                          'regex' => '(.*)'
                                      },
                  'Serial Number:' => { 
                                          'tag' => 'Serial',
                                          'regex' => '(.*)'
                                      },
                  'User Capacity:' => { 
                                          'tag' => 'Capacity',
                                          'regex' => '([0-9,]+)'
                                      },
                  'Sector Size:'   => { 
                                          'tag' => 'Sector',
                                          'regex' => '(\d+)'
                                      },
                  'Rotation Rate:' => { 
                                          'tag' => 'Rotation',
                                          'regex' => '(.*)'
                                      },
                  'Power_On_Hours' => {
                                           'tag' => 'Hours',
                                           'regex' => ' (\d+)\s*$'
                                      },
                  'Error Count:' => {
                                           'tag' => 'Errors',
                                           'regex' => 'Error Count: (\d+)'
                                      },
                                      
               );
   foreach my $key ( keys %keys ) {
      my @temp = grep { /^$key/ } @report;
      if ( @temp ) {
         $temp[0] =~ m/^$key\s+(.*)$/;
         my $value = $1;
         $value =~ m/$keys{$key}->{'regex'}/;
         $info{$keys{$key}->{'tag'}} = $1;
      }
   }
   # remove comma's from capacity
   $info{'Capacity'} =~ s/,//g if $info{'Capacity'};
   return \%info;
}

# output the drives and information as tab delimited,
foreach my $thisDrive ( sort keys %allDrives ) {
 print "$thisDrive\n";
 my @report = &getSmartReport( $thisDrive );
 my $writes = &getTotalWrites( @report );
 my $info = &getInformation( @report );
 $info->{'writes'} = $writes if $writes > 0;
 print Dumper( $info );
 #print "$thisDrive\t" . $allDrives{$thisDrive} . "\n" if $allDrives{$thisDrive} > 0;
}

