| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 1 | # -*- 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 | ## |
| 23 | import logging |
| 24 | |
| 25 | from osm_mon.core.common_db import CommonDbClient |
| 26 | from osm_mon.core.config import Config |
| 27 | import osm_mon.dashboarder.backends.grafana as grafana |
| 28 | |
| 29 | log = logging.getLogger(__name__) |
| 30 | |
| 31 | |
| 32 | class DashboarderService: |
| 33 | def __init__(self, config: Config): |
| 34 | self.conf = config |
| 35 | self.common_db = CommonDbClient(self.conf) |
| 36 | |
| 37 | def create_dashboards(self): |
| 38 | # TODO lavado: migrate these methods to mongo change streams |
| 39 | # Lists all dashboards and OSM resources for later comparisons |
| 40 | dashboard_uids = grafana.get_all_dashboard_uids() |
| 41 | osm_resource_uids = [] |
| 42 | |
| 43 | # Reads existing project list and creates a dashboard for each |
| 44 | projects = self.common_db.get_projects() |
| 45 | for project in projects: |
| 46 | project_id = project['_id'] |
| 47 | # Collect Project IDs for periodical dashboard clean-up |
| 48 | osm_resource_uids.append(project_id) |
| 49 | dashboard_path = '/mon/osm_mon/dashboarder/templates/project_scoped.json' |
| 50 | if project_id not in dashboard_uids: |
| 51 | project_name = project['name'] |
| 52 | grafana.create_dashboard(project_id, project_name, |
| 53 | dashboard_path) |
| 54 | log.debug('Created dashboard for Project: %s', project_id) |
| 55 | else: |
| 56 | log.debug('Dashboard already exists') |
| 57 | |
| 58 | # Reads existing NS list and creates a dashboard for each |
| 59 | # TODO lavado: only create for ACTIVE NSRs |
| 60 | nsrs = self.common_db.get_nsrs() |
| 61 | for nsr in nsrs: |
| 62 | nsr_id = nsr['_id'] |
| 63 | dashboard_path = '/mon/osm_mon/dashboarder/templates/ns_scoped.json' |
| 64 | # Collect NS IDs for periodical dashboard clean-up |
| 65 | osm_resource_uids.append(nsr_id) |
| 66 | # Check if the NSR's VNFDs contain metrics |
| 67 | constituent_vnfds = nsr['nsd']['constituent-vnfd'] |
| 68 | for constituent_vnfd in constituent_vnfds: |
| 69 | try: |
| 70 | vnfd = self.common_db.get_vnfd_by_name(constituent_vnfd['vnfd-id-ref']) |
| 71 | # If there are metrics, create dashboard (if exists) |
| 72 | if 'monitoring-param' in vnfd: |
| 73 | if nsr_id not in dashboard_uids: |
| 74 | nsr_name = nsr['name'] |
| 75 | grafana.create_dashboard(nsr_id, nsr_name, |
| 76 | dashboard_path) |
| 77 | log.debug('Created dashboard for NS: %s', nsr_id) |
| 78 | else: |
| 79 | log.debug('Dashboard already exists') |
| 80 | break |
| 81 | else: |
| 82 | log.debug('NS does not has metrics') |
| 83 | except Exception: |
| 84 | log.exception("VNFD is not valid or has been renamed") |
| 85 | continue |
| 86 | |
| 87 | # Delete obsolete dashboards |
| 88 | for dashboard_uid in dashboard_uids: |
| 89 | if dashboard_uid not in osm_resource_uids: |
| 90 | grafana.delete_dashboard(dashboard_uid) |
| 91 | log.debug('Deleted obsolete dashboard: %s', dashboard_uid) |
| 92 | else: |
| 93 | log.debug('All dashboards in use') |