From 8e870862d9194fc31833181e2a60f3963f00ab08 Mon Sep 17 00:00:00 2001 From: lanforge Date: Mon, 5 Apr 2021 12:38:15 -0700 Subject: [PATCH] Allow a user to gather data from only select variables Signed-off-by: lanforge --- py-json/influx.py | 70 +++++++++++++++++++++++--------------- py-scripts/recordinflux.py | 27 +++++++++------ 2 files changed, 58 insertions(+), 39 deletions(-) diff --git a/py-json/influx.py b/py-json/influx.py index dcc4b705..f33c07d0 100644 --- a/py-json/influx.py +++ b/py-json/influx.py @@ -13,7 +13,8 @@ import datetime from LANforge.lfcli_base import LFCliBase import time -class Influx(LFCliBase): + +class RecordInflux(LFCliBase): def __init__(self, _host="localhost", _port=8080, @@ -22,6 +23,7 @@ class Influx(LFCliBase): _influx_db=None, _longevity=None, _monitor_interval=None, + _target_kpi=None, _devices=None, _debug_on=False, _exit_on_fail=False @@ -30,40 +32,52 @@ class Influx(LFCliBase): _port, _debug=_debug_on, _exit_on_fail=_exit_on_fail) - self.host=_host - self.port=_port - 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.host = _host + self.port = _port + 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 = [ + { + "measurement": station + ' ' + key, + "tags": { + "host": self.host, + "region": "us-west" + }, + "time": str(datetime.datetime.utcnow().isoformat()), + "fields": { + "value": response['interface'][key] + } + } + ] + client.write_points(json_body) + def getdata(self): - url='http://192.168.1.32:8080/port/1/1/' + url = 'http://192.168.1.32: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, self.longevity) while datetime.datetime.now() < end: - stations=self.stations + stations = self.stations for station in stations: - url1=url+station + url1 = url + station response = json.loads(requests.get(url1).text) - for key in response['interface'].keys(): - json_body = [ - { - "measurement": station+' '+key, - "tags": { - "host": self.host, - "region": "us-west" - }, - "time":str(datetime.datetime.utcnow().isoformat()), - "fields": { - "value":response['interface'][key] - } - } - ] - client.write_points(json_body) + if self.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'] + 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) diff --git a/py-scripts/recordinflux.py b/py-scripts/recordinflux.py index 61b27a60..c33914b2 100755 --- a/py-scripts/recordinflux.py +++ b/py-scripts/recordinflux.py @@ -21,9 +21,10 @@ if 'py-json' not in sys.path: sys.path.append(os.path.join(os.path.abspath('..'), 'py-json')) from LANforge.lfcli_base import LFCliBase -from influx import Influx +from influx import RecordInflux import argparse + def main(): parser = LFCliBase.create_bare_argparse( prog='recordinflux.py', @@ -40,24 +41,28 @@ def main(): --stations \\ --longevity 5h''' ) + target_kpi = ['bps rx', 'rx bytes', 'pps rx', 'rx pkts', 'rx drop'] 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) parser.add_argument('--longevity', help='How long you want to gather data', default='4h') parser.add_argument('--device', help='Device to monitor', action='append', required=True) parser.add_argument('--monitor_interval', help='How frequently you want to append to your database', default='5s') + parser.add_argument('--target_kpi', help='Monitor only selected columns', action='append', default=target_kpi) args = parser.parse_args() - monitor_interval=LFCliBase.parse_time(args.monitor_interval).total_seconds() - longevity=LFCliBase.parse_time(args.longevity).total_seconds() - grapher=Influx(_host=args.mgr, - _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) + monitor_interval = LFCliBase.parse_time(args.monitor_interval).total_seconds() + longevity = LFCliBase.parse_time(args.longevity).total_seconds() + grapher = RecordInflux(_host=args.mgr, + _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() + if __name__ == "__main__": main()