mirror of
https://github.com/Telecominfraproject/wlan-lanforge-scripts.git
synced 2025-11-01 03:07:56 +00:00
merge latest lanforge-scripts repo
Signed-off-by: shivamcandela <shivam.thakur@candelatech.com>
This commit is contained in:
@@ -7,165 +7,23 @@ It gets the columns of the files and from that it automatically determines the n
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
import importlib
|
||||
import argparse
|
||||
|
||||
if sys.version_info[0] != 3:
|
||||
print("This script requires Python 3")
|
||||
exit(1)
|
||||
|
||||
if 'py-json' not in sys.path:
|
||||
sys.path.append(os.path.join(os.path.abspath('..'), 'py-json'))
|
||||
sys.path.append(os.path.join(os.path.abspath('..'), 'py-dashboard'))
|
||||
sys.path.append(os.path.join(os.path.abspath(__file__ + "../../../")))
|
||||
|
||||
from GrafanaRequest import GrafanaRequest
|
||||
from LANforge.lfcli_base import LFCliBase
|
||||
import string
|
||||
<<<<<<< HEAD
|
||||
import random
|
||||
# from GrafanaRequest import GrafanaRequest
|
||||
GrafanaRequest = importlib.import_module("py-dashboard.GrafanaRequest")
|
||||
GrafanaRequest = GrafanaRequest.GrafanaRequest
|
||||
lfcli_base = importlib.import_module("py-json.LANforge.lfcli_base")
|
||||
LFCliBase = lfcli_base.LFCliBase
|
||||
|
||||
|
||||
class UseGrafana(LFCliBase):
|
||||
def __init__(self,
|
||||
_grafana_token,
|
||||
host="localhost",
|
||||
_grafana_host="localhost",
|
||||
port=8080,
|
||||
_debug_on=False,
|
||||
_exit_on_fail=False,
|
||||
_grafana_port=3000):
|
||||
super().__init__(host, port, _debug=_debug_on, _exit_on_fail=_exit_on_fail)
|
||||
self.grafana_token = _grafana_token
|
||||
self.grafana_port = _grafana_port
|
||||
self.grafana_host = _grafana_host
|
||||
self.GR = GrafanaRequest(self.grafana_host, str(self.grafana_port), _folderID=0, _api_token=self.grafana_token)
|
||||
|
||||
def create_dashboard(self,
|
||||
dashboard_name):
|
||||
return self.GR.create_dashboard(dashboard_name)
|
||||
|
||||
def delete_dashboard(self,
|
||||
dashboard_uid):
|
||||
return self.GR.delete_dashboard(dashboard_uid)
|
||||
|
||||
def list_dashboards(self):
|
||||
return self.GR.list_dashboards()
|
||||
|
||||
def create_dashboard_from_data(self,
|
||||
json_file):
|
||||
return self.GR.create_dashboard_from_data(json_file=json_file)
|
||||
|
||||
def groupby(self, params, grouptype):
|
||||
dic = dict()
|
||||
dic['params'] = list()
|
||||
dic['params'].append(params)
|
||||
dic['type'] = grouptype
|
||||
return dic
|
||||
|
||||
def maketargets(self,
|
||||
bucket,
|
||||
scriptname,
|
||||
groupBy,
|
||||
index,
|
||||
graph_group,
|
||||
testbed):
|
||||
query = (
|
||||
'from(bucket: "%s")\n '
|
||||
'|> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n '
|
||||
'|> filter(fn: (r) => r["script"] == "%s")\n '
|
||||
'|> group(columns: ["_measurement"])\n '
|
||||
% (bucket, scriptname))
|
||||
queryend = ('|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)\n '
|
||||
'|> yield(name: "mean")\n ')
|
||||
if graph_group is not None:
|
||||
graphgroup = ('|> filter(fn: (r) => r["Graph-Group"] == "%s")\n' % graph_group)
|
||||
query += graphgroup
|
||||
if testbed is not None:
|
||||
query += ('|> filter(fn: (r) => r["testbed"] == "%s")\n' % testbed)
|
||||
targets = dict()
|
||||
targets['delimiter'] = ','
|
||||
targets['groupBy'] = groupBy
|
||||
targets['header'] = True
|
||||
targets['ignoreUnknown'] = False
|
||||
targets['orderByTime'] = 'ASC'
|
||||
targets['policy'] = 'default'
|
||||
targets['query'] = query + queryend
|
||||
targets['refId'] = dict(enumerate(string.ascii_uppercase, 1))[index + 1]
|
||||
targets['resultFormat'] = "time_series"
|
||||
targets['schema'] = list()
|
||||
targets['skipRows'] = 0
|
||||
targets['tags'] = list()
|
||||
return targets
|
||||
|
||||
def create_custom_dashboard(self,
|
||||
scripts=None,
|
||||
title=None,
|
||||
bucket=None,
|
||||
graph_groups=None,
|
||||
graph_groups_file=None,
|
||||
testbed=None,
|
||||
datasource='InfluxDB',
|
||||
from_date='now-1y',
|
||||
graph_height=8,
|
||||
graph__width=12):
|
||||
options = string.ascii_lowercase + string.ascii_uppercase + string.digits
|
||||
uid = ''.join(random.choice(options) for i in range(9))
|
||||
input1 = dict()
|
||||
annotations = dict()
|
||||
annotations['builtIn'] = 1
|
||||
annotations['datasource'] = '-- Grafana --'
|
||||
annotations['enable'] = True
|
||||
annotations['hide'] = True
|
||||
annotations['iconColor'] = 'rgba(0, 211, 255, 1)'
|
||||
annotations['name'] = 'Annotations & Alerts'
|
||||
annotations['type'] = 'dashboard'
|
||||
annot = dict()
|
||||
annot['list'] = list()
|
||||
annot['list'].append(annotations)
|
||||
|
||||
templating = dict()
|
||||
templating['list'] = list()
|
||||
|
||||
timedict = dict()
|
||||
timedict['from'] = from_date
|
||||
timedict['to'] = 'now'
|
||||
|
||||
panels = list()
|
||||
index = 1
|
||||
if graph_groups_file:
|
||||
print("graph_groups_file: %s" % graph_groups_file)
|
||||
target_csvs = open(graph_groups_file).read().split('\n')
|
||||
graph_groups = self.get_graph_groups(target_csvs) # Get the list of graph groups which are in the tests we ran
|
||||
unit_dict = dict()
|
||||
for csv in target_csvs:
|
||||
if len(csv)>1:
|
||||
print(csv)
|
||||
unit_dict.update(self.get_units(csv))
|
||||
for scriptname in graph_groups.keys():
|
||||
for graph_group in graph_groups[scriptname]:
|
||||
panel = dict()
|
||||
|
||||
gridpos = dict()
|
||||
gridpos['h'] = graph_height
|
||||
gridpos['w'] = graph__width
|
||||
gridpos['x'] = 0
|
||||
gridpos['y'] = 0
|
||||
|
||||
legend = dict()
|
||||
legend['avg'] = False
|
||||
legend['current'] = False
|
||||
legend['max'] = False
|
||||
legend['min'] = False
|
||||
legend['show'] = True
|
||||
legend['total'] = False
|
||||
legend['values'] = False
|
||||
|
||||
options = dict()
|
||||
options['alertThreshold'] = True
|
||||
=======
|
||||
|
||||
class UseGrafana(GrafanaRequest):
|
||||
>>>>>>> 0ef021e1165cbaa612e5128bc48d6abfbb7b887b
|
||||
|
||||
|
||||
def read_csv(self, file):
|
||||
csv = open(file).read().split('\n')
|
||||
@@ -182,7 +40,6 @@ class UseGrafana(GrafanaRequest):
|
||||
results.append(row[value])
|
||||
return results
|
||||
|
||||
|
||||
def get_units(self, target_csv):
|
||||
csv = self.read_csv(target_csv)
|
||||
graph_group = self.get_values(csv, 'Graph-Group')
|
||||
@@ -190,7 +47,6 @@ class UseGrafana(GrafanaRequest):
|
||||
return dict(zip(graph_group, units))
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
parser = LFCliBase.create_basic_argparse(
|
||||
prog='grafana_profile.py',
|
||||
@@ -204,7 +60,7 @@ def main():
|
||||
--grafana_token
|
||||
--dashbaord_name
|
||||
--scripts "Wifi Capacity"
|
||||
|
||||
|
||||
Create a custom dashboard with the following command:
|
||||
./grafana_profile.py --create_custom yes
|
||||
--title Dataplane
|
||||
@@ -213,7 +69,7 @@ def main():
|
||||
--graph_groups 'Per Stations Rate DL'
|
||||
--graph_groups 'Per Stations Rate UL'
|
||||
--graph_groups 'Per Stations Rate UL+DL'
|
||||
|
||||
|
||||
Create a snapshot of a dashboard:
|
||||
./grafana_profile.py --grafana_token TOKEN
|
||||
--grafana_host HOST
|
||||
@@ -240,7 +96,8 @@ def main():
|
||||
optional.add_argument('--influx_bucket', help='Name of your Influx Bucket', default=None)
|
||||
optional.add_argument('--graph_groups', help='How you want to filter your graphs on your dashboard',
|
||||
action='append', default=[None])
|
||||
optional.add_argument('--graph_groups_file', help='File which determines how you want to filter your graphs on your dashboard',
|
||||
optional.add_argument('--graph_groups_file',
|
||||
help='File which determines how you want to filter your graphs on your dashboard',
|
||||
default=None)
|
||||
optional.add_argument('--testbed', help='Which testbed you want to query', default=None)
|
||||
optional.add_argument('--kpi', help='KPI file(s) which you want to graph form', action='append', default=None)
|
||||
@@ -248,11 +105,13 @@ def main():
|
||||
optional.add_argument('--from_date', help='Date you want to start your Grafana dashboard from', default='now-1y')
|
||||
optional.add_argument('--graph_height', help='Custom height for the graph on grafana dashboard', default=8)
|
||||
optional.add_argument('--graph_width', help='Custom width for the graph on grafana dashboard', default=12)
|
||||
optional.add_argument('--create_snapshot', action='store_true')
|
||||
optional.add_argument('--list_snapshots', action='store_true')
|
||||
args = parser.parse_args()
|
||||
|
||||
Grafana = UseGrafana(args.grafana_token,
|
||||
args.grafana_port,
|
||||
args.grafana_host
|
||||
args.grafana_host,
|
||||
grafanajson_port=args.grafana_port
|
||||
)
|
||||
if args.dashboard_name is not None:
|
||||
Grafana.create_dashboard(args.dashboard_name)
|
||||
@@ -267,7 +126,7 @@ def main():
|
||||
Grafana.create_dashboard_from_data(args.dashboard_json)
|
||||
|
||||
if args.kpi is not None:
|
||||
args.graph_groups = args.graph_groups+Grafana.get_graph_groups(args.graph_groups)
|
||||
args.graph_groups = args.graph_groups + Grafana.get_graph_groups(args.graph_groups)
|
||||
|
||||
if args.create_custom:
|
||||
Grafana.create_custom_dashboard(scripts=args.scripts,
|
||||
@@ -281,6 +140,12 @@ def main():
|
||||
graph_height=args.graph_height,
|
||||
graph__width=args.graph_width)
|
||||
|
||||
if args.create_snapshot:
|
||||
Grafana.create_snapshot(args.title)
|
||||
|
||||
if args.list_snapshots:
|
||||
Grafana.list_snapshots()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user