#!/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;
my %attrlist = ("KINGSTON"	=>	{
			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,
			},
		);

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-Z]+)[ _\/].*)$';

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_id;
	my $device_brand;
        open(my $cat,'-|',"$smartctl_bin -a $drive");
        while(<$cat>) {
                if(/$device_pattern/){
                        ($device_id, $device_brand) = ($1,$2);
	                foreach my $skipdevice (@skipdevices){
	                        if($skipdevice eq $device_id){
	                                close $cat;
	                                next DRIVE_LOOP;
	                        }
	                }
		}
		if($device_brand && /$attr_pattern/) {
			$smart{$drive}->{$device_brand}->{$1}->{attrnam} = $2;
			$smart{$drive}->{$device_brand}->{$1}->{column4} = $3;
		}
	}
        close $cat;
}

chomp(my @megadrives = `$megacli_bin -PDList -aALL |grep 'Device Id' | awk '{print \$3}'`);
MEGADRIVE_LOOP: foreach my $megadrive (@megadrives) {
	my ($device_id, $device_brand);
        open(my $cat,'-|',"$smartctl_bin -a -d sat+megaraid,$megadrive /dev/sda");
        while(<$cat>) {
		if(/$device_pattern/){
			($device_id, $device_brand) = ($1,$2);
			foreach my $skipdevice (@skipdevices){
				if($skipdevice eq $device_id){
					close $cat;
					next MEGADRIVE_LOOP;
				}
			}
		}
		if($device_brand && /$attr_pattern/) {
			$smart{$megadrive}->{$device_brand}->{$1}->{attrnam} = $2;
			$smart{$megadrive}->{$device_brand}->{$1}->{column4} = $3;
		}
        }
        close $cat;
}

foreach my $match (keys %smart) {
	foreach my $brand (keys %attrlist) {
		if(exists($smart{$match}->{$brand})) {
			printf("brand->%-12s :: ",$brand) if($debug);
			printf("attrid->%03i :: ",$attrlist{$brand}->{attrid}) if($debug);
			printf("attrnam->%-24s :: ",$smart{$match}->{$brand}->{ $attrlist{$brand}->{attrid} }->{attrnam}) if($debug);
			printf("column4->%03i :: ",$smart{$match}->{$brand}->{ $attrlist{$brand}->{attrid} }->{column4}) if($debug);
			printf("thresh->%i\n",$attrlist{$brand}->{thresh}) if($debug);
			$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);
