Add interface metric

This commit is contained in:
Vitaliy Skrypnyk
2020-07-28 20:22:58 +03:00
committed by Vitaliy Skrypnyk
parent 2fcb3b1ce8
commit 7b858a065b
3 changed files with 39 additions and 12 deletions

View File

@@ -113,6 +113,20 @@
"total": "$.total", "total": "$.total",
"free": "$.free" "free": "$.free"
} }
},
{
"command": "interface stat",
"root" : "$",
"param" : {
"name" : "GigabitEthernet1"
},
"tags" : {},
"values" : {
"rxbytes": "$.rxbytes",
"txbytes": "$.txbytes",
"rxspeed": "$.rxspeed",
"txspeed": "$.txspeed"
}
} }
] ]
} }

View File

@@ -10,11 +10,12 @@ class InfuxWritter(object):
self.init_database() self.init_database()
def init_database(self): def init_database(self):
print("Connecting to InfluxDB: " + self._configuration['host'])
db_name = self._configuration['db'] db_name = self._configuration['db']
self._client.drop_database(db_name) self._client.drop_database(db_name)
if db_name not in self._client.get_list_database(): 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) self._client.create_database(db_name)
def write_metrics(self, metrics): def write_metrics(self, metrics):

View File

@@ -1,5 +1,7 @@
import json import json
import time import time
import urllib
import requests import requests
from jsonpath_ng.ext import parse from jsonpath_ng.ext import parse
@@ -22,17 +24,19 @@ def json_path_init(paths):
class KeeneticCollector(object): 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._influx = infuxdb_writter
self._endpoint = endpoint self._endpoint = endpoint
self._command = command self._command = metric_configration['command']
self._root = parse(root) self._params = metric_configration.get('param', {})
self._tags = json_path_init(tags) self._root = parse(metric_configration['root'])
self._values = json_path_init(values) self._tags = json_path_init(metric_configration['tags'])
self._values = json_path_init(metric_configration['values'])
def collect(self): 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')) response = json.loads(requests.get(url).content.decode('UTF-8'))
roots = self._root.find(response) roots = self._root.find(response)
@@ -40,7 +44,7 @@ class KeeneticCollector(object):
start_time = time.time_ns() start_time = time.time_ns()
for root in roots: for root in roots:
tags = {} tags = self._params.copy()
values = {} values = {}
for tagName, tagPath in self._tags.items(): for tagName, tagPath in self._tags.items():
@@ -56,10 +60,11 @@ class KeeneticCollector(object):
if values.__len__() == 0: continue if values.__len__() == 0: continue
metric = self.create_metric(self._command, tags, values) metric = self.create_metric(self._command, tags, values)
# print(json.dumps(metric))
metrics.append(metric) metrics.append(metric)
metrics.append( self.create_metric( "collector", { "command" : self._command }, { "duration" : (time.time_ns() - start_time) } ) ) metrics.append(
# print(json.dumps(metrics)) self.create_metric("collector", {"command": self._command}, {"duration": (time.time_ns() - start_time)}))
infuxdb_writter.write_metrics(metrics) infuxdb_writter.write_metrics(metrics)
@@ -81,6 +86,10 @@ class KeeneticCollector(object):
if __name__ == '__main__': if __name__ == '__main__':
print(
" _ __ _ _ _____ _ _ _ \n | |/ / | | (_) / ____| | | | | | \n | ' / ___ ___ _ __ ___| |_ _ ___ | | ___ | | | ___ ___| |_ ___ _ __ \n | < / _ \/ _ \ '_ \ / _ \ __| |/ __| | | / _ \| | |/ _ \/ __| __/ _ \| '__|\n | . \ __/ __/ | | | __/ |_| | (__ | |___| (_) | | | __/ (__| || (_) | | \n |_|\_\___|\___|_| |_|\___|\__|_|\___| \_____\___/|_|_|\___|\___|\__\___/|_| \n \n ")
configuration = json.load(open("config.json", "r")) configuration = json.load(open("config.json", "r"))
endpoint = configuration['endpoint'] endpoint = configuration['endpoint']
metrics = configuration['metrics'] metrics = configuration['metrics']
@@ -89,8 +98,11 @@ if __name__ == '__main__':
infuxdb_writter = InfuxWritter(configuration) infuxdb_writter = InfuxWritter(configuration)
for metric in metrics: print("Connecting to router: " + endpoint)
collectors.append(KeeneticCollector(infuxdb_writter, endpoint, metric['command'], metric['root'], metric['tags'], metric['values']))
for metric_configuration in metrics:
print("Configuring metric: " + metric_configuration['command'])
collectors.append(KeeneticCollector(infuxdb_writter, endpoint, metric_configuration))
while True: while True:
for collector in collectors: collector.collect() for collector in collectors: collector.collect()