Skip to content

Instantly share code, notes, and snippets.

@jacobtomlinson
Last active August 21, 2019 16:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobtomlinson/e02b9c631c21e1606354564822e09f14 to your computer and use it in GitHub Desktop.
Save jacobtomlinson/e02b9c631c21e1606354564822e09f14 to your computer and use it in GitHub Desktop.
Get FreeNAS system temperatures in InfluxDB format
#!/usr/bin/env python
import os
import re
import subprocess
def cpuTemp(hostname):
sysctl = subprocess.Popen(['/sbin/sysctl', '-a'],stdout=subprocess.PIPE)
while True:
line = sysctl.stdout.readline()
if line != '':
if 'cpu' in line and 'temp' in line:
search = re.search('.*cpu\.([0-9]+)\.temp.* ([0-9]+\.[0-9]+)C', line)
cpu = search.group(1)
temp = search.group(2)
print "temperature,host=" + hostname + ",type=cpu,cpu=" + cpu + " temperature=" + temp
else:
break
def diskTemp(hostname):
for disk in listDisks():
if 'ada' in disk:
sysctl = subprocess.Popen(['/usr/local/sbin/smartctl', '-iA', '-n', 'standby', '/dev/'+disk], stdout=subprocess.PIPE)
while True:
line = sysctl.stdout.readline()
if line != '':
if 'Serial Number' in line:
serial = line.split()[2]
if 'Temperature_Celsius' in line:
temp = str(float(line.split()[9]))
else:
print "temperature,host=" + hostname + ",type=disk,disk=" + serial + " temperature=" + temp
break
def listDisks():
sysctl = subprocess.Popen(['/sbin/sysctl', '-n', 'kern.disks'],stdout=subprocess.PIPE)
while True:
line = sysctl.stdout.readline()
if line != '':
return line.split()
else:
break
def main():
hostname = os.uname()[1]
cpuTemp(hostname)
diskTemp(hostname)
if __name__ == "__main__":
main()
@scottklarr
Copy link

scottklarr commented Aug 21, 2019

I expanded on your code to write the HDD temps to an InfluxDB database.

https://gist.github.com/scottklarr/34996645b824ad7a02b3d42a1718b186

@jacobtomlinson
Copy link
Author

@scottklarr nice! The intention was for this script to be used by the exec method in telegraf which would do that for you.

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