1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
24 import multiprocessing
25 from typing
import List
27 from osm_mon
.collector
.infra_collectors
.onos
import OnosInfraCollector
28 from osm_mon
.collector
.infra_collectors
.openstack
import OpenstackInfraCollector
29 from osm_mon
.collector
.infra_collectors
.vio
import VIOInfraCollector
30 from osm_mon
.collector
.infra_collectors
.vmware
import VMwareInfraCollector
31 from osm_mon
.collector
.metric
import Metric
32 from osm_mon
.collector
.vnf_collectors
.juju
import VCACollector
33 from osm_mon
.collector
.vnf_collectors
.openstack
import OpenstackCollector
34 from osm_mon
.collector
.vnf_collectors
.vio
import VIOCollector
35 from osm_mon
.collector
.vnf_collectors
.vmware
import VMwareCollector
36 from osm_mon
.core
.common_db
import CommonDbClient
37 from osm_mon
.core
.config
import Config
39 log
= logging
.getLogger(__name__
)
42 "openstack": OpenstackCollector
,
43 "vmware": VMwareCollector
,
46 VIM_INFRA_COLLECTORS
= {
47 "openstack": OpenstackInfraCollector
,
48 "vmware": VMwareInfraCollector
,
49 "vio": VIOInfraCollector
51 SDN_INFRA_COLLECTORS
= {
52 "onos": OnosInfraCollector
56 class CollectorService
:
57 def __init__(self
, config
: Config
):
59 self
.common_db
= CommonDbClient(self
.conf
)
60 self
.queue
= multiprocessing
.Queue()
62 def _collect_vim_metrics(self
, vnfr
: dict, vim_account_id
: str):
63 # TODO(diazb) Add support for aws
64 common_db
= CommonDbClient(self
.conf
)
65 vim_type
= common_db
.get_vim_account(vim_account_id
)['vim_type']
66 if vim_type
in VIM_COLLECTORS
:
67 collector
= VIM_COLLECTORS
[vim_type
](self
.conf
, vim_account_id
)
68 metrics
= collector
.collect(vnfr
)
69 for metric
in metrics
:
70 self
.queue
.put(metric
)
72 log
.debug("vimtype %s is not supported.", vim_type
)
74 def _collect_vim_infra_metrics(self
, vim_account_id
: str):
75 common_db
= CommonDbClient(self
.conf
)
76 vim_type
= common_db
.get_vim_account(vim_account_id
)['vim_type']
77 if vim_type
in VIM_INFRA_COLLECTORS
:
78 collector
= VIM_INFRA_COLLECTORS
[vim_type
](self
.conf
, vim_account_id
)
79 metrics
= collector
.collect()
80 for metric
in metrics
:
81 self
.queue
.put(metric
)
83 log
.debug("vimtype %s is not supported.", vim_type
)
85 def _collect_sdnc_infra_metrics(self
, sdnc_id
: str):
86 common_db
= CommonDbClient(self
.conf
)
87 sdn_type
= common_db
.get_sdnc(sdnc_id
)['type']
88 if sdn_type
in SDN_INFRA_COLLECTORS
:
89 collector
= SDN_INFRA_COLLECTORS
[sdn_type
](self
.conf
, sdnc_id
)
90 metrics
= collector
.collect()
91 for metric
in metrics
:
92 self
.queue
.put(metric
)
94 log
.debug("sdn_type %s is not supported.", sdn_type
)
96 def _collect_vca_metrics(self
, vnfr
: dict):
97 log
.debug('_collect_vca_metrics')
98 log
.debug('vnfr: %s', vnfr
)
99 vca_collector
= VCACollector(self
.conf
)
100 metrics
= vca_collector
.collect(vnfr
)
101 for metric
in metrics
:
102 self
.queue
.put(metric
)
104 def collect_metrics(self
) -> List
[Metric
]:
105 vnfrs
= self
.common_db
.get_vnfrs()
108 nsr_id
= vnfr
['nsr-id-ref']
109 vnf_member_index
= vnfr
['member-vnf-index-ref']
110 vim_account_id
= self
.common_db
.get_vim_account_id(nsr_id
, vnf_member_index
)
111 p
= multiprocessing
.Process(target
=self
._collect
_vim
_metrics
,
112 args
=(vnfr
, vim_account_id
))
115 p
= multiprocessing
.Process(target
=self
._collect
_vca
_metrics
,
119 vims
= self
.common_db
.get_vim_accounts()
121 p
= multiprocessing
.Process(target
=self
._collect
_vim
_infra
_metrics
,
125 sdncs
= self
.common_db
.get_sdncs()
127 p
= multiprocessing
.Process(target
=self
._collect
_sdnc
_infra
_metrics
,
131 for process
in processes
:
132 process
.join(timeout
=10)
134 while not self
.queue
.empty():
135 metrics
.append(self
.queue
.get())