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

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,
			},
		"INTEL"		=>	{
			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,
			},
		"CRUCIAL"	=>	{
			attrid	=>	202,
			attrnam	=>	"Percent_Lifetime_Used",
			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,
			},
		);

my @skipdevices = ("3E128-TS2-530G00");					# Nytrocache

my $megacli_bin = '/usr/local/eig_zabbix/sbin/raidcheck/bin/MegaCli64';
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+)$';

$debug = 1 if($ARGV[0] eq '--debug');

chomp(my @drives = `fdisk -l 2>/dev/null| grep -Po '/dev/(s|h|v)d[a-z]+' | uniq`);
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;
	                        }
	                }
		}
		$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 @megadrives = `$megacli_bin -PDList -aALL |grep 'Device Id' | awk '{print \$3}'`);
MEGADRIVE_LOOP: foreach my $megadrive (@megadrives) {
	my ($device_model, $device_brand, $serial_number);
        open(my $cat,'-|',"$smartctl_bin -a -d sat+megaraid,$megadrive /dev/sda");
        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})) {
			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} <= $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);
