#!/usr/bin/perl
#Pull SSD Life data from any drives with the value.
use strict;
use Sys::Hostname;
use Cwd 'abs_path';

my $debug = 0;
my %smart;
my $count = 0;
my $threshold = 10;

# Device brand must be upper case
my %attrlist = ("KINGSTON"	=>	{
			attrid	=>	231,
			attrnam	=>	"SSD_Life_Left",
			thresh	=>	$threshold,
			},
		"D2CSTK251M3T"	=>	{	# OCZ Sandforce
			attrid	=>	231,
			attrnam	=>	"SSD_Life_Left",
			thresh	=>	$threshold,
			},
		"D2CSTK251M21"	=>	{	# OCZ Sandforce
			attrid	=>	231,
			attrnam	=>	"SSD_Life_Left",
			thresh	=>	$threshold,
			},
		"D2CSTK251M11"	=>	{	# OCZ Sandforce
			attrid	=>	231,
			attrnam	=>	"SSD_Life_Left",
			thresh	=>	$threshold,
			},
		"OCZ"           =>      {       # OCZ Sandforce
                        attrid  =>      231,
                        attrnam =>      "SSD_Life_Left",
                        thresh  =>      $threshold,
                        },	
		"INTEL"		=>	{
			attrid	=>	233,
			attrnam	=>	"Media_Wearout_Indicator",
			thresh	=>	$threshold,
			},
		"SSDSC2KG240G8R" =>      {
                        attrid  =>      233,
                        attrnam =>      "Media_Wearout_Indicator",
                        thresh  =>      $threshold,
                        },
		"SSDSC2KG480G8R" =>      {
                        attrid  =>      233,
                        attrnam =>      "Media_Wearout_Indicator",
                        thresh  =>      $threshold,
                        },
		"SSDSC2KG019T8R" =>      {
                        attrid  =>      233,
                        attrnam =>      "Media_Wearout_Indicator",
                        thresh  =>      $threshold,
                        },
		"SSDSA2SH064G1GC" =>      {
                        attrid  =>      233,
                        attrnam =>      "Media_Wearout_Indicator",
                        thresh  =>      $threshold,
                        },	
		"SAMSUNG"	=>	{
			attrid	=>	177,
			attrnam	=>	"Wear_Leveling_Count",
			thresh	=>	$threshold,
			},
		"MICRON"	=>	{
			attrid	=>	202,
			attrnam	=>	"Percent_Lifetime_Used",
			thresh	=>	$threshold,
			},
		"MTFDDAK1T0TDL" =>      {
                        attrid  =>      202,
                        attrnam =>      "Percent_Lifetime_Used",
                        thresh  =>      $threshold,
                        },	
		"CRUCIAL"	=>	{
			attrid	=>	202,
			attrnam	=>	"Percent_Lifetime_Used",
			thresh	=>	$threshold,
			},
		"CT500MX500SSD1" =>      {
                        attrid  =>      202,
                        attrnam =>      "Percent_Lifetime_Used",
                        thresh  =>      $threshold,
                        },
		"THNSF8200CCSE" =>      {
                        attrid  =>      233,
                        attrnam =>      "Media_Wearout_Indicator",
                        thresh  =>      $threshold,
                        },	
		"M4"		=>	{
			attrid	=>	202,
			attrnam	=>	"Perc_Rated_Life_Used",
			thresh	=>	$threshold,
			},
		"SANDISK"	=>	{
			attrid	=>	230,
			attrnam	=>	"Life_Curve_Status",
			thresh	=>	90,
			},
		"SDLF1CRM"	=>	{	# SanDisk CloudSpeed
			attrid	=>	233,
			attrnam	=>	"Media_Wearout_Indicator",
			thresh	=>	$threshold,
			},
		"SDLFGC7R"	=>	{	# SanDisk CloudSpeed
			attrid	=>	233,
			attrnam	=>	"Media_Wearout_Indicator",
			thresh	=>	$threshold,
			},
		"MICRONM2" =>	{
			attrid   =>	  202,
			attrnam =>   "Percent_Lifetime_Used",
			thresh  => 3,
		}
		);

my @skipdevices = ("3E128-TS2-530G00");					# Nytrocache & Micron 512G M.2
my @m2_models	= ("Micron_1100_MTFDDAV512TBN");
my $storcli_bin = '/usr/local/eig_zabbix/sbin/raidcheck/bin/storcli64';
my $perccli_bin = '/usr/local/eig_zabbix/sbin/raidcheck/bin/perccli64';
my $smartctl_bin = '/usr/local/eig_zabbix/sbin/raidcheck/bin/smartctl';
my $attr_pattern = '^(\d+)\s+(\S+)\s+0x\S+\s+(\d{3})\s+\d{3}\s+[\d\-]{3}\s+\S+\s+\S+\s+\S+\s+\d+.*$';
my $device_pattern = '^Device Model:\s+(([a-zA-Z0-9]+)[ _\/]?.*)$';
my $serial_pattern = '^Serial Number:\s+(\S+)$';
my $cli_bin = $storcli_bin;
$cli_bin = $perccli_bin if( `PATH+=":/sbin:/usr/sbin" lshw -quiet -class disk` =~ /PERC/ );


