forked from FergusInLondon/Tapo-P110-Prometheus-Exporter
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
61 lines (46 loc) · 1.61 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
from threading import Event
import signal
from click import command, option
from loguru import logger
from prometheus_client import start_http_server
from prometheus_client.core import REGISTRY
from yaml import safe_load
from collector import Collector
def graceful_shutdown(shutdown_event):
def _handle(sig, frame):
logger.warning("caught signal for shutdown, stopping service.", extra={
"signal": sig,
})
shutdown_event.set()
signal.signal(signal.SIGINT, _handle)
def start_monitoring(prometheus_port, collector):
start_http_server(prometheus_port)
REGISTRY.register(collector)
@command()
@option(
'--tapo-email', envvar="TAPO_USER_EMAIL",
help="Email address associated with Meross account."
)
@option(
'--tapo-password', envvar="TAPO_USER_PASSWORD",
help="Password associated with TP-Link TAPO account."
)
@option(
'--config-file', default="tapo.yaml", envvar="TAPO_MONITOR_CONFIG",
help="Password associated with TP-Link TAPO account."
)
@option(
'--prometheus-port', default=8080, help="port for prometheus metric exposition"
)
def run(tapo_email, tapo_password, config_file, prometheus_port):
with open(config_file, "r") as cfg:
config = safe_load(cfg)
logger.info("configuring metrics collector and prometheus http server")
collector = Collector(config['devices'], tapo_email, tapo_password)
start_monitoring(prometheus_port, collector)
shutdown = Event()
graceful_shutdown(shutdown)
logger.info("service is up, and awaiting for signals to stop")
shutdown.wait()
if __name__ == "__main__":
run()