#!/bin/bash

DEVICES=$( /bin/ls /sys/block -1 | grep "^sd" )
SMARTLOG="/var/log/smartd.log"

function parse() {
LOGTIME=`date +%Y.%m.%d-%H:%M:%S`
ERROR='0'
if [ `echo "$disk" | grep megaraid | wc -l` -eq 1 ]
then
    read L
    while [ "$L" ]
    do
       if [ "`echo $L | cut -b 19-41`" = "command failed: exiting" ]
    then
        SMARTERROR='2'
        echo "$LOGTIME :$disk:attention disque illisible:2" >> $SMARTLOG
        return
        break
       fi
       if [ "`echo $L | cut -b 1-7`" = "Device:" ]
        then
            MODEL="`echo $L | awk '{print $2}'`"
        fi
        if [ "`echo $L | cut -b 1-14`" = "Serial number:" ]
        then
            SERIAL="`echo $L | awk '{print $3}'`"
        fi
    if [ "`echo $L | cut -b 1-26`"  = "Current Drive Temperature:" ]
    then
        TEMPERATURE="`echo $L | awk '{print $4}'`"
    fi
    if [ "`echo $L | cut -b 1-19`" = "SMART Self-test log" ]
    then
        read L
        read L
        read L
            if [ ! "`echo $L | awk '{print $5 $6 $7 $8}'`" = "Selftestinprogress" ]
            then
        if [ "`echo $L | awk '{print $5}'`" = "Completed" ]
        then
            LASTCHECK="OK"
        else
            LASTCHECK="FAILED"
        fi
        LASTCHECKAGE="`echo $L | awk '{print $7}'`"
        else
             LASTCHECK="in progess"
        fi
    fi
       read L
    done
    slot=`echo $disk | awk '{print $2}'`
    echo -n "$LOGTIME :$disk:$MODEL/$SERIAL:$TEMPERATURE°C:" >> $SMARTLOG
else
    read L
    while [ "$L" ]
    do
     if [ "`echo $L | cut -b 19-41`" = "command failed: exiting" ]
    then
        SMARTERROR='2'
        echo "$LOGTIME :$disk:attention disque illisible:2" >> $SMARTLOG
        return
        break
       fi
    if [ "`echo $L | cut -b 1-13`" = "Device Model:" ]
    then
        MODEL="`echo $L | awk '{print $3}'`"
    fi
    if [ "`echo $L | cut -b 1-14`" = "Serial Number:" ]
    then
        SERIAL="`echo $L | awk '{print $3}'`"
    fi
    if [ "`echo $L | awk '{print $2}'`" = "Reallocated_Sector_Ct" ]
    then
        REALLOCATED="`echo $L | awk '{print $10}'`"
    fi
    #   9 Power_On_Hours          0x0032   088   088   000    Old_age   Always       -       11383
    if [ "`echo $L | awk '{print $2}'`" = "Power_On_Hours" ]
    then
        AGE="`echo $L | awk '{print $10}'`"
    fi
    if [ "`echo $L | awk '{print $2}'`" = "Test_Description" ]
    then
        read L
        # "# 1  Short offline       Self-test routine in progress 40%       568         -"
        if [ ! "`echo $L | awk '{print $5 $6 $7 $8}'`" = "Self-testroutineinprogress" ]
        then
        LASTCHECKAGE="`echo $L | awk '{print $9}'`"
                # "# 1  Short offline       Completed without error       00%     11171         -"
        if [ "`echo $L | awk '{print $5 $6 $7}'`" = "Completedwithouterror" ]
        then
            LASTCHECK="OK"
        else
            LASTCHECK="FAILED"
        fi
        else
        LASTCHECK="In Progress"
        fi
    fi
    read L
    done
echo -n "$LOGTIME :$disk:$MODEL/$SERIAL:$AGE hrs:" >> $SMARTLOG
fi
if [ "$REALLOCATED" ]
    then
    echo -n "relocated sectors=$REALLOCATED:" >> $SMARTLOG
else
    REALLOCATED="0"
fi
echo -n "Last check $LASTCHECK at $LASTCHECKAGE h:" >> $SMARTLOG

if [ "$REALLOCATED" -gt "300" ]
then
    #return 2
    ERROR='1'
fi

if [ "$LASTCHECK" = "FAILED" ]
then
    #return 2
    ERROR=$(($ERROR + 2))
fi
echo "$ERROR" >> $SMARTLOG
}

old_IFS=$IFS
OUT=0
IFS=$'\n'
for disk in $DEVICES ; do
    echo "/usr/sbin/smartctl -a /dev/$disk" | sh | grep -v "^ *$" | parse
    OUT=$(( $? + $OUT ))
done
IFS=$old_IFS
if [ "$OUT" -gt 0 ]
then
    OUT="2"
fi
exit "$OUT"
