From 0fc857dba7be86ea601262420ca3f62c22fccf0b Mon Sep 17 00:00:00 2001 From: Evgeny Smyshlyaev Date: Mon, 8 Apr 2019 15:48:22 +0300 Subject: [PATCH] Hastic-exporter for Prometheus (#520) --- .../prometheus-hastic-exporter.py | 66 +++++++++++++++++++ .../prometheus-hastic-exporter.service | 10 +++ .../requirements.txt | 3 + 3 files changed, 79 insertions(+) create mode 100755 tools/prometheus-hastic-exporter/prometheus-hastic-exporter.py create mode 100644 tools/prometheus-hastic-exporter/prometheus-hastic-exporter.service create mode 100644 tools/prometheus-hastic-exporter/requirements.txt diff --git a/tools/prometheus-hastic-exporter/prometheus-hastic-exporter.py b/tools/prometheus-hastic-exporter/prometheus-hastic-exporter.py new file mode 100755 index 0000000..64d704b --- /dev/null +++ b/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) diff --git a/tools/prometheus-hastic-exporter/prometheus-hastic-exporter.service b/tools/prometheus-hastic-exporter/prometheus-hastic-exporter.service new file mode 100644 index 0000000..a0ee9d1 --- /dev/null +++ b/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 diff --git a/tools/prometheus-hastic-exporter/requirements.txt b/tools/prometheus-hastic-exporter/requirements.txt new file mode 100644 index 0000000..8ac139a --- /dev/null +++ b/tools/prometheus-hastic-exporter/requirements.txt @@ -0,0 +1,3 @@ +prometheus_client>=0.6.0 +requests>=2.7.0 +python-dateutil>=2.7.3