blob: 8008f7dd3f4827cc4cc9cb02c31b3bb0be52f3db [file] [log] [blame]
lavado456d0f32019-11-15 17:04:02 -05001# -*- coding: utf-8 -*-
2
3# Copyright 2018 Whitestack, LLC
4# *************************************************************
5
6# This file is part of OSM Monitoring module
7# All Rights Reserved to Whitestack, LLC
8
9# Licensed under the Apache License, Version 2.0 (the "License"); you may
10# not use this file except in compliance with the License. You may obtain
11# a copy of the License at
12
13# http://www.apache.org/licenses/LICENSE-2.0
14
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18# License for the specific language governing permissions and limitations
19# under the License.
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact: glavado@whitestack.com
22##
23import logging
24
25from osm_mon.core.common_db import CommonDbClient
26from osm_mon.core.config import Config
27import osm_mon.dashboarder.backends.grafana as grafana
Gianpietro Lavado1d71df52019-12-02 17:41:20 +000028from osm_mon import __path__ as mon_path
lavado456d0f32019-11-15 17:04:02 -050029
30log = logging.getLogger(__name__)
31
32
33class DashboarderService:
34 def __init__(self, config: Config):
35 self.conf = config
36 self.common_db = CommonDbClient(self.conf)
37
38 def create_dashboards(self):
39 # TODO lavado: migrate these methods to mongo change streams
40 # Lists all dashboards and OSM resources for later comparisons
41 dashboard_uids = grafana.get_all_dashboard_uids()
42 osm_resource_uids = []
43
44 # Reads existing project list and creates a dashboard for each
45 projects = self.common_db.get_projects()
46 for project in projects:
47 project_id = project['_id']
48 # Collect Project IDs for periodical dashboard clean-up
49 osm_resource_uids.append(project_id)
Gianpietro Lavado6fb69482019-12-03 15:16:14 +000050 dashboard_path = '{}/dashboarder/templates/project_scoped.json'.format(mon_path[0])
lavado456d0f32019-11-15 17:04:02 -050051 if project_id not in dashboard_uids:
52 project_name = project['name']
53 grafana.create_dashboard(project_id, project_name,
54 dashboard_path)
55 log.debug('Created dashboard for Project: %s', project_id)
56 else:
57 log.debug('Dashboard already exists')
58
59 # Reads existing NS list and creates a dashboard for each
60 # TODO lavado: only create for ACTIVE NSRs
61 nsrs = self.common_db.get_nsrs()
62 for nsr in nsrs:
63 nsr_id = nsr['_id']
Gianpietro Lavado6fb69482019-12-03 15:16:14 +000064 dashboard_path = '{}/dashboarder/templates/ns_scoped.json'.format(mon_path[0])
lavado456d0f32019-11-15 17:04:02 -050065 # Collect NS IDs for periodical dashboard clean-up
66 osm_resource_uids.append(nsr_id)
67 # Check if the NSR's VNFDs contain metrics
68 constituent_vnfds = nsr['nsd']['constituent-vnfd']
69 for constituent_vnfd in constituent_vnfds:
70 try:
71 vnfd = self.common_db.get_vnfd_by_name(constituent_vnfd['vnfd-id-ref'])
72 # If there are metrics, create dashboard (if exists)
73 if 'monitoring-param' in vnfd:
74 if nsr_id not in dashboard_uids:
75 nsr_name = nsr['name']
76 grafana.create_dashboard(nsr_id, nsr_name,
77 dashboard_path)
78 log.debug('Created dashboard for NS: %s', nsr_id)
79 else:
80 log.debug('Dashboard already exists')
81 break
82 else:
83 log.debug('NS does not has metrics')
84 except Exception:
85 log.exception("VNFD is not valid or has been renamed")
86 continue
87
88 # Delete obsolete dashboards
89 for dashboard_uid in dashboard_uids:
90 if dashboard_uid not in osm_resource_uids:
91 grafana.delete_dashboard(dashboard_uid)
92 log.debug('Deleted obsolete dashboard: %s', dashboard_uid)
93 else:
94 log.debug('All dashboards in use')