Skip to content

Instantly share code, notes, and snippets.

@erincerys
Last active December 30, 2015 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erincerys/7799553 to your computer and use it in GitHub Desktop.
Save erincerys/7799553 to your computer and use it in GitHub Desktop.
Return your drives' power on hours via smartctl (requires smartmontools)
#!/bin/bash
if [ ! "$(which smartctl)" ] ; then
echo "smartmontools is not installed"
exit 1
fi
logical=($(df | grep -Eo 'sd[a-z]'))
# This doesn't preserve the mountpoint of the raid device, but whatevs for now
if [ "$(cat /proc/mdstat | grep -Ec 'md[0-9]')" -gt 0 ] ; then
logical=(${logical[@]} $(cat /proc/mdstat | grep -Eo 'sd[a-z]'))
fi
if [ "${#logical[@]}" -gt 1 ; then
logical=($(echo $logical | uniq | sort -u))
fi
for l in "${logical[@]}" ; do
l="/dev/$l"
m=$(df | grep "$l" | awk '{print $6}' | head -1)
spintime=$(sudo smartctl -A $l | grep -i power_on_hours | awk '{print $10}' | tr -d "\n")
if [ $spintime ] ; then
if [ $spintime -gt 8760 ] ; then
spinyears=$(($spintime / 24 / 365))
spintimer="${spinyears}y $((($spintime - ($spinyears * 365 * 24)) / 24))d"
timeformat=''
elif [ $spintime -gt 24 ] ; then
spintimer=$(($spintime / 24))
timeformat='d'
else
timeformat='h'
fi
echo -e "Logical:\t${l}"
echo -e "Mountpoint:\t${m}"
echo -e "Raw:\t\t${spintime}"
echo -e "Readable:\t${spintimer}${timeformat}\n"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment