Fixed missing metric and changed metric types

This commit is contained in:
Nathan Higley 2022-01-15 14:25:23 -05:00
parent 1f39ee619e
commit e703fd5a80
1 changed files with 11 additions and 5 deletions

View File

@ -3,9 +3,10 @@ import sys
import signal import signal
from os import getenv from os import getenv
from time import sleep from time import sleep
from prometheus_client.core import GaugeMetricFamily, REGISTRY from prometheus_client.core import GaugeMetricFamily, REGISTRY, CounterMetricFamily
from prometheus_client import start_http_server from prometheus_client import start_http_server
class TasmotaCollector(object): class TasmotaCollector(object):
def __init__(self): def __init__(self):
self.ip = getenv('DEVICE_IP') self.ip = getenv('DEVICE_IP')
@ -24,9 +25,13 @@ class TasmotaCollector(object):
unit = None unit = None
if len(response[key].split()) > 1: if len(response[key].split()) > 1:
unit = response[key].split()[1] unit = response[key].split()[1]
g = GaugeMetricFamily(metric_name, key, labels=['device'], unit=unit)
g.add_metric([self.ip], metric) if "today" in metric_name or "yesterday" in metric_name or "total" in metric_name:
yield g r = CounterMetricFamily(metric_name, key, labels=['device'], unit=unit)
else:
r = GaugeMetricFamily(metric_name, key, labels=['device'], unit=unit)
r.add_metric([self.ip], metric)
yield r
def fetch(self): def fetch(self):
@ -41,8 +46,9 @@ class TasmotaCollector(object):
values = {} values = {}
string_values = str(page.text).split("{s}") string_values = str(page.text).split("{s}")
for i in range(1,len(string_values)-1): for i in range(1,len(string_values)):
label = string_values[i].split("{m}")[0] label = string_values[i].split("{m}")[0]
value = string_values[i].split("{m}")[1].split("{e}")[0] value = string_values[i].split("{m}")[1].split("{e}")[0]
values[label] = value values[label] = value