#!/bin/sh
# smartctl_all, author S. Timm, last modified 4/9/04
# This is a nev version set up to work with smartctl v5
# First, detect what IDE disks are there
DRIVELIST=""
driveorig="hda hdb hdc hdd hde hdf hdg hdh"
for drive in $driveorig
do
    if [ -e /proc/ide/$drive ] 
    then
	foo="`cat /proc/ide/$drive/media`"
	if [ "$foo" == "disk" ] 
	then 
	    DRIVELIST="$drive $DRIVELIST"
#
#    Check to make sure the logrotate file is there, create it if not
#
	    if [ ! -e /etc/logrotate.d/smartctl_$drive ]
	    then
		echo "/var/log/smartctl_$drive { " > /etc/logrotate.d/smartctl_$drive
		echo "    missingok" >> /etc/logrotate.d/smartctl_$drive
                echo "    create 0600 root root" >> /etc/logrotate.d/smartctl_$drive
		echo "}" >> /etc/logrotate.d/smartctl_$drive
	    fi
	fi

    fi
done
#echo $DRIVELIST
for drive in $DRIVELIST
do
drivelet="${drive:2}"
# Initialize variables
SMART=""
SECTORS=""
#Use the SMART self-test mode of the drive
/usr/sbin/smartctl -t short /dev/$drive >/dev/null 2>&1
#extra-long sleep to allow for IBM, WD drives that take longer to finish
sleep 150
#output full results of smartctl to latest log file
date >> /var/log/smartctl_$drive
/usr/sbin/smartctl -a /dev/$drive >> /var/log/smartctl_$drive
#Get the self-test results
teststate="`/usr/sbin/smartctl -l selftest /dev/$drive | grep '# 1' | awk '{print $6}'`"
if [ "$teststate" == "read" ] 
then
    SMART="read"
elif [ "$teststate" == "electrical" ] 
then
    SMART="elec"
else
    SMART="clean"
fi
#Get the current pending sector
SECTORS="`/usr/sbin/smartctl -A /dev/$drive | grep 'Current_Pending_Sector' | awk '{print $10}'`"
TODAY="`date`"
STRUCTURE="`/usr/sbin/smartctl -l error /dev/$drive | grep "Error Count" | awk '{print $4}'`"
#SECTORS="`grep 'Pending Sector' /tmp/smartctl_a | awk '{print $8}'`"

if [ "$SECTORS" == "" ]
then
	SECTORS="0"
fi
if [ "$STRUCTURE" == "" ]
then
        STRUCTURE="0"
fi
echo "$TODAY $SECTORS $SMART $STRUCTURE" >> /var/log/sc_$drivelet
#end drive loop
done

