longevity: Add initial influx support.

Doesn't work yet, calling influx with wrong data structure it seems.
But, getting closer.

Signed-off-by: Ben Greear <greearb@candelatech.com>
This commit is contained in:
Ben Greear
2021-04-07 17:20:31 -07:00
parent 54f2fe70af
commit b4bead52ff
2 changed files with 116 additions and 43 deletions

View File

@@ -1,5 +1,7 @@
#!/usr/bin/env python3
# pip3 install influxdb
import sys
if sys.version_info[0] != 3:
@@ -16,62 +18,75 @@ import time
class RecordInflux(LFCliBase):
def __init__(self,
_lfjson_host="lanforge",
_lfjson_port=8080,
_influx_host="localhost",
_port=8080,
_influx_port=8086,
_influx_user=None,
_influx_passwd=None,
_influx_db=None,
_debug_on=False,
_exit_on_fail=False):
super().__init__(_influx_host,
_port,
super().__init__(_lfjson_host, _lfjson_port,
_debug=_debug_on,
_exit_on_fail=_exit_on_fail)
self.host = _influx_host
self.port = _port
self.influx_host = _influx_host
self.influx_port = _influx_port
self.influx_user = _influx_user
self.influx_passwd = _influx_passwd
self.influx_db = _influx_db
self.client = InfluxDBClient(self.host,
8086,
self.client = InfluxDBClient(self.influx_host,
self.influx_port,
self.influx_user,
self.influx_passwd,
self.influx_db)
def posttoinflux(self, station, key, value):
json_body = [
{
"measurement": station + ' ' + key,
"tags": {
"host": self.host,
"region": "us-west"
},
"time": str(datetime.datetime.utcnow().isoformat()),
"fields": {
"value": value
}
}
]
self.client.write_points(json_body)
def post_to_influx(self, key, value, tags):
data = {}
data['measurement'] = key
data['tags'] = {}
for t in tags:
data['tags'][t.key] = t.val
data['time'] = str(datetime.datetime.utcnow().isoformat())
data['fields'] = {}
data['fields']['value'] = value
def getdata(self,
devices=None,
target_kpi=None,
longevity=None,
monitor_interval=None):
url = 'http://' + self.host + ':8080/port/1/1/'
# json_body = json.dumps(data)
# json_body = [
# {
# "measurement": key,
# "tags": {
# "host": self.host,
# "region": "us-west"
# },
# "time": str(datetime.datetime.utcnow().isoformat()),
# "fields": {
# "value": value
# }
# }
# ]
self.client.write_points(data)
# Don't use this unless you are sure you want to.
# More likely you would want to generate KPI in the
# individual test cases and poke those relatively small bits of
# info into influxdb.
# This will not return until the 'longevity' timer has expired.
def monitor_port_data(self,
lanforge_host="localhost",
devices=None,
longevity=None,
monitor_interval=None):
url = 'http://' + lanforge_host + ':8080/port/1/1/'
end = datetime.datetime.now() + datetime.timedelta(0, longevity)
while datetime.datetime.now() < end:
for station in devices:
url1 = url + station
response = json.loads(requests.get(url1).text)
if target_kpi is None:
for key in response['interface'].keys():
self.posttoinflux(station, key, response['interface'][key])
else:
targets = target_kpi + ['ip', 'ipv6 address', 'alias', 'mac']
response['interface'] = {your_key: response['interface'][your_key] for your_key in targets}
for key in response['interface'].keys():
self.posttoinflux(station, key, response['interface'][key])
# Poke everything into influx db
for key in response['interface'].keys():
self.posttoinflux("%s-%s"%(station, key), response['interface'][key])
time.sleep(monitor_interval)