blob: cc844364e91d749d2b2901fa3d3c0c0bbdb78b51 [file] [log] [blame]
Benjamin Diaz4da14662018-10-17 14:44:36 -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##
Benjamin Diaz4da14662018-10-17 14:44:36 -030023import logging
Benjamin Diaz0e342442018-11-09 17:52:08 -030024import time
Benjamin Diaz4da14662018-10-17 14:44:36 -030025
Benjamin Diaz83038622019-01-28 19:03:39 -030026import peewee
27
Benjamin Diaz0e342442018-11-09 17:52:08 -030028from osm_mon.collector.backends.prometheus import PrometheusBackend
Benjamin Diaza97bdb32019-04-10 15:22:22 -030029from osm_mon.collector.service import CollectorService
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030030from osm_mon.core.config import Config
Benjamin Diaz4da14662018-10-17 14:44:36 -030031
32log = logging.getLogger(__name__)
33
Benjamin Diaz51f44862018-11-15 10:27:12 -030034METRIC_BACKENDS = [
35 PrometheusBackend
36]
Benjamin Diaz4da14662018-10-17 14:44:36 -030037
Benjamin Diaz0e342442018-11-09 17:52:08 -030038
39class Collector:
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030040 def __init__(self, config: Config):
41 self.conf = config
Benjamin Diaza97bdb32019-04-10 15:22:22 -030042 self.service = CollectorService(config)
43 self.backends = []
Benjamin Diaz83038622019-01-28 19:03:39 -030044 self._init_backends()
Benjamin Diaz0e342442018-11-09 17:52:08 -030045
46 def collect_forever(self):
47 log.debug('collect_forever')
Benjamin Diaz0e342442018-11-09 17:52:08 -030048 while True:
49 try:
50 self.collect_metrics()
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030051 time.sleep(int(self.conf.get('collector', 'interval')))
Benjamin Diaz83038622019-01-28 19:03:39 -030052 except peewee.PeeweeException:
53 log.exception("Database error consuming message: ")
54 raise
Benjamin Diaz0e342442018-11-09 17:52:08 -030055 except Exception:
56 log.exception("Error collecting metrics")
57
Benjamin Diaz0e342442018-11-09 17:52:08 -030058 def collect_metrics(self):
Benjamin Diaza97bdb32019-04-10 15:22:22 -030059 metrics = self.service.collect_metrics()
60 for backend in self.backends:
61 backend.handle(metrics)
Benjamin Diaz83038622019-01-28 19:03:39 -030062
63 def _init_backends(self):
64 for backend in METRIC_BACKENDS:
Benjamin Diaza97bdb32019-04-10 15:22:22 -030065 self.backends.append(backend())