Add influxdb 2.x support

This commit is contained in:
Vitaliy Skypnyk
2021-11-10 21:17:28 +00:00
committed by Vitaliy Skrypnyk
parent b62d650cb5
commit 26ea31e401
10 changed files with 134 additions and 45 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@ __pycache__
.idea
*.iml
config/config.ini
_data

16
.vscode/launch.json vendored Normal file
View File

@@ -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
}
]
}

View File

@@ -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=<HOST>
port=80
username=admin
password=<INFLUX_PASS>
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=<token>
timeout=6000
# For influx v1.x DB name
bucket=keenetic
[keenetic]
admin_endpoint=http://<keenetic_ip>:80
skip_auth=false
admin_endpoint=http://192.168.1.1:80
login=admin
password=<KEENETIC_PASS>
login=<user>
password=<pass>
[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

View File

@@ -1,13 +0,0 @@
[influxdb]
host=<HOST>
port=80
username=admin
password=<INFLUX_PASS>
db=keenetic
[keenetic]
admin_endpoint=http://192.168.1.1:80
skip_auth=false
login=admin
password=<KEENETIC_PASS>
[collector]
interval_sec=30

View File

@@ -0,0 +1,16 @@
[influx2]
url=http://<influx_ip>: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://<keenetic_ip>:80
skip_auth=false
login=<user>
password=<pass>
[collector]
interval_sec=30

View File

@@ -0,0 +1,16 @@
[influx2]
url=http://<influx_ip>:8086
# For influx v1.x please use "-" as a value
org=keenetic
# For influx v1.x please use "username:password" as a token
token=<token>
timeout=6000
# For influx v1.x DB name
bucket=keenetic
[keenetic]
admin_endpoint=http://<keenetic_ip>:80
skip_auth=false
login=<user>
password=<pass>
[collector]
interval_sec=30

View File

@@ -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
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

View File

@@ -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)

View File

@@ -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'])

View File

@@ -1,3 +1,3 @@
influxdb==5.3.1
jsonpath-rw==1.4.0
requests==2.25.1
influxdb-client==1.23.0