$debug = 1 if($ARGV[0] eq '--debug');
chomp(my @drives = `PATH+=":/sbin:/usr/sbin" lshw -short -c disk -quiet 2>/dev/null| grep -Po '/dev/(s|h|v)d[a-z]+'`);
DRIVE_LOOP: foreach my $drive (@drives) {
	my ($device_model, $device_brand, $serial_number);
		open(my $cat,'-|',"$smartctl_bin -a $drive");
		while(<$cat>) {
				if(/$device_pattern/){
						($device_model, $device_brand) = ($1,uc($2));
					foreach my $skipdevice (@skipdevices){
							if($skipdevice eq $device_model){
									close $cat;
									next DRIVE_LOOP;
							}
					}
					foreach my $m2_models (@m2_models){
						if($device_model eq $m2_models){
							$device_brand = $device_brand.'M2';
						}
					}
		}
		$serial_number = $1 if(/$serial_pattern/);
		if($device_brand && /$attr_pattern/) {
			$smart{$drive}->{$device_brand}->{$1}->{attrnam} = $2;
			$smart{$drive}->{$device_brand}->{$1}->{column4} = $3;
			$smart{$drive}->{$device_brand}->{$1}->{model} = $device_model;
			$smart{$drive}->{$device_brand}->{$1}->{serial} = $serial_number;
		}
	}
		close $cat;
}


chomp(my @controllers = `$cli_bin /call/eall/sall show all | grep -oP 'Controller = (\\d+)' | awk '{print \$3}'`);
foreach my $controller (@controllers) {
	chomp(my @controllerdisks =`$cli_bin /c$controller/vall show all | grep -oP '^SCSI NAA Id = (\\S+)'| awk '{print \$5}'`);
	my $diskPath = "";
	foreach my $controllerdisk (@controllerdisks) {
		if( -l "/dev/disk/by-id/wwn-0x$controllerdisk") {
			$diskPath = abs_path("/dev/disk/by-id/wwn-0x$controllerdisk");
			last;
		}
	}
	chomp(my @megadrives = `$cli_bin /c$controller/eall/sall show | grep -oP '\d+:\d+\s+\d+' | awk '{print \$2}'`);
	MEGADRIVE_LOOP: foreach my $megadrive (@megadrives) {
		my ($device_model, $device_brand, $serial_number);
			open(my $cat,'-|',"$smartctl_bin -a -d sat+megaraid,$megadrive $diskPath");
			while(<$cat>) {
			if(/$device_pattern/){
				($device_model, $device_brand) = ($1,uc($2));
				foreach my $skipdevice (@skipdevices){
					if($skipdevice eq $device_model){
						close $cat;
						next MEGADRIVE_LOOP;
					}
				}
			}
			$serial_number = $1 if(/$serial_pattern/);
			if($device_brand && /$attr_pattern/) {
				$smart{$megadrive}->{$device_brand}->{$1}->{attrnam} = $2;
				$smart{$megadrive}->{$device_brand}->{$1}->{column4} = $3;
				$smart{$megadrive}->{$device_brand}->{$1}->{model} = $device_model;
				$smart{$megadrive}->{$device_brand}->{$1}->{serial} = $serial_number;
			}
			}
			close $cat;
	}
}

foreach my $match (keys %smart) {
	foreach my $brand (keys %attrlist) {
		if(exists($smart{$match}->{$brand}) && $smart{$match}->{$brand}->{ $attrlist{$brand}->{attrid} }->{column4}) {
			if($debug){
				printf("drive->%-8s :: ",$match);
				printf("brand->%-12s :: ",$brand);
				printf("model->%-25s :: ",$smart{$match}->{$brand}->{ $attrlist{$brand}->{attrid} }->{model});
				printf("attrid->%03i :: ",$attrlist{$brand}->{attrid});
				printf("attrnam->%-24s :: ",$smart{$match}->{$brand}->{ $attrlist{$brand}->{attrid} }->{attrnam});
				printf("column4->%03i :: ",$smart{$match}->{$brand}->{ $attrlist{$brand}->{attrid} }->{column4});
				printf("thresh->%03i :: ",$attrlist{$brand}->{thresh});
				printf("serial->%s\n",$smart{$match}->{$brand}->{ $attrlist{$brand}->{attrid} }->{serial});
			}
			$count++ if($smart{$match}->{$brand}->{ $attrlist{$brand}->{attrid} }->{column4} && $smart{$match}->{$brand}->{ $attrlist{$brand}->{attrid} }->{column4} <= $attrlist{$brand}->{thresh} || $smart{$match}->{$brand}->{ $attrlist{$brand}->{attrid} }->{column4} > 100);
		}
	}	
}

printf("count->%i\n",$count) if($debug);

if(! -d '/usr/local/eig_zabbix/tmp'){
        mkdir('/usr/local/eig_zabbix/tmp') or die('Error creating directory: /usr/local/eig_zabbix/tmp');
}

open(my $zabbix,'>>',"/usr/local/eig_zabbix/tmp/raid.tmp");
        print $zabbix hostname() . " raid[\"SSD_Life_Left\"] " . $count . "\n";
close($zabbix);
