Created
August 21, 2019 15:40
-
-
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)
This file contains hidden or 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
| #!/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() |
Author
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
Based on https://gist.github.com/jacobtomlinson/e02b9c631c21e1606354564822e09f14