Adds collection of VM status metric in OpenStack infra plugin
[osm/MON.git] / osm_mon / collector / collector.py
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 ##
23 import logging
24 import multiprocessing
25 import time
26
27 import peewee
28
29 from osm_mon.collector.backends.prometheus import PrometheusBackend
30 from osm_mon.collector.infra_collectors.openstack import OpenstackInfraCollector
31 from osm_mon.collector.vnf_collectors.juju import VCACollector
32 from osm_mon.collector.vnf_collectors.openstack import OpenstackCollector
33 from osm_mon.collector.vnf_collectors.vmware import VMwareCollector
34 from osm_mon.core.common_db import CommonDbClient
35 from osm_mon.core.config import Config
36 from osm_mon.core.database import DatabaseManager
37
38 log = logging.getLogger(__name__)
39
40 VIM_COLLECTORS = {
41 "openstack": OpenstackCollector,
42 "vmware": VMwareCollector
43 }
44 VIM_INFRA_COLLECTORS = {
45 "openstack": OpenstackInfraCollector
46 }
47 METRIC_BACKENDS = [
48 PrometheusBackend
49 ]
50
51
52 class Collector:
53 def __init__(self, config: Config):
54 self.conf = config
55 self.common_db = CommonDbClient(self.conf)
56 self.plugins = []
57 self.database_manager = DatabaseManager(self.conf)
58 self.database_manager.create_tables()
59 self.queue = multiprocessing.Queue()
60 self._init_backends()
61
62 def collect_forever(self):
63 log.debug('collect_forever')
64 while True:
65 try:
66 self.collect_metrics()
67 time.sleep(int(self.conf.get('collector', 'interval')))
68 except peewee.PeeweeException:
69 log.exception("Database error consuming message: ")
70 raise
71 except Exception:
72 log.exception("Error collecting metrics")
73
74 def _collect_vim_metrics(self, vnfr: dict, vim_account_id: str):
75 # TODO(diazb) Add support for vrops and aws
76 database_manager = DatabaseManager(self.conf)
77 vim_type = database_manager.get_vim_type(vim_account_id)
78 if vim_type in VIM_COLLECTORS:
79 collector = VIM_COLLECTORS[vim_type](self.conf, vim_account_id)
80 metrics = collector.collect(vnfr)
81 for metric in metrics:
82 self.queue.put(metric)
83 else:
84 log.debug("vimtype %s is not supported.", vim_type)
85
86 def _collect_vim_infra_metrics(self, vim_account_id: str):
87 database_manager = DatabaseManager(self.conf)
88 vim_type = database_manager.get_vim_type(vim_account_id)
89 if vim_type in VIM_INFRA_COLLECTORS:
90 collector = VIM_INFRA_COLLECTORS[vim_type](self.conf, vim_account_id)
91 metrics = collector.collect()
92 for metric in metrics:
93 self.queue.put(metric)
94 else:
95 log.debug("vimtype %s is not supported.", vim_type)
96
97 def _collect_vca_metrics(self, vnfr: dict):
98 log.debug('_collect_vca_metrics')
99 log.debug('vnfr: %s', vnfr)
100 vca_collector = VCACollector(self.conf)
101 metrics = vca_collector.collect(vnfr)
102 for metric in metrics:
103 self.queue.put(metric)
104
105 def collect_metrics(self):
106 vnfrs = self.common_db.get_vnfrs()
107 processes = []
108 for vnfr in vnfrs:
109 nsr_id = vnfr['nsr-id-ref']
110 vnf_member_index = vnfr['member-vnf-index-ref']
111 vim_account_id = self.common_db.get_vim_account_id(nsr_id, vnf_member_index)
112 p = multiprocessing.Process(target=self._collect_vim_metrics,
113 args=(vnfr, vim_account_id))
114 processes.append(p)
115 p.start()
116 p = multiprocessing.Process(target=self._collect_vca_metrics,
117 args=(vnfr,))
118 processes.append(p)
119 p.start()
120 vims = self.common_db.get_vim_accounts()
121 for vim in vims:
122 p = multiprocessing.Process(target=self._collect_vim_infra_metrics,
123 args=(vim['_id'],))
124 processes.append(p)
125 p.start()
126 for process in processes:
127 process.join(timeout=10)
128 metrics = []
129 while not self.queue.empty():
130 metrics.append(self.queue.get())
131 for plugin in self.plugins:
132 plugin.handle(metrics)
133
134 def _init_backends(self):
135 for backend in METRIC_BACKENDS:
136 self.plugins.append(backend())