Initial prometheus-yoctotemp-exporter implementation

This commit is contained in:
Timothée Floure 2020-11-04 08:29:41 +01:00
parent bfd33348ff
commit e3e4958057

45
prometheus-yoctotemp-exporter Executable file
View file

@ -0,0 +1,45 @@
#!/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.")