X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=osm_mon%2Fdashboarder%2Fservice.py;fp=osm_mon%2Fdashboarder%2Fservice.py;h=a0a80a754c374c59ccfc20ccb8fc1f894885fbed;hb=5d7b0d1beb1db2f0939604482e7998a6c1bc6951;hp=0a1f94733155d3a31ac88e508eaf57d195b1d784;hpb=3f176d90a43f9e95954111238299a0175fa845b8;p=osm%2FMON.git diff --git a/osm_mon/dashboarder/service.py b/osm_mon/dashboarder/service.py index 0a1f947..a0a80a7 100644 --- a/osm_mon/dashboarder/service.py +++ b/osm_mon/dashboarder/service.py @@ -28,6 +28,7 @@ from osm_mon.core.keystone import KeystoneConnection from osm_mon.dashboarder.backends.grafana import GrafanaBackend from osm_mon import __path__ as mon_path from osm_mon.core.utils import find_in_list, create_filter_from_nsr +import re log = logging.getLogger(__name__) @@ -46,8 +47,12 @@ class DashboarderService: def create_dashboards(self): # TODO lavado: migrate these methods to mongo change streams # Lists all dashboards and OSM resources for later comparisons + datasource_name_substr = self.conf.get("prometheus-operator", "ds_name_substr") + prom_operator_port = self.conf.get("prometheus-operator", "port") dashboard_uids = self.grafana.get_all_dashboard_uids() + datasource_names = self.grafana.get_all_datasource_names(datasource_name_substr) osm_resource_uids = [] + osm_datasource_names = [] projects = [] # Check if keystone is the auth/projects backend and get projects from there @@ -72,6 +77,9 @@ class DashboarderService: dashboard_path = "{}/dashboarder/templates/project_scoped.json".format( mon_path[0] ) + cnf_dashboard_path = "{}/dashboarder/templates/cnf_scoped.json".format( + mon_path[0] + ) if project_id not in dashboard_uids: project_name = project["name"] if project_name != "admin": @@ -83,6 +91,42 @@ class DashboarderService: else: log.debug("Dashboard already exists") + # Read existing k8s cluster list and creates a dashboard for each + k8sclusters = self.common_db.get_k8sclusters() + for k8scluster in k8sclusters: + k8scluster_id = k8scluster["_id"] + k8scluster_name = k8scluster["name"] + osm_resource_uids.append(k8scluster_id) + osm_datasource_names.append("{}-{}".format(datasource_name_substr, k8scluster_name)) + if k8scluster_id not in dashboard_uids: + projects_read = k8scluster["_admin"]["projects_read"] + if len(projects_read) and projects_read[0] == project_id: + # Collect K8S Cluster IDs for periodical dashboard clean-up + k8scluster_address = k8scluster["credentials"]["clusters"][0]["cluster"]["server"] + # Extract K8S Cluster ip from url + k8scluster_ip = re.findall(r'://([\w\-\.]+)', k8scluster_address)[0] + + # prometheus-operator url + datasource_url = "http://{}:{}".format(k8scluster_ip, prom_operator_port) + + # Create datsource for prometheus-operator in grafana + datasource_type = "prometheus" + datasource_name = "{}-{}".format(datasource_name_substr, k8scluster_name) + if datasource_name not in datasource_names: + self.grafana.create_datasource(datasource_name, datasource_type, datasource_url) + log.debug("Created datasource for k8scluster: %s", k8scluster_id) + + if project["name"] != "admin": + self.grafana.create_dashboard( + k8scluster_id, k8scluster_name, cnf_dashboard_path, project_name=project["name"], + datasource_name=datasource_name) + else: + self.grafana.create_dashboard( + k8scluster_id, k8scluster_name, cnf_dashboard_path, datasource_name=datasource_name) + log.debug("Created dashboard for k8scluster: %s", k8scluster_id) + else: + log.debug("Dashboard already exist for k8scluster: %s", k8scluster_id) + # Reads existing NS list and creates a dashboard for each # TODO lavado: only create for ACTIVE NSRs nsrs = self.common_db.get_nsrs() @@ -124,7 +168,7 @@ class DashboarderService: log.info("Project %s not found", project_id) log.debug("Exception %s" % e) self.grafana.create_dashboard( - nsr_id, nsr_name, dashboard_path, project_name + nsr_id, nsr_name, dashboard_path, project_name=project_name ) log.debug("Created dashboard for NS: %s", nsr_id) else: @@ -144,6 +188,14 @@ class DashboarderService: else: log.debug("All dashboards in use") + # Delete obsolute datasources + for datasource_name in datasource_names: + if datasource_name not in osm_datasource_names: + self.grafana.delete_datasource(datasource_name) + log.debug("Deleted obsolete datasource: %s", datasource_name) + else: + log.debug("All dashboards in use") + def create_grafana_user(self, user): self.grafana.create_grafana_users(user)