#!/bin/sh
# smartctl_all, author S. Timm, last modified 3/16/07
# This is a new 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"
sataorig="sda sdb"
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
# Now check for SATA drives

for drive in $sataorig
do
#  This will return "ATA" if a sata drive
#  Nothing if drive is not there
#  some other value if it is a scsi drive
    issata=`/usr/sbin/smartctl -i /dev/${drive} | grep Device: | cut -d' ' -f2`
    if [ "$issata" == "ATA" ] 
    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
done

#echo $DRIVELIST
if [ "$DRIVELIST" != "" ] 
then
for drive in $DRIVELIST
do
drivelet="${drive:2}"
drivetype="${drive:0:2}"
#echo $drivetype
atafield=""
if [ "$drivetype" == "sd" ] 
then
    atafield="-d ata "
fi
# Initialize variables
SMART=""
SECTORS=""
#Use the SMART self-test mode of the drive
/usr/sbin/smartctl ${atafield} -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 ${atafield} -a /dev/${drive} >> /var/log/smartctl_${drive}
#Get the self-test results
teststate="`/usr/sbin/smartctl ${atafield} -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 ${atafield} -A /dev/${drive} | grep 'Current_Pending_Sector' | awk '{print $10}'`"
TODAY="`date`"
STRUCTURE="`/usr/sbin/smartctl ${atafield} -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
fi
