#!/usr/bin/perl
#Copyright (c) 2026, Zane C. Bowers-Hadley
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without modification,
#are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
#INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
#THE POSSIBILITY OF SUCH DAMAGE.

=for comment

Add this to snmpd.conf like below.

    extend smart /bin/cat /var/cache/smart.snmp

Then add to root's cron tab, if you have more than a few disks.

    */5 * * * * /usr/lib64/librenms/snmp/smart -u

You will also need to create the config file, which defaults to the same path as the script,
but with .config appended. So if the script is located at /usr/lib64/librenms/snmp/smart, the config file
will be /usr/lib64/librenms/snmp/smart.config. Alternatively you can also specific a config via -c.

Anything starting with a # is comment. The format for variables is $variable=$value. Empty
lines are ignored. Spaces and tabes at either the start or end of a line are ignored. Any
line with out a matched variable or # are treated as a disk.

    #This is a comment
    cache=/var/cache/smart
    smartctl=/usr/local/sbin/smartctl
    useSN=0
    ada0
    da5 /dev/da5 -d sat
    twl0,0 /dev/twl0 -d 3ware,0
    twl0,1 /dev/twl0 -d 3ware,1
    twl0,2 /dev/twl0 -d 3ware,2

The variables are as below.

    cache = The path to the cache file to use. Default: /var/cache/smart
    smartctl = The path to use for smartctl. Default: /usr/bin/smartctl
    useSN = If set to 1, it will use the disks SN for reporting instead of the device name.
            1 is the default. 0 will use the device name.

A disk line is can be as simple as just a disk name under /dev/. Such as in the config above
The line "ada0" would resolve to "/dev/ada0" and would be called with no special argument. If
a line has a space in it, everything before the space is treated as the disk name and is what
used for reporting and everything after that is used as the argument to be passed to smartctl.

If you want to guess at the configuration, call it with -g and it will print out what it thinks
it should be.


Switches:

-c <config>   The config file to use.
-u            Update
-p            Pretty print the JSON.
-Z            GZip+Base64 compress the results.
-v            Verbose output (print status info to STDERR)
-V            Print version and exit

-g            Guess at the config and print it to STDOUT
-C            Enable manual checking for guess and cciss.
-S            Set useSN to 0 when using -g
-t <test>     Run the specified smart self test on all the devices.
-U            When calling cciss_vol_status, call it with -u.
-G <modes>    Guess modes to use. This is a comma seperated list.
              Default :: scan-open,cciss-vol-status

Guess Modes:

- scan :: Use "--scan" with smartctl. "scan-open" will take presidence.

- scan-open :: Call smartctl with "--scan-open".

- cciss-vol-status :: Freebsd/Linux specific and if it sees /dev/sg0(on Linux) or
    /dev/ciss0(on FreebSD) it will attempt to find drives via cciss-vol-status,
    and then optionally checking for disks via smrtctl if -C is given. Should be noted
    though that -C will not find drives that are currently missing/failed. If -U is given,
    cciss_vol_status will be called with -u.

=cut

##
## You should not need to touch anything below here.
##
use warnings;
use strict;
use Getopt::Std        qw( getopts );
use JSON               qw( decode_json );
use MIME::Base64       qw( encode_base64 );
use IO::Compress::Gzip qw(gzip);
use Scalar::Util       qw( looks_like_number );
use List::Util         qw( min );

my $cache    = '/var/cache/smart';
my $smartctl = '/usr/bin/smartctl';
my @disks;
my $useSN = 1;

$Getopt::Std::STANDARD_HELP_VERSION = 1;

sub main::VERSION_MESSAGE {
	print "SMART SNMP extend 0.3.4\n";
}

sub main::HELP_MESSAGE {
	&VERSION_MESSAGE;
	print "\n" . "-v            Verbose output\n";
	print "-V            Print version and exit\n";
	print "\n" . "-u   Update '" . $cache . "'\n" . '-g            Guess at the config and print it to STDOUT
-c <config>   The config file to use.
-p            Pretty print the JSON.
-Z            GZip+Base64 compress the results.
-C            Enable manual checking for guess and cciss.
-S            Set useSN to 0 when using -g
-t <test>     Run the specified smart self test on all the devices.
-U            When calling cciss_vol_status, call it with -u.
-G <modes>    Guess modes to use. This is a comma seperated list.
              Default :: scan-open,cciss-vol-status


Scan Modes:

- scan :: Use "--scan" with smartctl. "scan-open" will take presidence.

- scan-open :: Call smartctl with "--scan-open".

- cciss-vol-status :: Freebsd/Linux specific and if it sees /dev/sg0(on Linux) or
    /dev/ciss0(on FreebSD) it will attempt to find drives via cciss-vol-status,
    and then optionally checking for disks via smrtctl if -C is given. Should be noted
    though that -C will not find drives that are currently missing/failed. If -U is given,
    cciss_vol_status will be called with -u.
';

} ## end sub main::HELP_MESSAGE

#gets the options
my %opts = ();
my $verbose = 0;
getopts( 'ugc:pZhvVCSGt:U', \%opts );

if ( $opts{h} ) {
	&HELP_MESSAGE;
	exit;
}
if ( $opts{v} ) {
	$verbose = 1;
}
if ( $opts{V} ) {
	&VERSION_MESSAGE;
	exit;
}

