-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.py
47 lines (36 loc) · 1.37 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
import falcon
import logging
import socket
from wsgiref import simple_server
from prometheus_client.exposition import CONTENT_TYPE_LATEST
from prometheus_client.exposition import generate_latest
from collector import AristaMetricsCollector
class welcomePage:
def on_get(self, req, resp):
resp.body = '{"message": "This is the Arista eAPI exporter. Use /arista to retrieve metrics."}'
class metricHandler:
def __init__(self, config, exclude=list):
self._exclude = exclude
self._config = config
def on_get(self, req, resp):
self._target = req.get_param("target")
resp.set_header('Content-Type', CONTENT_TYPE_LATEST)
if not self._target:
msg = "No target parameter provided!"
logging.error(msg)
raise falcon.HTTPMissingParam('target')
try:
socket.gethostbyname(self._target)
except socket.gaierror as excptn:
msg = "Target does not exist in DNS: {0}".format(excptn)
logging.error(msg)
resp.status = falcon.HTTP_400
resp.body = msg
else:
registry = AristaMetricsCollector(
self._config,
exclude=self._exclude,
target=self._target
)
collected_metric = generate_latest(registry)
resp.body = collected_metric