Allow a user to gather data from only select variables

Signed-off-by: lanforge <stidmatt@gmail.com>
This commit is contained in:
lanforge
2021-04-05 12:38:15 -07:00
parent 430c5b2ad0
commit 8e870862d9
2 changed files with 58 additions and 39 deletions

View File

@@ -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)

View File

@@ -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()