Created
March 10, 2020 18:38
-
-
Save gsuberland/d877fb924426a3c8e1b784d872b1ce77 to your computer and use it in GitHub Desktop.
FreeNAS Influxdb Disk Temperature Script with NVMe Support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!zsh | |
# FreeNAS Influxdb disk temperature script with NVMe support. | |
# For regular disks, sends the Temperature_Celsius value reported by /dev/daX and /dev/adaX devices. | |
# For NVMe disks, sends all temperature sensor values reported by /dev/nvmeX devices. | |
# Written by Graham Sutherland (gsuberland) | |
# https://github.com/gsuberland/ | |
influxdb_db="graphite" | |
influxdb_host="influxdb.host.goes.here" | |
influxdb_port="8086" | |
influxdb_protocol="http" | |
influxdb_url=$(echo "${influxdb_protocol}://${influxdb_host}:${influxdb_port}/write?db=${influxdb_db}") | |
# loop through all the standard hard drives, i.e. /dev/daX, /dev/adaX | |
for dev in $(ls /dev/ | egrep 'a?da[0-9]+$'); do | |
# extract SMART temperature reading and push it to influxdb | |
smartctl -a /dev/$dev | grep 'Temperature_Celsius' | awk -v dev="$dev" '{print "DiskTemp,component="dev" value="$10}' \ | |
| curl -i -XPOST ${influxdb_url} --data-binary @-; | |
done | |
# loop through all NVMe drives, i.e. /dev/nvmeX | |
for dev in $(ls /dev/ | egrep 'nvme[0-9]+$'); do | |
# store the old separator, then set it to a newline | |
OLD_IFS=$IFS | |
IFS=$'\n' | |
# get SMART temperature sensor data for the drive and extract all relevant output lines into an array | |
tempstrs=( $(smartctl -a /dev/$dev | grep '^Temperature Sensor') ); | |
IFS=$OLD_IFS | |
# create an associative array to store the extracted temperature sensor data in | |
typeset -A temps | |
# go through each of the output lines to extract temperature data | |
for tempstr in $tempstrs; do | |
# this performs a regex match with groups for the sensor number and temperature value | |
[[ $tempstr =~ 'Temperature Sensor ([0-9]+):[[:space:]]+([0-9]+) Celsius' ]] | |
# put the temperature value for the sensor number into the associative array | |
temps[$match[1]]=$match[2]; | |
done; | |
# now format the data into a string to be pushed to influxdb | |
# start the data string with the data type (DiskTemp) and the component name | |
tempdata=$(echo "DiskTemp,component=${dev} ") | |
# now loop through all the associative array values and concatenate them together like temp1=123,temp2=345, | |
for sensor temp in ${(@kv)temps}; do | |
tempdata=$(echo "${tempdata}temp${sensor}=${temp},") | |
done; | |
# strip off the errant trailing comma | |
tempdata=$(echo $tempdata | sed 's/,$//') | |
# write them to the database | |
echo $tempdata | |
echo $tempdata | curl -i -XPOST ${influxdb_url} --data-binary @- | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment