blob: 9dd16838cdf92c03ac44a7f539ae49bf01decfe1 [file] [log] [blame]
Benjamin Diaz416a7532019-07-29 12:00:38 -03001# -*- 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##
palsus34a0a0f2021-01-20 18:26:13 +000023
24# This version uses a ProcessThreadPoolExecutor to limit the number of processes launched
25
Benjamin Diaza97bdb32019-04-10 15:22:22 -030026import logging
27import multiprocessing
28from typing import List
palsus34a0a0f2021-01-20 18:26:13 +000029import concurrent.futures
30import time
Benjamin Diaza97bdb32019-04-10 15:22:22 -030031
32from osm_mon.collector.infra_collectors.onos import OnosInfraCollector
33from osm_mon.collector.infra_collectors.openstack import OpenstackInfraCollector
kasare82c0622019-05-09 00:55:30 -070034from osm_mon.collector.infra_collectors.vio import VIOInfraCollector
Benjamin Diaz416a7532019-07-29 12:00:38 -030035from osm_mon.collector.infra_collectors.vmware import VMwareInfraCollector
Benjamin Diaza97bdb32019-04-10 15:22:22 -030036from osm_mon.collector.metric import Metric
Benjamin Diaza97bdb32019-04-10 15:22:22 -030037from osm_mon.collector.vnf_collectors.juju import VCACollector
38from osm_mon.collector.vnf_collectors.openstack import OpenstackCollector
39from osm_mon.collector.vnf_collectors.vio import VIOCollector
40from osm_mon.collector.vnf_collectors.vmware import VMwareCollector
41from osm_mon.core.common_db import CommonDbClient
42from osm_mon.core.config import Config
43
44log = logging.getLogger(__name__)
45
46VIM_COLLECTORS = {
47 "openstack": OpenstackCollector,
48 "vmware": VMwareCollector,
49 "vio": VIOCollector
50}
51VIM_INFRA_COLLECTORS = {
kasarf840f692019-04-19 03:57:58 -070052 "openstack": OpenstackInfraCollector,
kasare82c0622019-05-09 00:55:30 -070053 "vmware": VMwareInfraCollector,
54 "vio": VIOInfraCollector
Benjamin Diaza97bdb32019-04-10 15:22:22 -030055}
56SDN_INFRA_COLLECTORS = {
garciadeblas2bd1a0e2021-01-18 22:57:32 +000057 "onosof": OnosInfraCollector,
58 "onos_vpls": OnosInfraCollector
Benjamin Diaza97bdb32019-04-10 15:22:22 -030059}
60
61
62class CollectorService:
palsus34a0a0f2021-01-20 18:26:13 +000063 # The processes getting metrics will store the results in this queue
64 queue = multiprocessing.Queue()
65
Benjamin Diaza97bdb32019-04-10 15:22:22 -030066 def __init__(self, config: Config):
67 self.conf = config
68 self.common_db = CommonDbClient(self.conf)
palsus34a0a0f2021-01-20 18:26:13 +000069 return
Benjamin Diaza97bdb32019-04-10 15:22:22 -030070
palsus34a0a0f2021-01-20 18:26:13 +000071 # static methods to be executed in the Processes
72 @staticmethod
73 def _get_vim_type(conf: Config, vim_account_id: str) -> str:
74 common_db = CommonDbClient(conf)
Benjamin Diaz4de60c52019-08-27 17:49:59 -030075 vim_account = common_db.get_vim_account(vim_account_id)
76 vim_type = vim_account['vim_type']
77 if 'config' in vim_account and 'vim_type' in vim_account['config']:
78 vim_type = vim_account['config']['vim_type'].lower()
garciadeblas94fae152020-05-07 14:16:22 +000079 if vim_type == 'vio' and 'vrops_site' not in vim_account['config']:
80 vim_type = 'openstack'
Benjamin Diaz4de60c52019-08-27 17:49:59 -030081 return vim_type
palsus34a0a0f2021-01-20 18:26:13 +000082
83 @staticmethod
84 def _collect_vim_metrics(conf: Config, vnfr: dict, vim_account_id: str):
85 # TODO(diazb) Add support for aws
86 vim_type = CollectorService._get_vim_type(conf, vim_account_id)
87 log.debug("vim type.....{}".format(vim_type))
88 if vim_type in VIM_COLLECTORS:
89 collector = VIM_COLLECTORS[vim_type](conf, vim_account_id)
90 metrics = collector.collect(vnfr)
91 log.debug("Collecting vim metrics.....{}".format(metrics))
92 for metric in metrics:
93 pass
94 CollectorService.queue.put(metric)
95 else:
96 log.debug("vimtype %s is not supported.", vim_type)
97 return
98
99 @staticmethod
100 def _collect_vca_metrics(conf: Config, vnfr: dict):
101 vca_collector = VCACollector(conf)
102 metrics = vca_collector.collect(vnfr)
103 log.debug("Collecting vca metrics.....{}".format(metrics))
104 for metric in metrics:
105 CollectorService.queue.put(metric)
106 return
107
108 @staticmethod
109 def _collect_vim_infra_metrics(conf: Config, vim_account_id: str):
110 log.info("Collecting vim infra metrics")
111 vim_type = CollectorService._get_vim_type(conf, vim_account_id)
112 if vim_type in VIM_INFRA_COLLECTORS:
113 collector = VIM_INFRA_COLLECTORS[vim_type](conf, vim_account_id)
114 metrics = collector.collect()
115 log.debug("Collecting vim infra metrics.....{}".format(metrics))
116 for metric in metrics:
117 CollectorService.queue.put(metric)
118 else:
119 log.debug("vimtype %s is not supported.", vim_type)
120 return
121
122 @staticmethod
123 def _collect_sdnc_infra_metrics(conf: Config, sdnc_id: str):
124 log.info("Collecting sdnc metrics")
125 common_db = CommonDbClient(conf)
126 sdn_type = common_db.get_sdnc(sdnc_id)['type']
127 if sdn_type in SDN_INFRA_COLLECTORS:
128 collector = SDN_INFRA_COLLECTORS[sdn_type](conf, sdnc_id)
129 metrics = collector.collect()
130 log.debug("Collecting sdnc metrics.....{}".format(metrics))
131 for metric in metrics:
132 CollectorService.queue.put(metric)
133 else:
134 log.debug("sdn_type %s is not supported.", sdn_type)
135 return
136
137 @staticmethod
138 def _stop_process_pool(executor):
139 log.info('Stopping all processes in the process pool')
140 try:
141 for pid, process in executor._processes.items():
142 if process.is_alive():
143 process.terminate()
144 except Exception as e:
145 log.info("Exception during process termination")
146 log.debug("Exception %s" % (e))
147 executor.shutdown()
148 return
149
150 def collect_metrics(self) -> List[Metric]:
151 vnfrs = self.common_db.get_vnfrs()
152 metrics = []
153
154 start_time = time.time()
155 # Starting executor pool with pool size process_pool_size. Default process_pool_size is 20
156 with concurrent.futures.ProcessPoolExecutor(self.conf.get('collector', 'process_pool_size')) as executor:
157 log.debug('Started metric collector process pool with pool size %s' % (self.conf.get('collector',
158 'process_pool_size')))
159 futures = []
160 for vnfr in vnfrs:
161 nsr_id = vnfr['nsr-id-ref']
162 vnf_member_index = vnfr['member-vnf-index-ref']
163 vim_account_id = self.common_db.get_vim_account_id(nsr_id, vnf_member_index)
164 futures.append(executor.submit(CollectorService._collect_vim_metrics, self.conf, vnfr, vim_account_id))
165 futures.append(executor.submit(CollectorService._collect_vca_metrics, self.conf, vnfr))
166
167 vims = self.common_db.get_vim_accounts()
168 for vim in vims:
169 futures.append(executor.submit(CollectorService._collect_vim_infra_metrics, self.conf, vim['_id']))
170
171 sdncs = self.common_db.get_sdncs()
172 for sdnc in sdncs:
173 futures.append(executor.submit(CollectorService._collect_sdnc_infra_metrics, self.conf, sdnc['_id']))
174
175 try:
176 # Wait for future calls to complete till process_execution_timeout. Default is 50 seconds
177 for future in concurrent.futures.as_completed(futures, self.conf.get('collector',
178 'process_execution_timeout')):
179 result = future.result(timeout=int(self.conf.get('collector',
180 'process_execution_timeout')))
181 log.debug('result = %s' % (result))
182 except concurrent.futures.TimeoutError as e:
183 # Some processes have not completed due to timeout error
184 log.info(' Some processes have not finished due to TimeoutError exception')
185 log.debug('concurrent.futures.TimeoutError exception %s' % (e))
186 CollectorService._stop_process_pool(executor)
187
188 while not self.queue.empty():
189 metrics.append(self.queue.get())
190
191 end_time = time.time()
192 log.info("Collection completed in %s seconds", end_time - start_time)
193
194 return metrics