| Benjamin Diaz | 4da1466 | 2018-10-17 14:44:36 -0300 | [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: bdiaz@whitestack.com or glavado@whitestack.com |
| 22 | ## |
| Benjamin Diaz | 4da1466 | 2018-10-17 14:44:36 -0300 | [diff] [blame] | 23 | import logging |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 24 | import multiprocessing |
| 25 | import time |
| Benjamin Diaz | 4da1466 | 2018-10-17 14:44:36 -0300 | [diff] [blame] | 26 | |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 27 | from osm_mon.collector.backends.prometheus import PrometheusBackend |
| 28 | from osm_mon.collector.collectors.juju import VCACollector |
| 29 | from osm_mon.collector.collectors.openstack import OpenstackCollector |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 30 | from osm_mon.core.common_db import CommonDbClient |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 31 | from osm_mon.core.database import DatabaseManager |
| Benjamin Diaz | 4da1466 | 2018-10-17 14:44:36 -0300 | [diff] [blame] | 32 | from osm_mon.core.settings import Config |
| 33 | |
| 34 | log = logging.getLogger(__name__) |
| 35 | |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 36 | VIM_COLLECTORS = { |
| 37 | "openstack": OpenstackCollector |
| 38 | } |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 39 | METRIC_BACKENDS = [ |
| 40 | PrometheusBackend |
| 41 | ] |
| Benjamin Diaz | 4da1466 | 2018-10-17 14:44:36 -0300 | [diff] [blame] | 42 | |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 43 | |
| 44 | class Collector: |
| Benjamin Diaz | 4da1466 | 2018-10-17 14:44:36 -0300 | [diff] [blame] | 45 | def __init__(self): |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 46 | self.common_db = CommonDbClient() |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 47 | self.plugins = [] |
| 48 | self.database_manager = DatabaseManager() |
| 49 | self.database_manager.create_tables() |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 50 | self.queue = multiprocessing.Queue() |
| Benjamin Diaz | 4da1466 | 2018-10-17 14:44:36 -0300 | [diff] [blame] | 51 | |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 52 | def init_backends(self): |
| 53 | for backend in METRIC_BACKENDS: |
| 54 | self.plugins.append(backend()) |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 55 | |
| 56 | def collect_forever(self): |
| 57 | log.debug('collect_forever') |
| 58 | cfg = Config.instance() |
| 59 | while True: |
| 60 | try: |
| 61 | self.collect_metrics() |
| 62 | time.sleep(cfg.OSMMON_COLLECTOR_INTERVAL) |
| 63 | except Exception: |
| 64 | log.exception("Error collecting metrics") |
| 65 | |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 66 | def _collect_vim_metrics(self, vnfr: dict, vim_account_id: str): |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 67 | # TODO(diazb) Add support for vrops and aws |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 68 | vim_type = self.database_manager.get_vim_type(vim_account_id) |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 69 | if vim_type in VIM_COLLECTORS: |
| 70 | collector = VIM_COLLECTORS[vim_type](vim_account_id) |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 71 | metrics = collector.collect(vnfr) |
| 72 | for metric in metrics: |
| 73 | self.queue.put(metric) |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 74 | else: |
| 75 | log.debug("vimtype %s is not supported.", vim_type) |
| 76 | |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 77 | def _collect_vca_metrics(self, vnfr: dict): |
| 78 | log.debug('_collect_vca_metrics') |
| 79 | log.debug('vnfr: %s', vnfr) |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 80 | vca_collector = VCACollector() |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 81 | metrics = vca_collector.collect(vnfr) |
| 82 | for metric in metrics: |
| 83 | self.queue.put(metric) |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 84 | |
| 85 | def collect_metrics(self): |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 86 | vnfrs = self.common_db.get_vnfrs() |
| 87 | processes = [] |
| Benjamin Diaz | 27784a8 | 2018-10-25 14:54:35 -0300 | [diff] [blame] | 88 | for vnfr in vnfrs: |
| 89 | nsr_id = vnfr['nsr-id-ref'] |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 90 | vnf_member_index = vnfr['member-vnf-index-ref'] |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 91 | vim_account_id = self.common_db.get_vim_account_id(nsr_id, vnf_member_index) |
| 92 | p = multiprocessing.Process(target=self._collect_vim_metrics, |
| 93 | args=(vnfr, vim_account_id)) |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 94 | processes.append(p) |
| 95 | p.start() |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 96 | p = multiprocessing.Process(target=self._collect_vca_metrics, |
| 97 | args=(vnfr,)) |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 98 | processes.append(p) |
| 99 | p.start() |
| 100 | for process in processes: |
| 101 | process.join() |
| 102 | metrics = [] |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame^] | 103 | while not self.queue.empty(): |
| 104 | metrics.append(self.queue.get()) |
| Benjamin Diaz | 0e34244 | 2018-11-09 17:52:08 -0300 | [diff] [blame] | 105 | for plugin in self.plugins: |
| 106 | plugin.handle(metrics) |