prometheus-yoctotemp-exporter/prometheus-yoctotemp-exporter
2020-11-04 08:30:10 +01:00

45 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env python
from prometheus_client import start_http_server, Gauge
from yoctopuce.yocto_api import YAPI, YRefParam
from yoctopuce.yocto_temperature import YTemperature
import sys
HTTP_PORT = 9400
INTERVAL = 5000
YOCTO_SENSOR_TEMPERATURE = Gauge(
'yocto_sensor_temperature',
'Yocto-Temperature sensor temperature value in Celcius'
)
if __name__ == '__main__':
# Setup the API to use local USB devices
errmsg = YRefParam()
if YAPI.RegisterHub("usb", errmsg) != YAPI.SUCCESS:
sys.exit("Could not initialize USB: " + errmsg.value)
# Use first YoctoTemerature sensor found.
sensor = YTemperature.FirstTemperature()
if sensor is None:
sys.exit('Did not find any YoctoTemerature sensor.')
elif sensor.isOnline():
print("YoctoTemerature sensor is ONLINE.")
else:
sys.exit("I found a sensor but it seems unavailable for some reason. Exiting.")
# Serve metrics over HTTP.
print("Starting prometheus-yoctotemp-exporter on port",
"{}...".format(HTTP_PORT), end='')
start_http_server(HTTP_PORT)
print(" OK.")
while sensor.isOnline():
YOCTO_SENSOR_TEMPERATURE.set(sensor.get_currentValue())
YAPI.Sleep(INTERVAL)
sys.stderr.write("YoctoTemperature sensor disappeared for some reason.")
# Cleanup.
YAPI.FreeAPI()
sys.exit("Cleaned up - Now exiting.")