#!/bin/bash

# install the smartctl package first! 
# Ubuntu / Debian - `apt-get install smartctl`
# CentOS based - `yum install smartmontools`

# This works only with disks without raid.

# Creating a array to store and display results
results=()


for drive in /dev/sd[a-n] /dev/sd[a-n][a-n]
do
    if [[ ! -e $drive ]]; then continue ; fi

    echo "Starting with disk - $drive"
    results+=( "Drive Results for $drive:" )

    smart=$(
        sudo smartctl -H $drive 2>/dev/null |
        grep '^SMART overall'
    )

    [[ "$smart" == "" ]] && smart='unavailable'

    # get the S.M.A.R.T. data for the drive
    smartctl -a $drive > smartctl.txt    

    # extract the "Reallocated_Sector_Ct" value (number of reallocated sectors)
    reallocated_sectors=`grep "Reallocated_Sector_Ct" smartctl.txt | awk '{print $10}'`

    # get the drive size in bytes
    drive_size=`blockdev --getsize64 $drive`

    # calculate the remaining life of the drive (in percent)
    remaining_life=$(echo "100 - ($reallocated_sectors * 100) / ($drive_size / 512)" | bc -l)

    # Display all the results:

    #smartctl output
    cat smartctl.txt

    echo "--------------------------------------------"

    # print the remaining life
    # echo "Remaining life of drive $drive: $remaining_life%"
    results+=( "Remaining life of drive $drive: $remaining_life%" )

    # echo "------------------------"

    # echo "$smart"
    results+=( "$smart" )

    # echo "------------------------"

    results+=( "----------")

done

echo "======================================"

for i in "${results[@]}"; do
    echo $i
done

