mirror of
https://github.com/kemko/keenetic-grafana-monitoring.git
synced 2026-01-01 15:45:43 +03:00
Add value_normalizer.py
This commit is contained in:
committed by
Vitaliy Skrypnyk
parent
b56a2064b9
commit
98c1682f6e
@@ -1,12 +1,10 @@
|
|||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
import re
|
|
||||||
|
|
||||||
from typing import Dict
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
def isfloat(value): return (isinstance(value, str) and re.match(r'^-?\d+(?:\.\d+)?$', value) is not None)
|
from typing import Dict
|
||||||
|
from value_normalizer import normalize_data, isvalidmetric
|
||||||
|
|
||||||
|
|
||||||
class KeeneticCollector(object):
|
class KeeneticCollector(object):
|
||||||
|
|
||||||
@@ -19,15 +17,16 @@ class KeeneticCollector(object):
|
|||||||
|
|
||||||
response = json.loads(requests.get(self._endpoint).content.decode('UTF-8'))
|
response = json.loads(requests.get(self._endpoint).content.decode('UTF-8'))
|
||||||
prefix = self._request.split(' ')
|
prefix = self._request.split(' ')
|
||||||
metrics = self.iterate(response, prefix, [], {}, 0)
|
metrics = self.recursive_iterate(response, prefix, [], {}, 0)
|
||||||
|
|
||||||
for metric in metrics:
|
for metric in metrics:
|
||||||
print(json.dumps(metric))
|
print(json.dumps(metric))
|
||||||
|
|
||||||
def iterate(self, data, path, metrics, tags, level):
|
def recursive_iterate(self, data, path, metrics, tags, level):
|
||||||
|
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
|
|
||||||
|
data = normalize_data(data)
|
||||||
tags = tags.copy()
|
tags = tags.copy()
|
||||||
tags.update(self.tags(data))
|
tags.update(self.tags(data))
|
||||||
values = self.values(data)
|
values = self.values(data)
|
||||||
@@ -40,16 +39,16 @@ class KeeneticCollector(object):
|
|||||||
if isinstance(value, list) or isinstance(value, dict):
|
if isinstance(value, list) or isinstance(value, dict):
|
||||||
key_path = path.copy()
|
key_path = path.copy()
|
||||||
|
|
||||||
if level in self._tags_levels: # Need for some API, like show processes
|
if level in self._tags_levels: # Need for some API, like show processes
|
||||||
tags['_'.join(path)] = key
|
tags['_'.join(path)] = key
|
||||||
else:
|
else:
|
||||||
key_path.append(key)
|
key_path.append(key)
|
||||||
|
|
||||||
self.iterate(value, key_path, metrics, tags, level + 1)
|
self.recursive_iterate(value, key_path, metrics, tags, level + 1)
|
||||||
|
|
||||||
if isinstance(data, list):
|
if isinstance(data, list):
|
||||||
for idx, value in enumerate(data):
|
for idx, value in enumerate(data):
|
||||||
self.iterate(value, path, metrics, tags, level + 1)
|
self.recursive_iterate(value, path, metrics, tags, level + 1)
|
||||||
|
|
||||||
return metrics
|
return metrics
|
||||||
|
|
||||||
@@ -66,7 +65,7 @@ class KeeneticCollector(object):
|
|||||||
|
|
||||||
for key in data:
|
for key in data:
|
||||||
value = data.get(key)
|
value = data.get(key)
|
||||||
if isinstance(value, str) and not isfloat(value): labels[key] = value
|
if isinstance(value, str): labels[key] = value
|
||||||
|
|
||||||
return labels
|
return labels
|
||||||
|
|
||||||
@@ -76,16 +75,13 @@ class KeeneticCollector(object):
|
|||||||
for key in data:
|
for key in data:
|
||||||
value = data.get(key)
|
value = data.get(key)
|
||||||
|
|
||||||
if isinstance(value, int): values[key] = value
|
if isvalidmetric(value): values[key] = value
|
||||||
if isinstance(value, bool): values[key] = value
|
|
||||||
if isfloat(value): values[key] = float(value)
|
|
||||||
|
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
(KeeneticCollector('http://192.168.1.1:79/rci/show/processes', 'processes', [1])).collect()
|
|
||||||
(KeeneticCollector('http://192.168.1.1:79/rci/show/processes', 'processes', [1])).collect()
|
(KeeneticCollector('http://192.168.1.1:79/rci/show/processes', 'processes', [1])).collect()
|
||||||
|
# (KeeneticCollector('http://192.168.1.1:79/rci/show/processes', 'processes', [1])).collect()
|
||||||
|
|
||||||
# while True: time.sleep(1)
|
# while True: time.sleep(1)
|
||||||
|
|||||||
35
value_normalizer.py
Normal file
35
value_normalizer.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def isstring(value): return isinstance(value, str)
|
||||||
|
|
||||||
|
|
||||||
|
def isfloat(value: str): return (re.match(r'^-?\d+(?:\.\d+)?$', value) is not None)
|
||||||
|
|
||||||
|
|
||||||
|
def isinteger(value: str): return (re.match('^\d+$', value) is not None)
|
||||||
|
|
||||||
|
|
||||||
|
def isvalidmetric(value) : return isinstance(value, int) or isinstance(value, float) or isinstance(value, bool)
|
||||||
|
|
||||||
|
def normalize_data(data):
|
||||||
|
if isinstance(data, dict):
|
||||||
|
for key in data:
|
||||||
|
|
||||||
|
value = data.get(key)
|
||||||
|
|
||||||
|
if isstring(value):
|
||||||
|
value = remove_data_unit(value)
|
||||||
|
if isinteger(value):
|
||||||
|
data[key] = int(value)
|
||||||
|
continue
|
||||||
|
if isfloat(value):
|
||||||
|
data[key] = float(value)
|
||||||
|
continue
|
||||||
|
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def remove_data_unit(value: str):
|
||||||
|
return value.replace(" kB", "")
|
||||||
Reference in New Issue
Block a user