-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (77 loc) · 2.89 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import argparse
import logging
import time
from oligo import Iber
from oligo.exception import IberException
from prometheus_client import Gauge, start_http_server
class IberdrolaDistribucionMetrics:
def __init__(self, username, password) -> None:
# Iberdrola Client
self.conn = Iber()
self.conn.login(username, password)
logging.debug("Logged in")
# Prometheus metrics config
# Hardcoded value, it is working fine until now
self.polling_interval_seconds = 100
# Prometheus metrics
self.consumption = Gauge(
"iberdrola_distribucion_consumption", "Current consumption in watts"
)
self.meter_total = Gauge(
"iberdrola_distribucion_meter_total", "Total consumption in kWh"
)
def run_metrics_loop(self):
while True:
self.fetch()
time.sleep(self.polling_interval_seconds)
def fetch(self):
succed = False
while not succed:
try:
mea = self.conn.measurement()
watt = mea["consumption"]
kwh = mea["meter"]
self.consumption.set(watt)
self.meter_total.set(kwh)
succed = True
logging.debug("Consumption: %sW, Meter: %skWh", watt, kwh)
except IberException:
logging.debug("Error fetching data, retrying in 3 seconds")
time.sleep(3)
except Exception as e:
logging.error("Error fetching data: %s", e)
logging.debug("Error fetching data, retrying in 10 seconds")
time.sleep(10)
def main():
parser = argparse.ArgumentParser(
description="Iberdrola Distribucion Prometheus exporter"
)
parser.add_argument(
"-s",
"--server",
help="Exporter server address",
required=False,
default="0.0.0.0",
)
parser.add_argument(
"-P", "--port", help="Exporter server port", required=False, default=9988
)
parser.add_argument("-u", "--username", help="i-de username", required=True)
parser.add_argument("-p", "--password", help="i-de password", required=True)
parser.add_argument("-v", "--verbose", help="Verbose mode", action="store_true")
args = vars(parser.parse_args())
if args["verbose"]:
logging.basicConfig(level=logging.DEBUG)
logging.info("Verbose mode enabled")
else:
logging.basicConfig(level=logging.INFO)
# Print config
logging.info("Exporter server: %s:%s", args["server"], args["port"])
logging.info("i-de username: %s", args["username"])
# Server address and port
idm = IberdrolaDistribucionMetrics(args["username"], args["password"])
logging.info("Starting exporter server")
start_http_server(addr=args["server"], port=int(args["port"]))
idm.run_metrics_loop()
if __name__ == "__main__":
main()