#!/bin/bash

base_oid=".1.3.6.1.4.1.38696.2.1"
comm_string="public"
vers_string="1"

serial_sub="2.1.5"

# Seagate uses proprietary values, so only health is used. If not true (1) is a prefail or fail
health_sub="2.1.8"
temp_sub="2.1.9"
resect_sub="2.1.10"
currsect_sub="2.1.11"
offline_sub="2.1.12"
udma_sub="2.1.13"
readerr_sub="2.1.14"
seekerr_sub="2.1.15"
eccrec_sub="2.1.16"

err=0

function print_help() {
	echo "Syntax: $0 -H <hostname of IP-address> -s <serialnumber of the harddisk> [-g what]"
	echo "        The -g option is optional, without only the health status us printed"
	echo "        what values can be:"
	echo "                Health               : health status"
	echo "                Temp                 : disk temperature"
	echo "                ReallocatedSectorCt  : Counter for relocated (failed) sectors"
	echo "                CurrentPendingSector : Sectors waiting to be relocated"
	echo "                OfflineUncorrectable : Counter for sectors that could not be recovered"
	echo "                UDMACRCErrorCount    : CRC errors"
	echo "                ReadErrorRate        : Amount of read errors that occurred in de drives lifetime"
	echo "                SeekErrorRate        : Amount of seek errors that occurred in de drives lifetime"
	echo "                HardwareECCRecovered : Hardware ECC Recovered Count"
	exit $1
}

while [ "$1" != "" ]; do
	case $1 in
		-H) host=$2
		    shift
		;;
		-g)
		    get=$2
		    shift
		;;
		-s) serial=$2
		    shift
		;;
		-h) print_help 0
		;;
		--help) print_help 0
		;;
	esac
	shift
done

if [ "$host" = "" ]; then
	print_help 1
fi

if [ "${serial}" = "" ]; then
	print_help 1
fi

if [ "${get}" = "" ] || [ "${get}" = "Health" ]; then
	get_oid=${health_sub}
elif [ "${get}" = "Temp" ]; then
	get_oid=${temp_sub}
elif [ "${get}" = "ReallocatedSectorCt" ]; then
	get_oid=${resect_sub}
elif [ "${get}" = "CurrentPendingSector" ]; then
	get_oid=${currsect_sub}
elif [ "${get}" = "OfflineUncorrectable" ]; then
	get_oid=${offline_sub}
elif [ "${get}" = "UDMACRCErrorCount" ]; then
	get_oid=${udma_sub}
elif [ "${get}" = "ReadErrorRate" ]; then
	get_oid=${readerr_sub}
elif [ "${get}" = "SeekErrorRate" ]; then
	get_oid=${seekerr_sub}
elif [ "${get}" = "HardwareECCRecovered" ]; then
	get_oid=${eccrec_sub}
else
	print_help 1
fi

oid=`snmpwalk -On -c ${comm_string} -v ${vers_string} ${host} ${base_oid}.${serial_sub} | grep ${serial} | cut -d' ' -f1`

out=`snmpget -c ${comm_string} -v ${vers_string} ${host} ${base_oid}.${get_oid}.${oid##*.} | cut -d' ' -f4`

# Create output
if [ "${get}" = "" ] || [ "${get}" = "health" ]; then
	echo -n "SMART "
	if [ "${out}" = "1" ]; then
		echo OK
		# Set OK
		err=0
	else
		echo FAIL
		# Set critical
		ERR=2
	fi
elif [ ${err} = 0 ]; then
	echo ${out}
fi

exit $ERR
# END
