From 26ea31e401eb3c634ca916bf031eb2e775e5a6aa Mon Sep 17 00:00:00 2001 From: Vitaliy Skypnyk Date: Wed, 10 Nov 2021 21:17:28 +0000 Subject: [PATCH] Add influxdb 2.x support --- .gitignore | 1 + .vscode/launch.json | 16 ++++++++++ README.md | 46 ++++++++++++++++++++++------- config/config.ini.sample | 13 -------- config/config.sample-influx-1.8.ini | 16 ++++++++++ config/config.sample-influx-2.x.ini | 16 ++++++++++ docker-compose.yml | 35 +++++++++++++++++++++- influxdb_writter.py | 23 +++++---------- keentic_influxdb_exporter.py | 11 ++++--- requirements.txt | 2 +- 10 files changed, 134 insertions(+), 45 deletions(-) create mode 100644 .vscode/launch.json delete mode 100644 config/config.ini.sample create mode 100644 config/config.sample-influx-1.8.ini create mode 100644 config/config.sample-influx-2.x.ini diff --git a/.gitignore b/.gitignore index 0195fcd..1c4d1b7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__ .idea *.iml config/config.ini +_data \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d64a198 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Exporter", + "type": "python", + "request": "launch", + "program": "keentic_influxdb_exporter.py", + "console": "integratedTerminal", + "justMyCode": false + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index f222b9a..d6d281a 100644 --- a/README.md +++ b/README.md @@ -18,22 +18,29 @@ Tested with: May work on other Keenetic routers +# Supported InfluxDB version + +InfluxDB 2.x (recomended) and InfluxDB 1.8+ + # Preparation * Create configuration file `config.ini` ```ini -[influxdb] -host= -port=80 -username=admin -password= -db=keenetic +[influx2] +url=http://localhost:8086 +# For influx v1.x please use "-" as a value +org=keenetic +# For influx v1.x please use "username:password" as a token +token= +timeout=6000 +# For influx v1.x DB name +bucket=keenetic [keenetic] +admin_endpoint=http://:80 skip_auth=false -admin_endpoint=http://192.168.1.1:80 -login=admin -password= +login= +password= [collector] interval_sec=30 ``` @@ -87,9 +94,11 @@ nohup python /opt/home/keenetic-grafana-monitoring/keentic_influxdb_exporter.py ``` --- version: '3.7' + services: + keenetic-grafana-monitoring: - image: techh/keenetic-grafana-monitoring:1.1.1 + image: techh/keenetic-grafana-monitoring:2.0.0 container_name: keenetic-grafana-monitoring # environment: # - TZ=Europe/Kiev @@ -98,6 +107,23 @@ services: # Optionally you can override metrics - ./config/metrics.json:/home/config/metrics.json:ro restart: always + + # Influx 2.x + + influxdb: + image: 'influxdb:2.1' + volumes: + - ./_data/influxdb:/var/lib/influxdb + ports: + - 8086:8086 + environment: + - DOCKER_INFLUXDB_INIT_MODE=setup + - DOCKER_INFLUXDB_INIT_ORG=keenetic + - DOCKER_INFLUXDB_INIT_BUCKET=keenetic + - DOCKER_INFLUXDB_INIT_RETENTION=52w + - DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=admin_token + - DOCKER_INFLUXDB_INIT_USERNAME=admin + - DOCKER_INFLUXDB_INIT_PASSWORD=password ``` # Build Docker image diff --git a/config/config.ini.sample b/config/config.ini.sample deleted file mode 100644 index d1a43c5..0000000 --- a/config/config.ini.sample +++ /dev/null @@ -1,13 +0,0 @@ -[influxdb] -host= -port=80 -username=admin -password= -db=keenetic -[keenetic] -admin_endpoint=http://192.168.1.1:80 -skip_auth=false -login=admin -password= -[collector] -interval_sec=30 diff --git a/config/config.sample-influx-1.8.ini b/config/config.sample-influx-1.8.ini new file mode 100644 index 0000000..6092dc7 --- /dev/null +++ b/config/config.sample-influx-1.8.ini @@ -0,0 +1,16 @@ +[influx2] +url=http://:8086 +# For influx v1.x please use "-" as a value +org=- +# For influx v1.x please use "username:password" as a token +token=username:password +timeout=6000 +# For influx v1.x DB name +bucket=keenetic +[keenetic] +admin_endpoint=http://:80 +skip_auth=false +login= +password= +[collector] +interval_sec=30 diff --git a/config/config.sample-influx-2.x.ini b/config/config.sample-influx-2.x.ini new file mode 100644 index 0000000..651e6f9 --- /dev/null +++ b/config/config.sample-influx-2.x.ini @@ -0,0 +1,16 @@ +[influx2] +url=http://:8086 +# For influx v1.x please use "-" as a value +org=keenetic +# For influx v1.x please use "username:password" as a token +token= +timeout=6000 +# For influx v1.x DB name +bucket=keenetic +[keenetic] +admin_endpoint=http://:80 +skip_auth=false +login= +password= +[collector] +interval_sec=30 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 1978948..83cbc6a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,8 @@ --- version: '3.7' + services: + keenetic-grafana-monitoring: build: . container_name: keenetic-grafana-monitoring @@ -9,4 +11,35 @@ services: volumes: - ./config/config.ini:/home/config/config.ini:ro - ./config/metrics.json:/home/config/metrics.json:ro - restart: always \ No newline at end of file + restart: always + + # Influx 2.x + + influxdb: + image: 'influxdb:2.1' + volumes: + - ./_data/influxdb:/var/lib/influxdb + ports: + - 8086:8086 + environment: + - DOCKER_INFLUXDB_INIT_MODE=setup + - DOCKER_INFLUXDB_INIT_ORG=keenetic + - DOCKER_INFLUXDB_INIT_BUCKET=keenetic + - DOCKER_INFLUXDB_INIT_RETENTION=52w + - DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=admin_token + - DOCKER_INFLUXDB_INIT_USERNAME=admin + - DOCKER_INFLUXDB_INIT_PASSWORD=password + + # Influx 1.x + + # influxdb: + # image: 'influxdb:1.8.1' + # volumes: + # - ./_data/influxdb:/var/lib/influxdb + # ports: + # - 8086:8086 + # environment: + # - INFLUXDB_DB=db0 + # - INFLUXDB_HTTP_AUTH_ENABLED=true + # - INFLUXDB_ADMIN_USER=admin + # - INFLUXDB_ADMIN_PASSWORD=password diff --git a/influxdb_writter.py b/influxdb_writter.py index 8c2700e..3b504b8 100644 --- a/influxdb_writter.py +++ b/influxdb_writter.py @@ -1,27 +1,18 @@ import logging import requests -from influxdb import InfluxDBClient +from influxdb_client import InfluxDBClient +from influxdb_client.client.write_api import SYNCHRONOUS class InfuxWriter(object): - def __init__(self, configuration): + def __init__(self, configuration, configuration_file): requests.packages.urllib3.disable_warnings() self._configuration = configuration - 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): - logging.info("Connecting to InfluxDB: " + self._configuration['host']) - db_name = self._configuration['db'] - # self._client.drop_database(db_name) - - if db_name not in self._client.get_list_database(): - logging.info("Creating InfluxDB database: " + db_name) - self._client.create_database(db_name) + self._client = InfluxDBClient.from_config_file(configuration_file) + self._write_api = self._client.write_api(write_options=SYNCHRONOUS) + logging.info("Connecting to InfluxDB: " + self._configuration['url']) def write_metrics(self, metrics): - self._client.write_points(metrics) + self._write_api.write(bucket=self._configuration['bucket'], record=metrics) diff --git a/keentic_influxdb_exporter.py b/keentic_influxdb_exporter.py index 96bb89c..3ce356b 100644 --- a/keentic_influxdb_exporter.py +++ b/keentic_influxdb_exporter.py @@ -4,6 +4,7 @@ import logging import os import time from typing import Dict, List +from influxdb_client.client.write.point import Point from jsonpath_rw import parse @@ -80,12 +81,12 @@ class KeeneticCollector(object): @staticmethod def create_metric(measurement, tags, values): - return { + return Point.from_dict({ "measurement": measurement, "tags": tags, "time": time.time_ns(), "fields": values - } + }) @staticmethod def get_first_value(array): @@ -104,8 +105,10 @@ if __name__ == '__main__': metrics = metrics_configuration['metrics'] config = configparser.ConfigParser(interpolation=None) - config.read(pwd + "/config/config.ini") - infuxdb_writer = InfuxWriter(config['influxdb']) + config_path = pwd + "/config/config.ini" + config.read(config_path) + + infuxdb_writer = InfuxWriter(config['influx2'], config_path) keenetic_config = config['keenetic'] logging.info("Connecting to router: " + keenetic_config['admin_endpoint']) diff --git a/requirements.txt b/requirements.txt index 5d7381b..0385c99 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -influxdb==5.3.1 jsonpath-rw==1.4.0 requests==2.25.1 +influxdb-client==1.23.0 \ No newline at end of file