Browse Source

Hastic-exporter for Prometheus (#520)

pull/1/head
Evgeny Smyshlyaev 6 years ago committed by rozetko
parent
commit
0fc857dba7
  1. 66
      tools/prometheus-hastic-exporter/prometheus-hastic-exporter.py
  2. 10
      tools/prometheus-hastic-exporter/prometheus-hastic-exporter.service
  3. 3
      tools/prometheus-hastic-exporter/requirements.txt

66
tools/prometheus-hastic-exporter/prometheus-hastic-exporter.py

@ -0,0 +1,66 @@
#!/usr/bin/env python3
from prometheus_client import start_http_server, Metric, REGISTRY
import json
import requests
import sys
import time
import dateutil.parser as dt
class JsonCollector(object):
def __init__(self, endpoint):
self._endpoint = endpoint
def collect(self):
response = None
try:
resp = requests.get(self._endpoint).content.decode('UTF-8')
response = json.loads(resp)
except Exception as e:
print('got exception, skip polling loop {}'.format(e))
return
commitHash = response.get('git', {}).get('commitHash')
packageVersion = response.get('packageVersion')
labels={'commitHash': commitHash, 'packageVersion': packageVersion}
metrics = {
'activeWebhooks': response.get('activeWebhooks'),
'ready': int(response.get('analytics', {}).get('ready', 0)),
'tasksQueueLength': response.get('analytics', {}).get('tasksQueueLength'),
'awaitedTasksNumber': response.get('awaitedTasksNumber'),
'detectionsCount': response.get('detectionsCount')
}
for name, value in metrics.items():
if value is not None:
metric = Metric(name, name, 'gauge')
metric.add_sample(name, value=value, labels=labels)
yield metric
else:
print('{} value is {}, skip metric'.format(name, value))
lastAlive = response.get('analytics', {}).get('lastAlive')
if lastAlive:
lastAlive = int(dt.parse(lastAlive).timestamp()) * 1000 #ms
metric = Metric('lastAlive', 'lastAlive', 'gauge')
metric.add_sample('lastAlive', value=lastAlive, labels=labels)
yield metric
timestamp = response.get('timestamp')
if timestamp:
timestamp = int(dt.parse(timestamp).timestamp()) * 1000 #ms
metric = Metric('timestamp', 'timestamp', 'gauge')
metric.add_sample('timestamp', value=timestamp, labels=labels)
yield metric
if __name__ == '__main__':
hastic_url = sys.argv[1]
exporter_port = int(sys.argv[2])
start_http_server(exporter_port)
REGISTRY.register(JsonCollector(hastic_url))
while True: time.sleep(1)

10
tools/prometheus-hastic-exporter/prometheus-hastic-exporter.service

@ -0,0 +1,10 @@
[Unit]
Description=hastic-exporter
[Service]
RestartSec=1
Restart=always
ExecStart=/usr/bin/hastic-exporter.py http://0.0.0.0:8000 5777
[Install]
WantedBy=multi-user.target

3
tools/prometheus-hastic-exporter/requirements.txt

@ -0,0 +1,3 @@
prometheus_client>=0.6.0
requests>=2.7.0
python-dateutil>=2.7.3
Loading…
Cancel
Save