blob: 005f844e3ffdbb428dc10c06c28cea89db816534 [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##
palsus9a773322021-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
Benjamin Diaza97bdb32019-04-10 15:22:22 -030027from typing import List
palsus9a773322021-01-20 18:26:13 +000028import concurrent.futures
29import time
Atul Agarwal345e73d2021-10-08 05:18:27 +000030import keystoneauth1.exceptions
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,
garciadeblas8e4179f2021-05-14 16:47:03 +020049 "vio": VIOCollector,
Benjamin Diaza97bdb32019-04-10 15:22:22 -030050}
51VIM_INFRA_COLLECTORS = {
kasarf840f692019-04-19 03:57:58 -070052 "openstack": OpenstackInfraCollector,
kasare82c0622019-05-09 00:55:30 -070053 "vmware": VMwareInfraCollector,
garciadeblas8e4179f2021-05-14 16:47:03 +020054 "vio": VIOInfraCollector,
Benjamin Diaza97bdb32019-04-10 15:22:22 -030055}
garciadeblas8e4179f2021-05-14 16:47:03 +020056SDN_INFRA_COLLECTORS = {"onosof": OnosInfraCollector, "onos_vpls": OnosInfraCollector}
Benjamin Diaza97bdb32019-04-10 15:22:22 -030057
58
59class CollectorService:
60 def __init__(self, config: Config):
61 self.conf = config
62 self.common_db = CommonDbClient(self.conf)
palsus9a773322021-01-20 18:26:13 +000063 return
Benjamin Diaza97bdb32019-04-10 15:22:22 -030064
palsus9a773322021-01-20 18:26:13 +000065 # static methods to be executed in the Processes
66 @staticmethod
67 def _get_vim_type(conf: Config, vim_account_id: str) -> str:
68 common_db = CommonDbClient(conf)
Benjamin Diaz4de60c52019-08-27 17:49:59 -030069 vim_account = common_db.get_vim_account(vim_account_id)
garciadeblas8e4179f2021-05-14 16:47:03 +020070 vim_type = vim_account["vim_type"]
71 if "config" in vim_account and "vim_type" in vim_account["config"]:
72 vim_type = vim_account["config"]["vim_type"].lower()
73 if vim_type == "vio" and "vrops_site" not in vim_account["config"]:
74 vim_type = "openstack"
Benjamin Diaz4de60c52019-08-27 17:49:59 -030075 return vim_type
palsus9a773322021-01-20 18:26:13 +000076
77 @staticmethod
78 def _collect_vim_metrics(conf: Config, vnfr: dict, vim_account_id: str):
79 # TODO(diazb) Add support for aws
palsuse57f2f12021-03-01 19:59:41 +000080 metrics = []
palsus9a773322021-01-20 18:26:13 +000081 vim_type = CollectorService._get_vim_type(conf, vim_account_id)
82 log.debug("vim type.....{}".format(vim_type))
83 if vim_type in VIM_COLLECTORS:
84 collector = VIM_COLLECTORS[vim_type](conf, vim_account_id)
85 metrics = collector.collect(vnfr)
86 log.debug("Collecting vim metrics.....{}".format(metrics))
palsus9a773322021-01-20 18:26:13 +000087 else:
88 log.debug("vimtype %s is not supported.", vim_type)
palsuse57f2f12021-03-01 19:59:41 +000089 return metrics
palsus9a773322021-01-20 18:26:13 +000090
91 @staticmethod
92 def _collect_vca_metrics(conf: Config, vnfr: dict):
palsuse57f2f12021-03-01 19:59:41 +000093 metrics = []
palsus9a773322021-01-20 18:26:13 +000094 vca_collector = VCACollector(conf)
95 metrics = vca_collector.collect(vnfr)
96 log.debug("Collecting vca metrics.....{}".format(metrics))
palsuse57f2f12021-03-01 19:59:41 +000097 return metrics
palsus9a773322021-01-20 18:26:13 +000098
99 @staticmethod
100 def _collect_vim_infra_metrics(conf: Config, vim_account_id: str):
101 log.info("Collecting vim infra metrics")
palsuse57f2f12021-03-01 19:59:41 +0000102 metrics = []
palsus9a773322021-01-20 18:26:13 +0000103 vim_type = CollectorService._get_vim_type(conf, vim_account_id)
104 if vim_type in VIM_INFRA_COLLECTORS:
105 collector = VIM_INFRA_COLLECTORS[vim_type](conf, vim_account_id)
106 metrics = collector.collect()
107 log.debug("Collecting vim infra metrics.....{}".format(metrics))
palsus9a773322021-01-20 18:26:13 +0000108 else:
109 log.debug("vimtype %s is not supported.", vim_type)
palsuse57f2f12021-03-01 19:59:41 +0000110 return metrics
palsus9a773322021-01-20 18:26:13 +0000111
112 @staticmethod
113 def _collect_sdnc_infra_metrics(conf: Config, sdnc_id: str):
114 log.info("Collecting sdnc metrics")
palsuse57f2f12021-03-01 19:59:41 +0000115 metrics = []
palsus9a773322021-01-20 18:26:13 +0000116 common_db = CommonDbClient(conf)
garciadeblas8e4179f2021-05-14 16:47:03 +0200117 sdn_type = common_db.get_sdnc(sdnc_id)["type"]
palsus9a773322021-01-20 18:26:13 +0000118 if sdn_type in SDN_INFRA_COLLECTORS:
119 collector = SDN_INFRA_COLLECTORS[sdn_type](conf, sdnc_id)
120 metrics = collector.collect()
121 log.debug("Collecting sdnc metrics.....{}".format(metrics))
palsus9a773322021-01-20 18:26:13 +0000122 else:
123 log.debug("sdn_type %s is not supported.", sdn_type)
palsuse57f2f12021-03-01 19:59:41 +0000124 return metrics
palsus9a773322021-01-20 18:26:13 +0000125
126 @staticmethod
127 def _stop_process_pool(executor):
garciadeblas8e4179f2021-05-14 16:47:03 +0200128 log.info("Shutting down process pool")
palsus9a773322021-01-20 18:26:13 +0000129 try:
garciadeblas8e4179f2021-05-14 16:47:03 +0200130 log.debug("Stopping residual processes in the process pool")
palsus9a773322021-01-20 18:26:13 +0000131 for pid, process in executor._processes.items():
132 if process.is_alive():
133 process.terminate()
134 except Exception as e:
135 log.info("Exception during process termination")
136 log.debug("Exception %s" % (e))
palsuse57f2f12021-03-01 19:59:41 +0000137
138 try:
139 # Shutting down executor
garciadeblas8e4179f2021-05-14 16:47:03 +0200140 log.debug("Shutting down process pool executor")
palsuse57f2f12021-03-01 19:59:41 +0000141 executor.shutdown()
142 except RuntimeError as e:
garciadeblas8e4179f2021-05-14 16:47:03 +0200143 log.info("RuntimeError in shutting down executer")
144 log.debug("RuntimeError %s" % (e))
palsus9a773322021-01-20 18:26:13 +0000145 return
146
147 def collect_metrics(self) -> List[Metric]:
148 vnfrs = self.common_db.get_vnfrs()
149 metrics = []
150
151 start_time = time.time()
152 # Starting executor pool with pool size process_pool_size. Default process_pool_size is 20
garciadeblas8e4179f2021-05-14 16:47:03 +0200153 with concurrent.futures.ProcessPoolExecutor(
154 self.conf.get("collector", "process_pool_size")
155 ) as executor:
156 log.info(
157 "Started metric collector process pool with pool size %s"
158 % (self.conf.get("collector", "process_pool_size"))
159 )
palsus9a773322021-01-20 18:26:13 +0000160 futures = []
161 for vnfr in vnfrs:
garciadeblas8e4179f2021-05-14 16:47:03 +0200162 nsr_id = vnfr["nsr-id-ref"]
163 vnf_member_index = vnfr["member-vnf-index-ref"]
164 vim_account_id = self.common_db.get_vim_account_id(
165 nsr_id, vnf_member_index
166 )
167 futures.append(
168 executor.submit(
169 CollectorService._collect_vim_metrics,
170 self.conf,
171 vnfr,
172 vim_account_id,
173 )
174 )
175 futures.append(
176 executor.submit(
177 CollectorService._collect_vca_metrics, self.conf, vnfr
178 )
179 )
palsus9a773322021-01-20 18:26:13 +0000180
181 vims = self.common_db.get_vim_accounts()
182 for vim in vims:
garciadeblas8e4179f2021-05-14 16:47:03 +0200183 futures.append(
184 executor.submit(
185 CollectorService._collect_vim_infra_metrics,
186 self.conf,
187 vim["_id"],
188 )
189 )
palsus9a773322021-01-20 18:26:13 +0000190
191 sdncs = self.common_db.get_sdncs()
192 for sdnc in sdncs:
garciadeblas8e4179f2021-05-14 16:47:03 +0200193 futures.append(
194 executor.submit(
195 CollectorService._collect_sdnc_infra_metrics,
196 self.conf,
197 sdnc["_id"],
198 )
199 )
palsus9a773322021-01-20 18:26:13 +0000200
201 try:
202 # Wait for future calls to complete till process_execution_timeout. Default is 50 seconds
garciadeblas8e4179f2021-05-14 16:47:03 +0200203 for future in concurrent.futures.as_completed(
204 futures, self.conf.get("collector", "process_execution_timeout")
205 ):
Atul Agarwal345e73d2021-10-08 05:18:27 +0000206 try:
207 result = future.result(
208 timeout=int(
209 self.conf.get("collector", "process_execution_timeout")
210 )
garciadeblas8e4179f2021-05-14 16:47:03 +0200211 )
Atul Agarwal345e73d2021-10-08 05:18:27 +0000212 metrics.extend(result)
213 log.debug("result = %s" % (result))
214 except keystoneauth1.exceptions.connection.ConnectTimeout as e:
215 log.info("Keystone connection timeout during metric collection")
216 log.debug("Keystone connection timeout exception %s" % (e))
palsus9a773322021-01-20 18:26:13 +0000217 except concurrent.futures.TimeoutError as e:
218 # Some processes have not completed due to timeout error
garciadeblas8e4179f2021-05-14 16:47:03 +0200219 log.info(
220 "Some processes have not finished due to TimeoutError exception"
221 )
222 log.debug("concurrent.futures.TimeoutError exception %s" % (e))
palsus9a773322021-01-20 18:26:13 +0000223
palsuse57f2f12021-03-01 19:59:41 +0000224 # Shutting down process pool executor
225 CollectorService._stop_process_pool(executor)
palsus9a773322021-01-20 18:26:13 +0000226
227 end_time = time.time()
228 log.info("Collection completed in %s seconds", end_time - start_time)
229
230 return metrics