From fe45a2c657f23915fd3987be41ebe81f79803c20 Mon Sep 17 00:00:00 2001 From: lanforge Date: Wed, 7 Apr 2021 12:35:27 -0700 Subject: [PATCH] Option to paste data from test_ipv4_variable_time to influx database Signed-off-by: lanforge --- py-json/influx.py | 28 ++++------ py-json/l3_cxprofile2.py | 1 - py-scripts/recordinflux.py | 11 ++-- py-scripts/test_ipv4_variable_time.py | 79 ++++++++++++++++----------- 4 files changed, 62 insertions(+), 57 deletions(-) diff --git a/py-json/influx.py b/py-json/influx.py index 8304f5ad..d114483c 100644 --- a/py-json/influx.py +++ b/py-json/influx.py @@ -21,13 +21,8 @@ class RecordInflux(LFCliBase): _influx_user=None, _influx_passwd=None, _influx_db=None, - _longevity=None, - _monitor_interval=None, - _target_kpi=None, - _devices=None, _debug_on=False, - _exit_on_fail=False - ): + _exit_on_fail=False): super().__init__(_influx_host, _port, _debug=_debug_on, @@ -37,10 +32,6 @@ class RecordInflux(LFCliBase): self.influx_user = _influx_user self.influx_passwd = _influx_passwd self.influx_db = _influx_db - self.longevity = _longevity - self.stations = _devices - self.monitor_interval = _monitor_interval - self.target_kpi = _target_kpi def posttoinflux(self,station,key,response,client): json_body = [ @@ -58,26 +49,29 @@ class RecordInflux(LFCliBase): ] client.write_points(json_body) - def getdata(self): + def getdata(self, + devices=None, + target_kpi=None, + longevity=None, + monitor_interval=None): url = 'http://'+self.host+':8080/port/1/1/' client = InfluxDBClient(self.host, 8086, self.influx_user, self.influx_passwd, self.influx_db) - end = datetime.datetime.now() + datetime.timedelta(0, self.longevity) + end = datetime.datetime.now() + datetime.timedelta(0, longevity) while datetime.datetime.now() < end: - stations = self.stations - for station in stations: + for station in devices: url1 = url + station response = json.loads(requests.get(url1).text) - if self.target_kpi is None: + if target_kpi is None: for key in response['interface'].keys(): self.posttoinflux(station, key, response, client) else: - targets=self.target_kpi+['ip','ipv6 address','alias','mac'] + 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, client) - time.sleep(self.monitor_interval) + time.sleep(monitor_interval) diff --git a/py-json/l3_cxprofile2.py b/py-json/l3_cxprofile2.py index f9aa34e1..0eb67df4 100644 --- a/py-json/l3_cxprofile2.py +++ b/py-json/l3_cxprofile2.py @@ -2,7 +2,6 @@ import re import time import pprint -from pprint import pprint from lfdata import LFDataCollection from base_profile import BaseProfile import os diff --git a/py-scripts/recordinflux.py b/py-scripts/recordinflux.py index 3f6f78d4..05483a9c 100755 --- a/py-scripts/recordinflux.py +++ b/py-scripts/recordinflux.py @@ -56,12 +56,11 @@ def main(): _port=args.mgr_port, _influx_db=args.influx_db, _influx_user=args.influx_user, - _influx_passwd=args.influx_passwd, - _longevity=longevity, - _devices=args.device, - _monitor_interval=monitor_interval, - _target_kpi=args.target_kpi) - grapher.getdata() + _influx_passwd=args.influx_passwd) + grapher.getdata(longevity=longevity, + devices=args.device, + monitor_interval=monitor_interval, + target_kpi=args.target_kpi) if __name__ == "__main__": diff --git a/py-scripts/test_ipv4_variable_time.py b/py-scripts/test_ipv4_variable_time.py index d352261e..e6077784 100755 --- a/py-scripts/test_ipv4_variable_time.py +++ b/py-scripts/test_ipv4_variable_time.py @@ -20,7 +20,7 @@ if sys.version_info[0] != 3: if 'py-json' not in sys.path: sys.path.append(os.path.join(os.path.abspath('..'), 'py-json')) - + import argparse from LANforge import LFUtils from realm import Realm @@ -139,25 +139,6 @@ class IPV4VariableTime(Realm): def main(): - optional = [] - optional.append({'name': '--mode', 'help': 'Used to force mode of stations'}) - optional.append({'name': '--ap', 'help': 'Used to force a connection to a particular AP'}) - optional.append({'name': '--output_format', 'help': 'choose either csv or xlsx'}) - optional.append({'name': '--report_file', 'help': 'where you want to store results', 'default': None}) - optional.append({'name': '--a_min', 'help': '--a_min bps rate minimum for side_a', 'default': 256000}) - optional.append({'name': '--b_min', 'help': '--b_min bps rate minimum for side_b', 'default': 256000}) - optional.append( - {'name': '--test_duration', 'help': '--test_duration sets the duration of the test', 'default': "2m"}) - optional.append({'name': '--layer3_cols', 'help': 'Columns wished to be monitored from layer 3 endpoint tab', - 'default': ['name', 'tx bytes', 'rx bytes','tx rate','rx rate']}) - optional.append({'name': '--port_mgr_cols', 'help': 'Columns wished to be monitored from port manager tab', - 'default': ['ap', 'ip', 'parent dev']}) - optional.append( - {'name': '--compared_report', 'help': 'report path and file which is wished to be compared with new report', - 'default': None}) - optional.append({'name': '--monitor_interval', - 'help': 'how frequently do you want your monitor function to take measurements; 250ms, 35s, 2h', - 'default': '10s'}) parser = Realm.create_basic_argparse( prog='test_ipv4_variable_time.py', formatter_class=argparse.RawTextHelpFormatter, @@ -266,8 +247,27 @@ python3 ./test_ipv4_variable_time.py Elapsed | 'elapsed' Destination Addr | 'destination addr' Source Addr | 'source addr' - ''', - more_optional=optional) + ''') + + parser.add_argument('--mode', help='Used to force mode of stations') + parser.add_argument('--ap', help='Used to force a connection to a particular AP') + parser.add_argument('--output_format', help='choose either csv or xlsx') + parser.add_argument('--report_file', help='where you want to store results', default=None) + parser.add_argument('--a_min', help='--a_min bps rate minimum for side_a', default=256000) + parser.add_argument('--b_min', help='--b_min bps rate minimum for side_b', default=256000) + parser.add_argument('--test_duration', help='--test_duration sets the duration of the test', default="2m") + parser.add_argument('--layer3_cols', help='Columns wished to be monitored from layer 3 endpoint tab', + default=['name', 'tx bytes', 'rx bytes', 'tx rate', 'rx rate']) + parser.add_argument('--port_mgr_cols', help='Columns wished to be monitored from port manager tab', + default=['ap', 'ip', 'parent dev']) + parser.add_argument('--compared_report', help='report path and file which is wished to be compared with new report', + default=None) + parser.add_argument('--monitor_interval', + help='how frequently do you want your monitor function to take measurements; 250ms, 35s, 2h', + default='10s') + parser.add_argument('--influx_user', help='Username for your Influx database', required=True) + parser.add_argument('--influx_passwd', help='Password for your Influx database', required=True) + parser.add_argument('--influx_db', help='Name of your Influx database', required=True) args = parser.parse_args() @@ -283,7 +283,7 @@ python3 ./test_ipv4_variable_time.py if args.report_file is None: new_file_path = str(datetime.datetime.now().strftime("%Y-%m-%d-%H-h-%M-m-%S-s")).replace(':', - '-') + '_test_ipv4_variable_time' # create path name + '-') + '_test_ipv4_variable_time' # create path name try: path = os.path.join('/home/lanforge/report-data/', new_file_path) os.mkdir(path) @@ -291,14 +291,15 @@ python3 ./test_ipv4_variable_time.py curr_dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) path = os.path.join(curr_dir_path, new_file_path) os.mkdir(path) - systeminfopath=str(path)+'/systeminfo.txt' + systeminfopath = str(path) + '/systeminfo.txt' - if args.output_format in ['csv', 'json', 'html', 'hdf','stata', 'pickle', 'pdf', 'png', 'parquet', + if args.output_format in ['csv', 'json', 'html', 'hdf', 'stata', 'pickle', 'pdf', 'png', 'parquet', 'xlsx']: report_f = str(path) + '/data.' + args.output_format output = args.output_format else: - print('Not supporting this report format or cannot find report format provided. Defaulting to csv data file output type, naming it data.csv.') + print( + 'Not supporting this report format or cannot find report format provided. Defaulting to csv data file output type, naming it data.csv.') report_f = str(path) + '/data.csv' output = 'csv' @@ -314,14 +315,14 @@ python3 ./test_ipv4_variable_time.py # Retrieve last data file compared_rept = None if args.compared_report: - compared_report_format=args.compared_report.split('.')[-1] - #if compared_report_format not in ['csv', 'json', 'dta', 'pkl','html','xlsx','parquet','h5']: + compared_report_format = args.compared_report.split('.')[-1] + # if compared_report_format not in ['csv', 'json', 'dta', 'pkl','html','xlsx','parquet','h5']: if compared_report_format != 'csv': print(ValueError("Cannot process this file type. Please select a different file and re-run script.")) exit(1) else: - compared_rept=args.compared_report - + compared_rept = args.compared_report + station_list = LFUtils.portNameSeries(prefix_="sta", start_id_=0, end_id_=num_sta - 1, padding_number_=10000, radio=args.radio) ip_var_test = IPV4VariableTime(host=args.mgr, @@ -347,7 +348,6 @@ python3 ./test_ipv4_variable_time.py if not ip_var_test.passes(): print(ip_var_test.get_fail_message()) ip_var_test.exit_fail() - try: layer3connections = ','.join([[*x.keys()][0] for x in ip_var_test.json_get('endp')['endpoint']]) @@ -381,12 +381,13 @@ python3 ./test_ipv4_variable_time.py monitor_interval = Realm.parse_time(args.monitor_interval).total_seconds() except ValueError as error: print(str(error)) - print(ValueError("The time string provided for monitor_interval argument is invalid. Please see supported time stamp increments and inputs for monitor_interval in --help. ")) + print(ValueError( + "The time string provided for monitor_interval argument is invalid. Please see supported time stamp increments and inputs for monitor_interval in --help. ")) exit(1) ip_var_test.start(False, False) ip_var_test.l3cxprofile.monitor(layer3_cols=layer3_cols, sta_list=station_list, - #port_mgr_cols=port_mgr_cols, + # port_mgr_cols=port_mgr_cols, report_file=report_f, systeminfopath=systeminfopath, duration_sec=Realm.parse_time(args.test_duration).total_seconds(), @@ -403,6 +404,18 @@ python3 ./test_ipv4_variable_time.py print(ip_var_test.get_fail_message()) ip_var_test.exit_fail() time.sleep(30) + if args.influx_db is not None: + from influx import RecordInflux + grapher = RecordInflux(_influx_host=args.mgr, + _port=args.mgr_port, + _influx_db=args.influx_db, + _influx_user=args.influx_user, + _influx_passwd=args.influx_passwd) + devices=[station.split('.')[-1] for station in station_list] + grapher.getdata(longevity=5, + devices=devices, + monitor_interval=2, + target_kpi=['bps rx']) ip_var_test.cleanup() if ip_var_test.passes(): ip_var_test.success()