hardcoded prometheus-textfile exporter
This commit is contained in:
@@ -3,18 +3,22 @@
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
from datetime import date
|
||||
from os import path
|
||||
from os import getenv
|
||||
import time
|
||||
import sys
|
||||
|
||||
# Configuration
|
||||
FAN_PIN = getenv("RPI_FAN_PIN", 21) # BCM pin used to drive transistor's base
|
||||
WAIT_TIME = getenv("RPI_FAN_WAITTIME", 5) # [s] Time to wait between each refresh
|
||||
FAN_MIN = getenv("RPI_FAN_MIN", 70) # [%] Fan minimum speed.
|
||||
PWM_FREQ = getenv("RPI_FAN_PWMFREQ", 25000) # [Hz] Change this value if fan has strange behavior
|
||||
FAN_PIN = getenv('RPI_FAN_PIN', 21) # BCM pin used to drive transistor's base
|
||||
WAIT_TIME = getenv('RPI_FAN_WAITTIME', 5) # [s] Time to wait between each refresh
|
||||
FAN_MIN = getenv('RPI_FAN_MIN', 70) # [%] Fan minimum speed.
|
||||
PWM_FREQ = getenv('RPI_FAN_PWMFREQ', 25000) # [Hz] Change this value if fan has strange behavior
|
||||
# Fan speed will change only of the difference of temperature is higher than hysteresis
|
||||
hyst = getenv("RPI_FAN_HYST", 5)
|
||||
debug = bool(getenv("RPI_FAN_DEBUG", "False"))
|
||||
hyst = getenv('RPI_FAN_HYST', 1)
|
||||
debug = bool(getenv('RPI_FAN_DEBUG', 'False'))
|
||||
|
||||
prom_collect_dir = getenv('RPI_PROM_COLLECTDIR','/tmp/textfile-collector')
|
||||
prom_collect_file = prom_collect_dir + '/rpi_fan.prom'
|
||||
|
||||
# Configurable temperature and fan speed steps
|
||||
tempSteps = [55, 57, 60, 65, 70] # [°C]
|
||||
@@ -35,21 +39,21 @@ cpuTempOld = 0
|
||||
fanSpeedOld = 0
|
||||
|
||||
if debug:
|
||||
print("%s Temp: %s, Fan: %s" % (date.today(), cpuTemp, fanSpeed))
|
||||
print('%s Temp: %s, Fan: %s' % (date.today(), cpuTemp, fanSpeed))
|
||||
|
||||
# We must set a speed value for each temperature step
|
||||
if len(speedSteps) != len(tempSteps):
|
||||
print("Numbers of temp steps and speed steps are different")
|
||||
print('Numbers of temp steps and speed steps are different')
|
||||
exit(0)
|
||||
|
||||
try:
|
||||
while 1:
|
||||
# Read CPU temperature
|
||||
cpuTempFile = open("/sys/class/thermal/thermal_zone0/temp", "r")
|
||||
cpuTempFile = open('/sys/class/thermal/thermal_zone0/temp', 'r')
|
||||
cpuTemp = float(cpuTempFile.read()) / 1000
|
||||
cpuTempFile.close()
|
||||
if debug:
|
||||
print("%s Temp: %s, Fan: %s" % (date.today(), cpuTemp, fanSpeed))
|
||||
print('%s Temp: %s, Fan: %s' % (date.today(), cpuTemp, fanSpeed))
|
||||
# Calculate desired fan speed
|
||||
if abs(cpuTemp - cpuTempOld) > hyst:
|
||||
# Below first value, fan will run at min speed.
|
||||
@@ -72,15 +76,21 @@ try:
|
||||
and (fanSpeed >= FAN_MIN or fanSpeed == 0)):
|
||||
fan.ChangeDutyCycle(fanSpeed)
|
||||
fanSpeedOld = fanSpeed
|
||||
if debug:
|
||||
print("%s Temp: %s, Fan: %s" % (date.today(), cpuTemp, fanSpeed))
|
||||
if path.isdir(prom_collect_dir):
|
||||
with open(prom_collect_file, 'w') as f:
|
||||
str = '#HELP rpi_fan fan running at percent duty cycle\n'
|
||||
str += '#TYPE rpi_fan gauge\n'
|
||||
str += 'rpi_fan{component="sys"} %s\n' % fanSpeed
|
||||
f.write(str)
|
||||
if debug:
|
||||
print('%s Temp: %s, Fan: %s' % (date.today(), cpuTemp, fanSpeed))
|
||||
cpuTempOld = cpuTemp
|
||||
|
||||
# Wait until next refresh
|
||||
time.sleep(WAIT_TIME)
|
||||
|
||||
# If a keyboard interrupt occurs (ctrl + c), the GPIO is set to 0 and the program exits.
|
||||
finally:
|
||||
print("Fan ctrl interrupted by keyboard")
|
||||
except Exception as exception:
|
||||
print('Exception: {}'.format(type(exception).__name__))
|
||||
print('Exception message: {}'.format(exception))
|
||||
GPIO.cleanup()
|
||||
sys.exit()
|
||||
|
||||
Reference in New Issue
Block a user