From ab8426f893d8f010f7a4db5c5877e768bc9af93a Mon Sep 17 00:00:00 2001 From: Vitaliy Skrypnyk <2773025+vitaliy-sk@users.noreply.github.com> Date: Tue, 28 Jul 2020 19:28:25 +0300 Subject: [PATCH] Add integration with influx --- config.json | 13 ++++++++++--- influxdb_writter.py | 21 +++++++++++++++++++++ keentic_influxdb_exporter.py | 18 +++++++++++++----- 3 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 influxdb_writter.py diff --git a/config.json b/config.json index 5136477..a619854 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,13 @@ { "endpoint" : "http://192.168.1.1:79/rci", - "interval_sec" : 10, + "interval_sec" : 30, + "influxdb" : { + "host" : "", + "port" : 80, + "username" : "", + "password" : "", + "db" : "" + }, "metrics" : [ { "command": "processes", @@ -24,10 +31,10 @@ "ssid": "$.ssid", "mode": "$.mode", "ip": "$.ip", - "mac": "$.mac" + "mac": "$.mac", + "active": "$.active" }, "values" : { - "active": "$.active", "rxbytes": "$.rxbytes", "txbytes": "$.txbytes", "txrate": "$.txrate", diff --git a/influxdb_writter.py b/influxdb_writter.py new file mode 100644 index 0000000..2fa2d32 --- /dev/null +++ b/influxdb_writter.py @@ -0,0 +1,21 @@ +import requests +from influxdb import InfluxDBClient + +class InfuxWritter(object): + + def __init__(self, configuration): + requests.packages.urllib3.disable_warnings() + self._configuration = configuration['influxdb'] + self._client = InfluxDBClient(self._configuration['host'], self._configuration['port'], self._configuration['username'], self._configuration['password'], self._configuration['db']) + self.init_database() + + def init_database(self): + db_name = self._configuration['db'] + self._client.drop_database(db_name) + + if db_name not in self._client.get_list_database(): + print("Creating: " + db_name) + self._client.create_database(db_name) + + def write_metrics(self, metrics): + self._client.write_points( metrics ) \ No newline at end of file diff --git a/keentic_influxdb_exporter.py b/keentic_influxdb_exporter.py index 812c907..196ba3d 100644 --- a/keentic_influxdb_exporter.py +++ b/keentic_influxdb_exporter.py @@ -4,6 +4,7 @@ import requests from jsonpath_ng.ext import parse +from influxdb_writter import InfuxWritter from value_normalizer import normalize_value @@ -21,7 +22,8 @@ def json_path_init(paths): class KeeneticCollector(object): - def __init__(self, endpoint, command, root, tags, values): + def __init__(self, infuxdb_writter, endpoint, command, root, tags, values): + self._influx = infuxdb_writter self._endpoint = endpoint self._command = command self._root = parse(root) @@ -34,6 +36,7 @@ class KeeneticCollector(object): response = json.loads(requests.get(url).content.decode('UTF-8')) roots = self._root.find(response) + metrics = [] for root in roots: tags = {} @@ -41,22 +44,25 @@ class KeeneticCollector(object): for tagName, tagPath in self._tags.items(): if tagPath == '~': - tags[tagName] = root.path.fields[0] + tags[tagName] = str(root.path.fields[0]) else: - tags[tagName] = self.get_first_value(tagPath.find(root.value)) + tags[tagName] = str(self.get_first_value(tagPath.find(root.value))) for valueName, valuePath in self._values.items(): values[valueName] = normalize_value(self.get_first_value(valuePath.find(root.value))) metric = self.create_metric(self._command, tags, values) + metrics.append(metric) print(json.dumps(metric)) + infuxdb_writter.write_metrics(metrics) + @staticmethod def create_metric(measurement, tags, values): return { "measurement": measurement, "tags": tags, - "time": int(time.time() * 1000.0), + "time": time.time_ns(), "fields": values } @@ -75,8 +81,10 @@ if __name__ == '__main__': collectors = [] + infuxdb_writter = InfuxWritter(configuration) + for metric in metrics: - collectors.append(KeeneticCollector(endpoint, metric['command'], metric['root'], metric['tags'], metric['values'])) + collectors.append(KeeneticCollector(infuxdb_writter, endpoint, metric['command'], metric['root'], metric['tags'], metric['values'])) while True: for collector in collectors: collector.collect()