From 08401996b296fec427df4ab0e2ab7dc08290897e Mon Sep 17 00:00:00 2001 From: Ben Greear Date: Thu, 8 Apr 2021 22:02:34 -0700 Subject: [PATCH] l3-longevity: Add influxdb-2 API This properly inserts data into the c7-graphana system. Signed-off-by: Ben Greear --- py-json/influx.py | 1 - py-json/influx2.py | 86 +++++++++++++++++++++++++++++++++ py-scripts/test_l3_longevity.py | 24 ++++----- 3 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 py-json/influx2.py diff --git a/py-json/influx.py b/py-json/influx.py index 1a85a3a7..b6725316 100644 --- a/py-json/influx.py +++ b/py-json/influx.py @@ -44,7 +44,6 @@ class RecordInflux(LFCliBase): def post_to_influx(self, key, value, tags): data = dict() data["measurement"] = key - tags["host"] = self.influx_host data["tags"] = tags data["time"] = str(datetime.datetime.utcnow().isoformat()) data["fields"] = dict() diff --git a/py-json/influx2.py b/py-json/influx2.py new file mode 100644 index 00000000..a25c2634 --- /dev/null +++ b/py-json/influx2.py @@ -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) diff --git a/py-scripts/test_l3_longevity.py b/py-scripts/test_l3_longevity.py index 0c6b0735..9dbe617e 100755 --- a/py-scripts/test_l3_longevity.py +++ b/py-scripts/test_l3_longevity.py @@ -666,9 +666,11 @@ class L3VariableTime(Realm): tags['station-count'] = sta_count tags["script"] = 'test_l3_longevity' - self.influxdb.post_to_influx("total-download-bps", total_dl_bps, tags) - 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) + time = str(datetime.datetime.utcnow().isoformat()) + + 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. 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_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_passwd', help='Password for the Influx database') - parser.add_argument('--influx_db', help='Name of the Influx database') + parser.add_argument('--influx_org', help='Organization for the Influx database') + parser.add_argument('--influx_token', help='Token for 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("--wait", help="--wait