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
29 from osm_mon
.collector
.backends
.prometheus
import PrometheusBackend
30 from osm_mon
.collector
.collectors
.juju
import VCACollector
31 from osm_mon
.collector
.collectors
.openstack
import OpenstackCollector
32 from osm_mon
.collector
.collectors
.vmware
import VMwareCollector
33 from osm_mon
.core
.common_db
import CommonDbClient
34 from osm_mon
.core
.database
import DatabaseManager
35 from osm_mon
.core
.settings
import Config
37 log
= logging
.getLogger(__name__
)
40 "openstack": OpenstackCollector
,
41 "vmware": VMwareCollector
50 self
.common_db
= CommonDbClient()
52 self
.database_manager
= DatabaseManager()
53 self
.database_manager
.create_tables()
54 self
.queue
= multiprocessing
.Queue()
57 def collect_forever(self
):
58 log
.debug('collect_forever')
59 cfg
= Config
.instance()
62 self
.collect_metrics()
63 time
.sleep(cfg
.OSMMON_COLLECTOR_INTERVAL
)
64 except peewee
.PeeweeException
:
65 log
.exception("Database error consuming message: ")
68 log
.exception("Error collecting metrics")
70 def _collect_vim_metrics(self
, vnfr
: dict, vim_account_id
: str):
71 # TODO(diazb) Add support for vrops and aws
72 vim_type
= self
.database_manager
.get_vim_type(vim_account_id
)
73 if vim_type
in VIM_COLLECTORS
:
74 collector
= VIM_COLLECTORS
[vim_type
](vim_account_id
)
75 metrics
= collector
.collect(vnfr
)
76 for metric
in metrics
:
77 self
.queue
.put(metric
)
79 log
.debug("vimtype %s is not supported.", vim_type
)
81 def _collect_vca_metrics(self
, vnfr
: dict):
82 log
.debug('_collect_vca_metrics')
83 log
.debug('vnfr: %s', vnfr
)
84 vca_collector
= VCACollector()
85 metrics
= vca_collector
.collect(vnfr
)
86 for metric
in metrics
:
87 self
.queue
.put(metric
)
89 def collect_metrics(self
):
90 vnfrs
= self
.common_db
.get_vnfrs()
93 nsr_id
= vnfr
['nsr-id-ref']
94 vnf_member_index
= vnfr
['member-vnf-index-ref']
95 vim_account_id
= self
.common_db
.get_vim_account_id(nsr_id
, vnf_member_index
)
96 p
= multiprocessing
.Process(target
=self
._collect
_vim
_metrics
,
97 args
=(vnfr
, vim_account_id
))
100 p
= multiprocessing
.Process(target
=self
._collect
_vca
_metrics
,
104 for process
in processes
:
107 while not self
.queue
.empty():
108 metrics
.append(self
.queue
.get())
109 for plugin
in self
.plugins
:
110 plugin
.handle(metrics
)
112 def _init_backends(self
):
113 for backend
in METRIC_BACKENDS
:
114 self
.plugins
.append(backend())