mirror of
https://github.com/kemko/keenetic-grafana-monitoring.git
synced 2026-01-01 15:45:43 +03:00
* influxdb2 integration * using python alpine base image * add authorization, refactoring config * logging instead print, keenetic api exception handling when try to collect metrics, a little pep8
28 lines
987 B
Python
28 lines
987 B
Python
import logging
|
|
|
|
import requests
|
|
from influxdb import InfluxDBClient
|
|
|
|
|
|
class InfuxWriter(object):
|
|
|
|
def __init__(self, configuration):
|
|
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)
|
|
|
|
def write_metrics(self, metrics):
|
|
self._client.write_points(metrics)
|