diff --git a/config.json b/config.json index 1206ffd..fdbcc19 100644 --- a/config.json +++ b/config.json @@ -113,6 +113,20 @@ "total": "$.total", "free": "$.free" } + }, + { + "command": "interface stat", + "root" : "$", + "param" : { + "name" : "GigabitEthernet1" + }, + "tags" : {}, + "values" : { + "rxbytes": "$.rxbytes", + "txbytes": "$.txbytes", + "rxspeed": "$.rxspeed", + "txspeed": "$.txspeed" + } } ] } \ No newline at end of file diff --git a/influxdb_writter.py b/influxdb_writter.py index 2fa2d32..ddfdeab 100644 --- a/influxdb_writter.py +++ b/influxdb_writter.py @@ -10,11 +10,12 @@ class InfuxWritter(object): self.init_database() def init_database(self): + print("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(): - print("Creating: " + db_name) + print("Creating InfluxDB database: " + db_name) self._client.create_database(db_name) def write_metrics(self, metrics): diff --git a/keentic_influxdb_exporter.py b/keentic_influxdb_exporter.py index 9a4fcb8..fdd6392 100644 --- a/keentic_influxdb_exporter.py +++ b/keentic_influxdb_exporter.py @@ -1,5 +1,7 @@ import json import time +import urllib + import requests from jsonpath_ng.ext import parse @@ -22,17 +24,19 @@ def json_path_init(paths): class KeeneticCollector(object): - def __init__(self, infuxdb_writter, endpoint, command, root, tags, values): + def __init__(self, infuxdb_writter, endpoint, metric_configration): self._influx = infuxdb_writter self._endpoint = endpoint - self._command = command - self._root = parse(root) - self._tags = json_path_init(tags) - self._values = json_path_init(values) + self._command = metric_configration['command'] + self._params = metric_configration.get('param', {}) + self._root = parse(metric_configration['root']) + self._tags = json_path_init(metric_configration['tags']) + self._values = json_path_init(metric_configration['values']) def collect(self): - url = '{}/show/{}'.format(self._endpoint, self._command.replace(' ', '/')) + url = '{}/show/{}'.format(self._endpoint, self._command.replace(' ', '/')) + "?" + urllib.parse.urlencode( + self._params) response = json.loads(requests.get(url).content.decode('UTF-8')) roots = self._root.find(response) @@ -40,7 +44,7 @@ class KeeneticCollector(object): start_time = time.time_ns() for root in roots: - tags = {} + tags = self._params.copy() values = {} for tagName, tagPath in self._tags.items(): @@ -56,10 +60,11 @@ class KeeneticCollector(object): if values.__len__() == 0: continue metric = self.create_metric(self._command, tags, values) + # print(json.dumps(metric)) metrics.append(metric) - metrics.append( self.create_metric( "collector", { "command" : self._command }, { "duration" : (time.time_ns() - start_time) } ) ) - # print(json.dumps(metrics)) + metrics.append( + self.create_metric("collector", {"command": self._command}, {"duration": (time.time_ns() - start_time)})) infuxdb_writter.write_metrics(metrics) @@ -81,6 +86,10 @@ class KeeneticCollector(object): if __name__ == '__main__': + + print( + " _ __ _ _ _____ _ _ _ \n | |/ / | | (_) / ____| | | | | | \n | ' / ___ ___ _ __ ___| |_ _ ___ | | ___ | | | ___ ___| |_ ___ _ __ \n | < / _ \/ _ \ '_ \ / _ \ __| |/ __| | | / _ \| | |/ _ \/ __| __/ _ \| '__|\n | . \ __/ __/ | | | __/ |_| | (__ | |___| (_) | | | __/ (__| || (_) | | \n |_|\_\___|\___|_| |_|\___|\__|_|\___| \_____\___/|_|_|\___|\___|\__\___/|_| \n \n ") + configuration = json.load(open("config.json", "r")) endpoint = configuration['endpoint'] metrics = configuration['metrics'] @@ -89,8 +98,11 @@ if __name__ == '__main__': infuxdb_writter = InfuxWritter(configuration) - for metric in metrics: - collectors.append(KeeneticCollector(infuxdb_writter, endpoint, metric['command'], metric['root'], metric['tags'], metric['values'])) + print("Connecting to router: " + endpoint) + + for metric_configuration in metrics: + print("Configuring metric: " + metric_configuration['command']) + collectors.append(KeeneticCollector(infuxdb_writter, endpoint, metric_configuration)) while True: for collector in collectors: collector.collect()