-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.py
executable file
·44 lines (35 loc) · 1.3 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
#!/usr/bin/env python3
import logging
import re
import socket
import falcon
from collector import AristaMetricsCollector
from prometheus_client.exposition import CONTENT_TYPE_LATEST, generate_latest
class MetricHandler:
def __init__(self, config):
self._config = config
def validate_modules(self, modules):
if not modules or re.match(r"^([a-zA-Z]+)(,[a-zA-Z]+)*$", modules):
return True
logging.error("Invalid modules specified")
return False
def on_get(self, req, resp):
target = req.get_param("target")
modules = req.get_param("modules")
if modules and not self.validate_modules(modules):
resp.status = falcon.HTTP_400
resp.text = "Invalid modules specified"
return
resp.set_header("Content-Type", CONTENT_TYPE_LATEST)
if not target:
resp.status = falcon.HTTP_400
resp.text = "No target parameter provided!"
return
try:
socket.getaddrinfo(target, None)
except socket.gaierror as e:
resp.status = falcon.HTTP_400
resp.text = f"Target does not exist in DNS: {e}"
return
registry = AristaMetricsCollector(self._config, target=target)
resp.text = generate_latest(registry)