Last active
August 21, 2019 16:06
-
-
Save jacobtomlinson/e02b9c631c21e1606354564822e09f14 to your computer and use it in GitHub Desktop.
Get FreeNAS system temperatures in InfluxDB format
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
#!/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 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
I expanded on your code to write the HDD temps to an InfluxDB database.
https://gist.github.com/scottklarr/34996645b824ad7a02b3d42a1718b186