#
# figure out what scan modes to use if -g specified
#
my $scan_modes = {
	'scan-open'        => 0,
	'scan'             => 0,
	'cciss_vol_status' => 0,
};
if ( $opts{g} ) {
	if ( !defined( $opts{G} ) ) {
		$opts{G} = 'scan-open,cciss_vol_status';
	}
	$opts{G} =~ s/[\ \t]//g;
	my @scan_modes_split = split( /,/, $opts{G} );
	foreach my $mode (@scan_modes_split) {
		if ( !defined $scan_modes->{$mode} ) {
			die( '"' . $mode . '" is not a recognized scan mode' );
		}
		$scan_modes->{$mode} = 1;
	}
} ## end if ( $opts{g} )

# configure JSON for later usage
# only need to do this if actually running as in -g is not specified
my $json;
if ( !$opts{g} ) {

	$json = JSON->new->allow_nonref->canonical(1);
	if ( $opts{p} ) {
		$json->pretty;
	}
}

#
#
# guess if asked
#
#
if ( defined( $opts{g} ) ) {

	#get what path to use for smartctl
	$smartctl = `which smartctl`;
	chomp($smartctl);
	if ( $? != 0 ) {
		warn("'which smartctl' failed with a exit code of $?");
		exit 1;
	}

	#try to touch the default cache location and warn if it can't be done
	system( 'touch ' . $cache . '>/dev/null' );
	if ( $? != 0 ) {
		$cache = '#Could not touch ' . $cache . "You will need to manually set it\n" . "cache=?\n";
	} else {
		system( 'rm -f ' . $cache . '>/dev/null' );
		$cache = 'cache=' . $cache . "\n";
	}

	my $drive_lines = '';

	#
	#
	# scan-open and scan guess mode handling
	#
	#
	if ( $scan_modes->{'scan-open'} || $scan_modes->{'scan'} ) {
		# used for checking if a disk has been found more than once
		my %found_disks_names;
		my @argumentsA;

		# use scan-open if it is set, overriding scan if it is also set
		my $mode = 'scan';
		if ( $scan_modes->{'scan-open'} ) {
			$mode = 'scan-open';
		}

		#have smartctl scan and see if it finds anythings not get found
		my $scan_output  = `$smartctl --$mode`;
		my @scan_outputA = split( /\n/, $scan_output );

		# remove non-SMART devices sometimes returned
		@scan_outputA = grep( !/ses[0-9]/,  @scan_outputA );    # not a disk, but may or may not have SMART attributes
		@scan_outputA = grep( !/pass[0-9]/, @scan_outputA );    # very likely a duplicate and a disk under another name
		@scan_outputA = grep( !/cd[0-9]/,   @scan_outputA );    # CD drive
		if ( $^O eq 'freebsd' ) {
			@scan_outputA = grep( !/sa[0-9]/,  @scan_outputA );    # tape drive
			@scan_outputA = grep( !/ctl[0-9]/, @scan_outputA );    # CAM target layer
		} elsif ( $^O eq 'linux' ) {
			@scan_outputA = grep( !/st[0-9]/, @scan_outputA );     # SCSI tape drive
			@scan_outputA = grep( !/ht[0-9]/, @scan_outputA );     # ATA tape drive
		}

		# make the first pass, figuring out what all we have and trimming comments
		foreach my $arguments (@scan_outputA) {
			my $name = $arguments;

			$arguments =~ s/ \#.*//;                               # trim the comment out of the argument
			$name      =~ s/ .*//;
			$name      =~ s/\/dev\///;
			if ( defined( $found_disks_names{$name} ) ) {
				$found_disks_names{$name}++;
			} else {
				$found_disks_names{$name} = 0;
			}

			push( @argumentsA, $arguments );

		} ## end foreach my $arguments (@scan_outputA)

		# second pass, putting the lines together
		my %current_disk;
		foreach my $arguments (@argumentsA) {
			my $not_virt = 1;

			# check to see if we have a virtual device
			my @virt_check = split( /\n/, `smartctl -i $arguments 2> /dev/null` );
			foreach my $virt_check_line (@virt_check) {
				if ( $virt_check_line =~ /(?i)Product\:.*LOGICAL VOLUME/ ) {
					$not_virt = 0;
				}
			}

			my $name = $arguments;
			$name =~ s/ .*//;
			$name =~ s/\/dev\///;

			# only add it if not a virtual RAID drive
			# HP RAID virtual disks will show up with very basical but totally useless smart data
			if ($not_virt) {
				if ( $found_disks_names{$name} == 0 ) {
					# If no other devices, just name it after the base device.
					$drive_lines = $drive_lines . $name . " " . $arguments . "\n";
				} else {
					# if more than one, start at zero and increment, apennding comma number to the base device name
					if ( defined( $current_disk{$name} ) ) {
						$current_disk{$name}++;
					} else {
						$current_disk{$name} = 0;
					}
					$drive_lines = $drive_lines . $name . "," . $current_disk{$name} . " " . $arguments . "\n";
				}
			} ## end if ($not_virt)

		} ## end foreach my $arguments (@argumentsA)
	} ## end if ( $scan_modes->{'scan-open'} || $scan_modes...)

	#
	#
	# scan mode handler for cciss_vol_status
	# /dev/sg* devices for cciss on Linux
	# /dev/ccis* devices for cciss on FreeBSD
	#
	#
	if ( $scan_modes->{'cciss_vol_status'} && ( $^O eq 'linux' || $^O eq 'freebsd' ) ) {
		my $cciss;
		if ( $^O eq 'freebsd' ) {
			$cciss = 'ciss';
		} elsif ( $^O eq 'linux' ) {
			$cciss = 'sg';
		}

		my $uarg = '';
		if ( $opts{U} ) {
			$uarg = '-u';
		}

		# generate the initial device path that will be checked
		my $sg_int = 0;
		my $device = '/dev/' . $cciss . $sg_int;

		my $sg_process = 1;
		if ( -e $device ) {
			my $output = `which cciss_vol_status 2> /dev/null`;
			if ( $? != 0 && !$opts{C} ) {
				$sg_process = 0;
				$drive_lines
					= $drive_lines
					. "# -C not given, but "
					. $device
					. " exists and cciss_vol_status is not present\n"
					. "# in path or 'ccis_vol_status -V "
					. $device
					. "' is failing\n";
			} ## end if ( $? != 0 && !$opts{C} )
		} ## end if ( -e $device )
		my $seen_lines   = {};
		my $ignore_lines = {};
		while ( -e $device && $sg_process ) {
			my $output = `cciss_vol_status -V $uarg $device 2> /dev/null`;
			if ( $? != 0 && $output eq '' && !$opts{C} ) {
				# just empty here as we just want to skip it if it fails and there is no C
				# warning is above
			} elsif ( $? != 0 && $output eq '' && $opts{C} ) {
				my $drive_count = 0;
				my $continue    = 1;
				while ($continue) {
					my $output = `$smartctl -i $device -d cciss,$drive_count 2> /dev/null`;
					if ( $? != 0 ) {
						$continue = 0;
					} else {
						my $add_it = 0;
						my $id;
						while ( $output =~ /(?i)Serial Number:(.*)/g ) {
							$id = $1;
							$id =~ s/^\s+|\s+$//g;
						}
						if ( defined($id) && !defined( $seen_lines->{$id} ) ) {
							$add_it = 1;
							$seen_lines->{$id} = 1;
						}
						if ( $continue && $add_it ) {
							$drive_lines
								= $drive_lines
								. $cciss . '0-'
								. $drive_count . ' '
								. $device
								. ' -d cciss,'
								. $drive_count . "\n";
						}
					} ## end else [ if ( $? != 0 ) ]
					$drive_count++;
				} ## end while ($continue)
			} else {
				my $drive_count = 0;
				# count the connector lines, this will make sure failed are founded as well
				my $seen_conectors = {};
				while ( $output =~ /(connector +\d+[IA]\ +box +\d+\ +bay +\d+.*)/g ) {
					my $cciss_drive_line = $1;
					my $connector        = $cciss_drive_line;
					$connector =~ s/(.*\ bay +\d+).*/$1/;
					if (   !defined( $seen_lines->{$cciss_drive_line} )
						&& !defined( $seen_conectors->{$connector} )
						&& !defined( $ignore_lines->{$cciss_drive_line} ) )
					{
						$seen_lines->{$cciss_drive_line} = 1;
						$seen_conectors->{$connector}    = 1;
						$drive_count++;
					} else {
						# going to be a connector we've already seen
						# which will happen when it is processing replacement drives
						# so save this as a device to ignore
						$ignore_lines->{$cciss_drive_line} = 1;
					}
				} ## end while ( $output =~ /(connector +\d+[IA]\ +box +\d+\ +bay +\d+.*)/g)
				my $drive_int = 0;
				while ( $drive_int < $drive_count ) {
					$drive_lines
						= $drive_lines
						. $cciss
						. $sg_int . '-'
						. $drive_int . ' '
						. $device
						. ' -d cciss,'
						. $drive_int . "\n";

					$drive_int++;
				} ## end while ( $drive_int < $drive_count )
			} ## end else [ if ( $? != 0 && $output eq '' && !$opts{C})]

			$sg_int++;
			$device = '/dev/' . $cciss . $sg_int;
		} ## end while ( -e $device && $sg_process )
	} ## end if ( $scan_modes->{'cciss_vol_status'} && ...)

	my $useSN = 1;
	if ( $opts{S} ) {
		$useSN = 0;
	}

	print '# scan_modes='
		. $opts{G}
		. "\nuseSN="
		. $useSN . "\n"
		. 'smartctl='
		. $smartctl . "\n"
		. $cache
		. $drive_lines;

	exit 0;
} ## end if ( defined( $opts{g} ) )

