Skip to content

Instantly share code, notes, and snippets.

@kendokan
Created November 2, 2017 20:08
Show Gist options
  • Save kendokan/aa3a72bac8862902b3927ca4018d103e to your computer and use it in GitHub Desktop.
Save kendokan/aa3a72bac8862902b3927ca4018d103e to your computer and use it in GitHub Desktop.
This script uses smartctl to report current temperature and health of all disks as a JSON array. It can be used on FreeNAS systems with telegraf's exec input.
#!/usr/bin/env sh
# This script uses smartctl to report current temperature and health of all disks as a JSON array.
# It can be used on FreeNAS systems with telegraf's exec input.
#
# Example telegraf.conf input:
#
# [[inputs.exec]]
# name_override = "diskhealth"
# commands = ["/path/to/diskstatus.sh"]
# timeout = "10s"
# data_format = "json"
# tag_keys = ["disk","health"]
# Location of smartctl executable.
SMARTCTL=/usr/local/sbin/smartctl
# Returns a list of disks.
DISKS=$(/sbin/sysctl -n kern.disks | cut -d= -f2)
for DISK in ${DISKS}
do
TEMP=$(${SMARTCTL} -l scttemp /dev/${DISK} | grep '^Current Temperature:' | awk '{print $3}')
HEALTH=$(${SMARTCTL} -H /dev/${DISK} | grep 'test result:' | cut -d: -f2 | sed 's/^[ \t]*//')
if [ -z != ${TEMP} ] && [ -z != ${HEALTH} ]
then
JSON=$(echo ${JSON}{\"disk\":\"${DISK}\",\"health\":\"${HEALTH}\",\"temperature\":${TEMP}},)
fi
done
# Remove trailing comma on last item.
JSON=$(echo ${JSON} | sed 's/,$//')
echo [${JSON}] >&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment