l3-longevity: Add influxdb-2 API

This properly inserts data into the c7-graphana system.

Signed-off-by: Ben Greear <greearb@candelatech.com>
This commit is contained in:
Ben Greear
2021-04-08 22:02:34 -07:00
parent 2d79dcb038
commit 08401996b2
3 changed files with 99 additions and 12 deletions

View File

@@ -44,7 +44,6 @@ class RecordInflux(LFCliBase):
def post_to_influx(self, key, value, tags): def post_to_influx(self, key, value, tags):
data = dict() data = dict()
data["measurement"] = key data["measurement"] = key
tags["host"] = self.influx_host
data["tags"] = tags data["tags"] = tags
data["time"] = str(datetime.datetime.utcnow().isoformat()) data["time"] = str(datetime.datetime.utcnow().isoformat())
data["fields"] = dict() data["fields"] = dict()

86
py-json/influx2.py Normal file
View File

@@ -0,0 +1,86 @@
#!/usr/bin/env python3
# pip3 install influxdb-client
# Version 2.0 influx DB Client
import sys
if sys.version_info[0] != 3:
print("This script requires Python 3")
exit(1)
import requests
import json
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
import datetime
from LANforge.lfcli_base import LFCliBase
import time
class RecordInflux(LFCliBase):
def __init__(self,
_lfjson_host="lanforge",
_lfjson_port=8080,
_influx_host="localhost",
_influx_port=8086,
_influx_org=None,
_influx_token=None,
_influx_bucket=None,
_debug_on=False,
_exit_on_fail=False):
super().__init__(_lfjson_host, _lfjson_port,
_debug=_debug_on,
_exit_on_fail=_exit_on_fail)
self.influx_host = _influx_host
self.influx_port = _influx_port
self.influx_org = _influx_org
self.influx_token = _influx_token
self.influx_bucket = _influx_bucket
url = "http://%s:%s"%(self.influx_host, self.influx_port)
self.client = influxdb_client.InfluxDBClient(url=url, token=self.influx_token, org=self.influx_org)
self.write_api = self.client.write_api(write_options=SYNCHRONOUS)
#print("org: ", self.influx_org)
#print("token: ", self.influx_token)
#print("bucket: ", self.influx_bucket)
#exit(0)
def post_to_influx(self, key, value, tags, time):
p = influxdb_client.Point(key)
for tag_key, tag_value in tags.items():
p.tag(tag_key, tag_value)
p.time(time)
p.field("value", value)
self.write_api.write(bucket=self.influx_bucket, org=self.influx_org, record=p)
def set_bucket(self, b):
self.influx_bucket = b
# 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 end until the 'longevity' timer has expired.
# This function pushes data directly into the Influx database and defaults to all columns.
def monitor_port_data(self,
lanforge_host="localhost",
devices=None,
longevity=None,
monitor_interval=None,
bucket=None,
tags=None): # dict
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)
time = str(datetime.datetime.utcnow().isoformat())
# Poke everything into influx db
for key in response['interface'].keys():
self.posttoinflux(bucket, "%s-%s" % (station, key), response['interface'][key], tags, time)
time.sleep(monitor_interval)

View File

@@ -666,9 +666,11 @@ class L3VariableTime(Realm):
tags['station-count'] = sta_count tags['station-count'] = sta_count
tags["script"] = 'test_l3_longevity' tags["script"] = 'test_l3_longevity'
self.influxdb.post_to_influx("total-download-bps", total_dl_bps, tags) time = str(datetime.datetime.utcnow().isoformat())
self.influxdb.post_to_influx("total-upload-bps", total_ul_bps, tags)
self.influxdb.post_to_influx("total-bi-directional-bps", total_ul_bps + total_dl_bps, tags) self.influxdb.post_to_influx("total-download-bps", total_dl_bps, tags, time)
self.influxdb.post_to_influx("total-upload-bps", total_ul_bps, tags, time)
self.influxdb.post_to_influx("total-bi-directional-bps", total_ul_bps + total_dl_bps, tags, time)
# Stop traffic and admin down stations. # Stop traffic and admin down stations.
def stop(self): def stop(self):
@@ -909,9 +911,9 @@ python3 test_l3_longevity.py --cisco_ctlr 192.168.100.112 --cisco_dfs True --mgr
parser.add_argument('--influx_host', help='Hostname for the Influx database') parser.add_argument('--influx_host', help='Hostname for the Influx database')
parser.add_argument('--influx_port', help='IP Port for the Influx database', default=8086) parser.add_argument('--influx_port', help='IP Port for the Influx database', default=8086)
parser.add_argument('--influx_user', help='Username for the Influx database') parser.add_argument('--influx_org', help='Organization for the Influx database')
parser.add_argument('--influx_passwd', help='Password for the Influx database') parser.add_argument('--influx_token', help='Token for the Influx database')
parser.add_argument('--influx_db', help='Name of the Influx database') parser.add_argument('--influx_bucket', help='Name of the Influx bucket')
parser.add_argument("--cap_ctl_out", help="--cap_ctl_out , switch the cisco controller output will be captured", action='store_true') parser.add_argument("--cap_ctl_out", help="--cap_ctl_out , switch the cisco controller output will be captured", action='store_true')
parser.add_argument("--wait", help="--wait <time> , time to wait at the end of the test", default='0') parser.add_argument("--wait", help="--wait <time> , time to wait at the end of the test", default='0')
@@ -945,15 +947,15 @@ python3 test_l3_longevity.py --cisco_ctlr 192.168.100.112 --cisco_dfs True --mgr
print("csv output file : {}".format(csv_outfile)) print("csv output file : {}".format(csv_outfile))
influxdb = None influxdb = None
if args.influx_db is not None: if args.influx_bucket is not None:
from influx import RecordInflux from influx2 import RecordInflux
influxdb = RecordInflux(_lfjson_host=lfjson_host, influxdb = RecordInflux(_lfjson_host=lfjson_host,
_lfjson_port=lfjson_port, _lfjson_port=lfjson_port,
_influx_host=args.influx_host, _influx_host=args.influx_host,
_influx_port=args.influx_port, _influx_port=args.influx_port,
_influx_db=args.influx_db, _influx_org=args.influx_org,
_influx_user=args.influx_user, _influx_token=args.influx_token,
_influx_passwd=args.influx_passwd) _influx_bucket=args.influx_bucket)
MAX_NUMBER_OF_STATIONS = 1000 MAX_NUMBER_OF_STATIONS = 1000