#!/bin/bash

# Travis Morgan <travis.morgan@gmail.com>
# v0.1 - 2020-04-17

# Print harddrive temperatures

printf "%-8s %4s %4s %4s %8s\n" Drive Size Air Temp Age
ls -1 /dev/sd? | while read D
do
	SC="$(smartctl -a $D)"
	SZ="$(echo "scale=2; `echo \"$SC\" | grep Capacity | awk '{print $3}' | sed -e 's/,//g'`/1024/1024/1024/1024" | bc)"
	AF="$(echo \""$SC"\" | grep '190 Airflow_Temperature_Cel' | awk '{print $10}')"
	T="$(echo \""$SC"\" | grep '194 Temperature_Celsius' | awk '{print $10}')"
	AGE="$(echo \""$SC"\" | grep '9 Power_On_Hours' | awk '{print $10}')"

	[ -z "$AF" ] && AF='0'
	[ -z "$T" ] && T="$(echo \""$SC"\" | grep 'Current Drive Temperature' | awk '{print $4}')"
	[ -z "$T" ] && T='0'
	[ -z "$AGE" ] && AGE="$(smartctl -l background $D | grep 'power on time' | awk '{print $6}' | awk -F: '{print $1}')"
	[ -z "$AGE" ] && AGE='0' || AGE="$(printf "%2iy %3id" $(echo "scale=0;(${AGE}/8670)" | bc) $(echo "scale=0;(${AGE}%8670)/24" | bc))"

	printf "%8s %4.2f %4i %4i %8s\n" $D $SZ $AF $T "$AGE"
done

