forked from Showmax/arista-eos-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
executable file
·61 lines (50 loc) · 1.77 KB
/
handler.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
import logging
import re
import socket
import falcon
from collector import AristaMetricsCollector
from prometheus_client.exposition import CONTENT_TYPE_LATEST
from prometheus_client.exposition import generate_latest
class metricHandler:
def __init__(self, config):
self._config = config
self._target = None
def handle_modules(self, modules):
if not modules:
return False
module_functions = []
modules = modules.split(",")
for module in modules:
if module == "all":
return False
elif module == "memory":
module.functions
def on_get(self, req, resp):
self._target = req.get_param("target")
modules = req.get_param("modules")
if modules:
if re.match(r"^([a-zA-Z]+)(,[a-zA-Z]+)*$", modules):
self._config["module_names"] = modules
else:
msg = "Invalid modules specified"
logging.error(msg)
resp.status = falcon.HTTP_400
resp.body = msg
return
resp.set_header("Content-Type", CONTENT_TYPE_LATEST)
if not self._target:
msg = "No target parameter provided!"
logging.error(msg)
resp.status = falcon.HTTP_400
resp.body = msg
try:
socket.getaddrinfo(self._target, None)
except socket.gaierror as e:
msg = f"Target does not exist in DNS: {e}"
logging.error(msg)
resp.status = falcon.HTTP_400
resp.body = msg
else:
registry = AristaMetricsCollector(self._config, target=self._target)
collected_metric = generate_latest(registry)
resp.body = collected_metric