#get which config file to use
my $config = $0 . '.config';
if ( defined( $opts{c} ) ) {
	$config = $opts{c};
}

#reads the config file, optionally
my $config_file = '';
open( my $readfh, "<", $config ) or die "Can't open '" . $config . "'";
read( $readfh, $config_file, 1000000 );
close($readfh);

#
#
# parse the config file and remove comments and empty lines
#
#
my @configA = split( /\n/, $config_file );
@configA = grep( !/^$/,        @configA );
@configA = grep( !/^\#/,       @configA );
@configA = grep( !/^[\s\t]*$/, @configA );
my $configA_int = 0;
while ( defined( $configA[$configA_int] ) ) {
	my $line = $configA[$configA_int];
	chomp($line);
	$line =~ s/^[\t\s]+//;
	$line =~ s/[\t\s]+$//;

	my ( $var, $val ) = split( /=/, $line, 2 );

	my $matched;
	if ( $var eq 'cache' ) {
		$cache   = $val;
		$matched = 1;
	}

	if ( $var eq 'smartctl' ) {
		$smartctl = $val;
		$matched  = 1;
	}

	if ( $var eq 'useSN' ) {
		$useSN   = $val;
		$matched = 1;
	}

	if ( !defined($val) ) {
		push( @disks, $line );
	}

	$configA_int++;
} ## end while ( defined( $configA[$configA_int] ) )

#
#
# run the specified self test on all disks if asked
#
#
if ( defined( $opts{t} ) ) {

	# make sure we have something that atleast appears sane for the test name
	my $valid_tesks = {
		'offline'        => 1,
		'short'          => 1,
		'long'           => 1,
		'conveyance'     => 1,
		'afterselect,on' => 1,
	};
	if ( !defined( $valid_tesks->{ $opts{t} } ) && $opts{t} !~ /select,(\d+[\-\+]\d+|next|next\+\d+|redo\+\d+)/ ) {
		print '"' . $opts{t} . "\" does not appear to be a valid test\n";
		exit 1;
	}

	print "Running the SMART $opts{t} on all devices in the config...\n\n";

	foreach my $line (@disks) {
		my $disk;
		my $name;
		if ( $line =~ /\ / ) {
			( $name, $disk ) = split( /\ /, $line, 2 );
		} else {
			$disk = $line;
			$name = $line;
		}
		if ( $disk !~ /\// ) {
			$disk = '/dev/' . $disk;
		}

		print "\n------------------------------------------------------------------\nDoing "
			. $smartctl . ' -t '
			. $opts{t} . ' '
			. $disk
			. "  ...\n\n";
		print `$smartctl -t $opts{t} $disk` . "\n";

	} ## end foreach my $line (@disks)

	exit 0;
} ## end if ( defined( $opts{t} ) )

#if set to 1, no cache will be written and it will be printed instead
my $noWrite = 0;

#
#
# if no -u, it means we are being called from snmped
#
#
if ( !defined( $opts{u} ) ) {
	my $cache_extra = '';

	if ( $opts{'Z'} ) {
		$cache_extra = '.snmp';
	}
	# if the cache file exists, print it, otherwise assume one is not being used
	if ( -f $cache . $cache_extra ) {
		my $old = '';
		open( my $readfh, "<", $cache . $cache_extra ) or die "Can't open '" . $cache . $cache_extra . "'";
		read( $readfh, $old, 1000000 );
		close($readfh);
		print $old;
		exit 0;
	} else {
		$opts{u} = 1;
		$noWrite = 1;
	}
} ## end if ( !defined( $opts{u} ) )

#
#
# Process each disk
#
#
my $to_return = {
	'data'        => { 'disks' => {}, 'exit_nonzero' => 0, 'dev_error' => 0, 'unhealthy' => 0, 'useSN' => $useSN },
	'version'     => 1,
	'error'       => 0,
	'errorString' => '',
};

if ($verbose) {
	print "Processing " . scalar(@disks) . " disks (useSN=$useSN)\n";
}
foreach my $line (@disks) {
	my $disk;
	my $name;
	if ( $line =~ /\ / ) {
		( $name, $disk ) = split( /\ /, $line, 2 );
	} else {
		$disk = $line;
		$name = $line;
	}
	if ( $disk !~ /\// ) {
		$disk = '/dev/' . $disk;
	}

	if ($verbose) {
		print "Processing disk: $name -> $disk\n";
	}

	if ($verbose) {
		print "  Command: $smartctl --json -a $disk\n";
	}

	my $output = `$smartctl --json -a $disk`;
	my $exit_code = $? >> 8;
	
	if ($verbose && $exit_code != 0) {
		print "  WARNING: smartctl exit code: $exit_code\n";
		my @output_lines = split(/\n/, $output);
		if (@output_lines > 0) {
			print "  First few lines of output:\n";
			for my $i (0..min($#output_lines, 5)) {
				print "    $output_lines[$i]\n";
			}
		}
	}

	my %IDs    = (
		'5'            => undef,
		'10'           => undef,
		'12'           => undef,
		'173'          => undef,
		'177'          => undef,
		'183'          => undef,
		'184'          => undef,
		'187'          => undef,
		'188'          => undef,
		'190'          => undef,
		'194'          => undef,
		'196'          => undef,
		'197'          => undef,
		'198'          => undef,
		'199'          => undef,
		'231'          => undef,
		'232'          => undef,
		'233'          => undef,
		'9'            => undef,
		'disk'         => $disk,
		'serial'       => undef,
		'selftest_log' => undef,
		'health_pass'  => 0,
		'max_temp'     => undef,
		'exit'         => $?,
		'dev_error'    => 0,
		'json_err'     => 0,
		'json_err_str' => '',
		'temp_limit'   => undef,
		'over_temp'    => 0,
	);
	$IDs{'disk'} =~ s/^\/dev\///;

	my $a_output;
	eval {
		$a_output = decode_json($output);
		if ( ref($a_output) ne 'HASH' ) {
			die(      'decoded output from "'
					. $smartctl
					. ' --json -a '
					. $disk
					. '" has a ref of "'
					. ref($a_output)
					. '" and not "HASH"' );
		}
	};
	if ($@) {
		$IDs{'json_err_str'} = $@;
		$IDs{'json_err'}     = 1;
	}

	if ( $IDs{'exit'} != 0 ) {
		$to_return->{'data'}{'exit_nonzero'}++;
	}

	# if polling exited non-zero above, no reason running the rest of the checks
	my $disk_id = $name;
	if ( $IDs{'json_err'} == 0 ) {
		if (   defined( $a_output->{'smartctl'} )
			&& ( ref( $a_output->{'smartctl'} ) eq 'HASH' )
			&& defined( $a_output->{'smartctl'}{'messages'} )
			&& ( ref( $a_output->{'smartctl'}{'messages'} ) eq 'ARRAY' )
			&& defined( $a_output->{'smartctl'}{'messages'}[0] )
			&& ( ref( $a_output->{'smartctl'}{'messages'}[0] ) eq 'HASH' )
			&& defined( $a_output->{'smartctl'}{'messages'}[0]{'string'} )
			&& ( ref( $a_output->{'smartctl'}{'messages'}[0]{'string'} ) eq '' )
			&& defined( $a_output->{'smartctl'}{'messages'}[0]{'severity'} )
			&& ( ref( $a_output->{'smartctl'}{'messages'}[0]{'severity'} ) eq '' )
			&& ( $a_output->{'smartctl'}{'messages'}[0]{'string'}   =~ /device/ )
			&& ( $a_output->{'smartctl'}{'messages'}[0]{'severity'} =~ /error/ ) )
		{
			$IDs{'dev_error'} = 1;
			$to_return->{'data'}{'dev_error'}++;
		} ## end if ( defined( $a_output->{'smartctl'} ) &&...)

		if (   defined( $a_output->{'ata_smart_attributes'} )
			&& ( ref( $a_output->{'ata_smart_attributes'} ) eq 'HASH' )
			&& defined( $a_output->{'ata_smart_attributes'}{'table'} )
			&& ( ref( $a_output->{'ata_smart_attributes'}{'table'} ) eq 'ARRAY' ) )
		{
			foreach my $attribute ( @{ $a_output->{'ata_smart_attributes'}{'table'} } ) {

				if (   ( ref($attribute) eq 'HASH' )
					&& defined( $attribute->{'id'} )
					&& ( ref( $attribute->{'id'} ) eq '' )
					&& defined( $attribute->{'name'} )
					&& ( ref( $attribute->{'name'} ) eq '' )
					&& defined( $attribute->{'value'} )
					&& ( ref( $attribute->{'value'} ) eq '' )
					&& defined( $attribute->{'raw'} )
					&& ( ref( $attribute->{'raw'} ) eq 'HASH' )
					&& defined( $attribute->{'raw'}{'string'} )
					&& ( ref( $attribute->{'raw'}{'string'} ) eq '' ) )
				{
					my $id             = $attribute->{'id'};
					my $normalized     = $attribute->{'value'};
					my $raw            = $attribute->{'raw'}{'string'};
					my $attribute_name = $attribute->{'name'};

					# Crucial SSD
					# 202, Percent_Lifetime_Remain, same as 231, SSD Life Left
					if (   $id == 202
						&& $attribute_name =~ /Percent_Lifetime_Remain/ )
					{
						$IDs{231} = $raw;
					}

					# single int raw values
					if (   ( $id == 5 )
						|| ( $id == 9 )
						|| ( $id == 10 )
						|| ( $id == 173 )
						|| ( $id == 183 )
						|| ( $id == 184 )
						|| ( $id == 187 )
						|| ( $id == 196 )
						|| ( $id == 197 )
						|| ( $id == 198 )
						|| ( $id == 199 ) )
					{
						my @rawA = split( /\ /, $raw );
						$IDs{$id} = $rawA[0];
					} ## end if ( ( $id == 5 ) || ( $id == 9 ) || ( $id...))

					# single int normalized values
					if (   ( $id == 177 )
						|| ( $id == 230 )
						|| ( $id == 231 )
						|| ( $id == 232 )
						|| ( $id == 233 ) )
					{
				 # annoying non-standard disk
				 # WDC WDS500G2B0A
				 # 230 Media_Wearout_Indicator 0x0032   100   100   ---    Old_age   Always       -       0x002e000a002e
				 # 232 Available_Reservd_Space 0x0033   100   100   004    Pre-fail  Always       -       100
				 # 233 NAND_GB_Written_TLC     0x0032   100   100   ---    Old_age   Always       -       9816

						if (   $id == 230
							&& $attribute_name =~ /Media_Wearout_Indicator/ )
						{
							$IDs{233} = int($normalized);
						} elsif ( $id == 232
							&& $attribute_name =~ /Available_Reservd_Space/ )
						{
							$IDs{232} = int($normalized);
						} else {
							# only set 233 if it has not been set yet
							# if it was set already then the above did it and we don't want
							# to overwrite it
							if ( $id == 233 && !defined( $IDs{'233'} ) ) {
								$IDs{$id} = int($normalized);
							} elsif ( $id != 233 ) {
								$IDs{$id} = int($normalized);
							}
						} ## end else [ if ( $id == 230 && $attribute_name =~ /Media_Wearout_Indicator/)]
					} ## end if ( ( $id == 177 ) || ( $id == 230 ) || (...))

					# 188, Command_Timeout
					if ( $id == 188 ) {
						my $total   = 0;
						my @rawA    = split( /\ /, $raw );
						my $rawAint = 0;
						while ( defined( $rawA[$rawAint] ) ) {
							$total = $total + $rawA[$rawAint];
							$rawAint++;
						}
						$IDs{$id} = $total;
					} ## end if ( $id == 188 )

					# 190, airflow temp
					# 194, temp
					if (   ( $id == 190 )
						|| ( $id == 194 ) )
					{
						my ($temp) = split( /\ /, $raw );
						$IDs{$id} = $temp;
					}

				} ## end if ( ( ref($attribute) eq 'HASH' ) && defined...)
			} ## end foreach my $attribute ( @{ $a_output->{'ata_smart_attributes'...}})
		} ## end if ( defined( $a_output->{'ata_smart_attributes'...}))

		#get the selftest logs
		if ($verbose) {
			print "  Command: $smartctl -l selftest $disk\n";
		}
		$output = `$smartctl -l selftest $disk`;
		my @outputA   = split( /\n/, $output );
		my @completed = grep( /Completed/, @outputA );
		$IDs{'completed'} = scalar @completed;
		my @interrupted = grep( /Interrupted/, @outputA );
		$IDs{'interrupted'} = scalar @interrupted;
		my @read_failure = grep( /read failure/, @outputA );
		$IDs{'read_failure'} = scalar @read_failure;
		my @read_failure2 = grep( /Failed in segment/, @outputA );
		$IDs{'read_failure'} = $IDs{'read_failure'} + scalar @read_failure2;
		my @unknown_failure = grep( /unknown failure/, @outputA );
		$IDs{'unknown_failure'} = scalar @unknown_failure;
		my @extended = grep( /\d.*\ ([Ee]xtended|[Ll]ong).*(?![Dd]uration)/, @outputA );
		$IDs{'extended'} = scalar @extended;
		my @short = grep( /[Ss]hort/, @outputA );
		$IDs{'short'} = scalar @short;
		my @conveyance = grep( /[Cc]onveyance/, @outputA );
		$IDs{'conveyance'} = scalar @conveyance;
		my @selective = grep( /[Ss]elective/, @outputA );
		$IDs{'selective'} = scalar @selective;
		my @offline = grep( /(\d|[Bb]ackground|[Ff]oreground)+\ +[Oo]ffline/, @outputA );
		$IDs{'offline'} = scalar @offline;

		# if we have logs, actually grab the log output
		if (   $IDs{'completed'} > 0
			|| $IDs{'interrupted'} > 0
			|| $IDs{'read_failure'} > 0
			|| $IDs{'extended'} > 0
			|| $IDs{'short'} > 0
			|| $IDs{'conveyance'} > 0
			|| $IDs{'selective'} > 0
			|| $IDs{'offline'} > 0 )
		{
			my @headers = grep( /(Num\ +Test.*LBA| Description .*[Hh]ours)/, @outputA );

			my @log_lines;
			push( @log_lines, @extended, @short, @conveyance, @selective, @offline );
			$IDs{'selftest_log'} = join( "\n", @headers, sort(@log_lines) );
		} ## end if ( $IDs{'completed'} > 0 || $IDs{'interrupted'...})

		$disk_id = $name;
		if ( defined( $a_output->{'serial_number'} )
			&& ( ref( $a_output->{'serial_number'} ) eq '' ) )
		{
			$IDs{'serial'} = $a_output->{'serial_number'};
		}
		if ($useSN) {
			$disk_id = $IDs{'serial'};
		}

		if ( defined( $a_output->{'firmware_version'} )
			&& ( ref( $a_output->{'firmware_version'} ) eq '' ) )
		{
			$IDs{'fw_version'} = $a_output->{'firmware_version'};
		}

		if ( defined( $a_output->{'model_family'} )
			&& ( ref( $a_output->{'model_family'} ) eq '' ) )
		{
			$IDs{'model_family'} = $a_output->{'model_family'};
		}

		if ( defined( $a_output->{'model_name'} )
			&& ( ref( $a_output->{'model_name'} ) eq '' ) )
		{
			$IDs{'model_name'} = $a_output->{'model_name'};
		}

		if ( defined( $a_output->{'device_model'} )
			&& ( ref( $a_output->{'device_model'} ) eq '' ) )
		{
			$IDs{'device_model'} = $a_output->{'device_model'};
		}

		if ( defined( $a_output->{'model_number'} )
			&& ( ref( $a_output->{'model_number'} ) eq '' ) )
		{
			$IDs{'model_number'} = $a_output->{'model_number'};
		}

		#
		# scsi(including sas) drives
		#
		if ( defined( $a_output->{'scsi_vendor'} )
			&& ( ref( $a_output->{'scsi_vendor'} ) eq '' ) )
		{
			$IDs{'vendor'} = $a_output->{'scsi_vendor'};
		}
		if ( defined( $a_output->{'scsi_product'} )
			&& ( ref( $a_output->{'scsi_product'} ) eq '' ) )
		{
			$IDs{'product'} = $a_output->{'scsi_product'};
		}
		if ( defined( $a_output->{'scsi_model_name'} )
			&& ( ref( $a_output->{'scsi_model_name'} ) eq '' ) )
		{
			$IDs{'model_name'} = $a_output->{'scsi_model_name'};
		}
		if ( defined( $a_output->{'scsi_revision'} )
			&& ( ref( $a_output->{'scsi_revision'} ) eq '' ) )
		{
			$IDs{'revision'} = $a_output->{'scsi_revision'};
		}
		if ( defined( $a_output->{'scsi_grown_defect_list'} )
			&& ( ref( $a_output->{'scsi_grown_defect_list'} ) eq '' ) )
		{
			$IDs{'5'} = $a_output->{'scsi_grown_defect_list'};
		}
		if (   defined( $a_output->{'power_on_time'} )
			&& ( ref( $a_output->{'power_on_time'} ) eq 'HASH' )
			&& defined( $a_output->{'power_on_time'}{'hours'} )
			&& ( ref( $a_output->{'power_on_time'}{'hours'} ) eq '' ) )
		{
			$IDs{'9'} = $a_output->{'power_on_time'}{'hours'};
		}
		if (   defined( $a_output->{'scsi_start_stop_cycle_counter'} )
			&& ( ref( $a_output->{'scsi_start_stop_cycle_counter'} ) eq 'HASH' )
			&& defined( $a_output->{'scsi_start_stop_cycle_counter'}{'accumulated_start_stop_cycles'} )
			&& ( ref( $a_output->{'scsi_start_stop_cycle_counter'}{'accumulated_start_stop_cycles'} ) eq '' ) )
		{
			$IDs{'12'} = $a_output->{'scsi_start_stop_cycle_counter'}{'accumulated_start_stop_cycles'};
		}

		#
		# nvme
		#
		if ( defined( $a_output->{'nvme_smart_health_information_log'} )
			&& ( ref( $a_output->{'nvme_smart_health_information_log'} ) eq 'HASH' ) )
		{
			if (   defined( $a_output->{'nvme_smart_health_information_log'} )
				&& ( ref( $a_output->{'nvme_smart_health_information_log'}{'percentage_used'} ) eq '' )
				&& looks_like_number( $a_output->{'nvme_smart_health_information_log'}{'percentage_used'} ) )
			{
				$IDs{'231'} = 100 - $a_output->{'nvme_smart_health_information_log'}{'percentage_used'};
			}
			if ( defined( $a_output->{'nvme_smart_health_information_log'} )
				&& ( ref( $a_output->{'nvme_smart_health_information_log'}{'power_cycles'} ) eq '' ) )
			{
				$IDs{'12'} = $a_output->{'nvme_smart_health_information_log'}{'power_cycles'};
			}
		} ## end if ( defined( $a_output->{'nvme_smart_health_information_log'...}))

		# common on most things, just copy this to 194 is present
		if (   defined( $a_output->{'temperature'} )
			&& ( ref( $a_output->{'temperature'} ) eq 'HASH' )
			&& defined( $a_output->{'temperature'}{'current'} )
			&& ( ref( $a_output->{'temperature'}{'current'} ) eq '' ) )
		{
			$IDs{'194'} = $a_output->{'temperature'}{'current'};
		}

		# get temp limit stuff
		# seems to be common on NVMe
		if (   defined( $a_output->{'temperature'} )
			&& ( ref( $a_output->{'temperature'} ) eq 'HASH' )
			&& defined( $a_output->{'temperature'}{'op_limit_max'} )
			&& ( ref( $a_output->{'temperature'}{'op_limit_max'} ) eq '' ) )
		{
			$IDs{'temp_limit'} = $a_output->{'temperature'}{'op_limit_max'};
		}
		# seems to be common on some SAS
		if (   defined( $a_output->{'temperature'} )
			&& ( ref( $a_output->{'temperature'} ) eq 'HASH' )
			&& defined( $a_output->{'temperature'}{'drive_trip'} )
			&& ( ref( $a_output->{'temperature'}{'drive_drip'} ) eq '' ) )
		{
			$IDs{'temp_limit'} = $a_output->{'temperature'}{'drive_trip'};
		}

		# figure out what to use for the max temp, if there is one
		if ( defined( $IDs{'190'} )
			&& !defined( $IDs{'194'} && looks_like_number( $IDs{'190'} ) ) )
		{
			$IDs{max_temp} = $IDs{'190'};
		} elsif ( !defined( $IDs{'190'} )
			&& defined( $IDs{'194'} && looks_like_number( $IDs{'194'} ) ) )
		{
			$IDs{max_temp} = $IDs{'194'};
		} elsif ( defined( $IDs{'190'} )
			&& defined( $IDs{'194'} && looks_like_number( $IDs{'190'} ) && looks_like_number( $IDs{'194'} ) ) )
		{
			if ( $IDs{'190'} > $IDs{'194'} ) {
				$IDs{'max_temp'} = $IDs{'190'};
			} else {
				$IDs{'max_temp'} = $IDs{'194'};
			}
		}

		# checks if we are have exceeded the max temp
		if (   defined( $IDs{'max_temp'} )
			&& looks_like_number( $IDs{'max_temp'} )
			&& defined( $IDs{'temp_limit'} )
			&& looks_like_number( $IDs{'temp_limit'} )
			&& ( $IDs{'max_temp'} >= $IDs{'temp_limit'} ) )
		{
			$IDs{'over_temp'} = 1;
		}

		if (   defined( $a_output->{'smart_status'} )
			&& ( ref( $a_output->{'smart_status'} ) eq 'HASH' )
			&& defined( $a_output->{'smart_status'}{'passed'} )
			&& ( ref( $a_output->{'smart_status'}{'passed'} ) eq 'JSON::PP::Boolean' )
			&& $a_output->{'smart_status'}{'passed'} )
		{
			$IDs{'health_pass'} = 1;
		}

		if ( !$IDs{'health_pass'} ) {
			$to_return->{data}{unhealthy}++;
		}

		# get form factor info
		if (   defined( $a_output->{'form_factor'} )
			&& ( ref( $a_output->{'form_factor'} ) eq 'HASH' )
			&& defined( $a_output->{'form_factor'}{'name'} )
			&& ( ref( $a_output->{'form_factor'}{'name'} ) eq '' ) )
		{
			$IDs{'form_factor'} = $a_output->{'form_factor'}{'name'};

			if (   defined( $a_output->{'scsi_transport_protocol'} )
				&& ( ref( $a_output->{'scsi_transport_protocol'} ) eq 'HASH' )
				&& defined( $a_output->{'scsi_transport_protocol'}{'name'} )
				&& ( ref( $a_output->{'scsi_transport_protocol'}{'name'} ) eq '' ) )
			{
				$IDs{'form_factor'} = $IDs{'form_factor'} . ' ' . $a_output->{'scsi_transport_protocol'}{'name'};
			} elsif ( defined( $a_output->{'device'} )
				&& ( ref( $a_output->{'device'} ) eq 'HASH' )
				&& defined( $a_output->{'device'}{'type'} )
				&& ( ref( $a_output->{'device'}{'type'} ) eq '' )
				&& ( $a_output->{'device'}{'type'} eq 'sat' )
				&& defined( $a_output->{'device'}{'protocol'} )
				&& ( ref( $a_output->{'device'}{'protocol'} ) eq '' )
				&& ( $a_output->{'device'}{'protocol'} eq 'ATA' ) )
			{
				$IDs{'form_factor'} = $IDs{'form_factor'} . ' SATA';
			} ## end elsif ( defined( $a_output->{'device'} ) && (...))
		} elsif ( defined( $a_output->{'nvme_namespaces'} )
			&& ( ref( $a_output->{'nvme_namespaces'} ) eq 'ARRAY' )
			&& defined( $a_output->{'nvme_version'} ) )
		{
			$IDs{'form_factor'} = 'NVMe';
		}

		# seems to be common for both SAS and SATA
		if ( defined( $a_output->{'rotation_rate'} )
			&& ( ref( $a_output->{'rotation_rate'} ) eq '' ) )
		{
			$IDs{'rpm'} = $a_output->{'rotation_rate'};
		}
	} ## end if ( $IDs{'json_err'} == 0 )

	# only bother to save this if useSN is not being used
	if ( !$useSN ) {
		if ($verbose) {
			print "Adding disk (useSN disabled): $disk_id\n";
		}
		$to_return->{data}{disks}{$disk_id} = \%IDs;
	} elsif ( $IDs{'json_err'} == 0 && defined($disk_id) && defined( $IDs{'serial'} ) ) {
		if ($verbose) {
			my $exit_info = '';
			if ( $IDs{exit} != 0 ) {
				$exit_info = " (smartctl exit: " . ($IDs{exit} >> 8) . ")";
			}
			print "Adding disk: $disk_id (serial: $IDs{serial}$exit_info)\n";
		}
		$to_return->{data}{disks}{$disk_id} = \%IDs;
	} else {
		if ($verbose) {
			my $reason = '';
			if ( !defined($disk_id) ) {
				$reason = 'undefined disk_id';
			} elsif ( !defined( $IDs{'serial'} ) ) {
				$reason = 'no serial number';
			} elsif ( $IDs{'json_err'} != 0 ) {
				$reason = "JSON parse error";
			}
			print "Skipping disk: $name (reason: $reason)\n";
		}
	}

	# smartctl will in some cases exit zero when it can't pull data for cciss
	# so if we get a zero exit, but no serial then it means something errored
	# and the device is likely dead
	if ( $IDs{exit} == 0 && !defined( $IDs{serial} ) ) {
		if ($verbose) {
			print "Marking disk as unhealthy (no serial): $name\n";
		}
		$to_return->{data}{unhealthy}++;
	}
} ## end foreach my $line (@disks)

my $toReturn = $json->encode($to_return);

if ( !$opts{p} ) {
	$toReturn = $toReturn . "\n";
}

my $toReturnCompressed;
gzip \$toReturn => \$toReturnCompressed;
my $compressed = encode_base64($toReturnCompressed);
$compressed =~ s/\n//g;
$compressed = $compressed . "\n";

if ( !$noWrite ) {
	if ($verbose) {
		my @disk_keys = keys %{ $to_return->{data}{disks} };
		print "Writing " . scalar(@disk_keys) . " disks to cache\n";
		print "Exit non-zero: $to_return->{data}{exit_nonzero}\n";
		print "Dev errors: $to_return->{data}{dev_error}\n";
		print "Unhealthy: $to_return->{data}{unhealthy}\n";
	}
	open( my $writefh, ">", $cache ) or die "Can't open '" . $cache . "'";
	print $writefh $toReturn;
	close($writefh);
	open( $writefh, ">", $cache . '.snmp' ) or die "Can't open '" . $cache . ".snmp'";
	print $writefh $compressed;
	close($writefh);
} else {
	print $toReturn;
}
