Skip to content

Instantly share code, notes, and snippets.

@scottklarr
Created August 21, 2019 15:40
Show Gist options
  • Select an option

  • Save scottklarr/34996645b824ad7a02b3d42a1718b186 to your computer and use it in GitHub Desktop.

Select an option

Save scottklarr/34996645b824ad7a02b3d42a1718b186 to your computer and use it in GitHub Desktop.
Get S.M.A.R.T. HDD Temps and save to InfluxDB (for FreeNAS)
#!/usr/bin/env python
import os
import re
import subprocess
from influxdb import InfluxDBClient
from datetime import datetime
import time
client = InfluxDBClient('10.1.1.124', 8086, 'username', 'password', 'database_name')
def diskTemp(hostname):
current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
print("")
print(current_time)
hdd_temps = {}
for disk in listDisks():
serial_found = False
temp_found = False
if 'da' in disk:
sysctl = subprocess.Popen(
['/usr/local/sbin/smartctl', '-iA', '-n', 'standby', '/dev/'+disk], stdout=subprocess.PIPE)
for line in sysctl.stdout:
if 'Serial Number' in line.decode():
serial_found = True
if 'Temperature_Celsius' in line.decode():
temp = ((float(line.decode().split()[9])) * 9/5) + 32
temp_found = True
if serial_found == True and temp_found == True:
print("" + disk + "," + str(temp) + "")
hdd_temps[""+disk] = temp
break
json_body = [
{
"measurement": "HDD_Temperatures",
"tags": {
"host": hostname
},
"time": current_time,
"fields": hdd_temps
}
]
client.write_points(json_body)
def listDisks():
sysctl = subprocess.Popen(
['/sbin/sysctl', '-a', 'kern.disks'], stdout=subprocess.PIPE)
while True:
line = sysctl.stdout.readline()
if line != '':
return line.decode().split()
else:
break
def main():
hostname = os.uname()[1]
print(hostname)
while True:
diskTemp(hostname)
time.sleep(60)
if __name__ == "__main__":
main()
@scottklarr
Copy link
Author

@Josholith
Copy link

How about instead of writing your own smartctl output, parser, you could use the pySMART module (https://pypi.org/project/pySMART/)? iXsystems has done some work on to make pySMART 1.0 play nicely with FreeBSD. The pySMART module also takes care of detecting disks, so you wouldn't even need your own sysctl wrapper either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment