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
28 from prometheus_client
import start_http_server
29 from prometheus_client
.core
import REGISTRY
31 from osm_mon
.collector
.collector
import MonCollector
32 from osm_mon
.core
.settings
import Config
34 log
= logging
.getLogger(__name__
)
37 class MonPrometheusExporter
:
40 self
.custom_collector
= CustomCollector()
42 def _run_exporter(self
):
43 log
.debug('_run_exporter')
44 REGISTRY
.register(self
.custom_collector
)
45 log
.info("Starting MON Prometheus exporter at port %s", 8000)
46 start_http_server(8000)
50 collector_thread
= threading
.Thread(target
=self
._run
_collector
)
51 collector_thread
.setDaemon(True)
52 collector_thread
.start()
53 exporter_thread
= threading
.Thread(target
=self
._run
_exporter
)
54 exporter_thread
.setDaemon(True)
55 exporter_thread
.start()
56 collector_thread
.join()
57 exporter_thread
.join()
59 def _run_collector(self
):
60 log
.debug('_run_collector')
61 asyncio
.set_event_loop(asyncio
.new_event_loop())
62 mon_collector
= MonCollector()
63 cfg
= Config
.instance()
66 log
.debug('_run_collector_loop')
67 metrics
= asyncio
.get_event_loop().run_until_complete(mon_collector
.collect_metrics())
68 self
.custom_collector
.metrics
= metrics
69 time
.sleep(cfg
.OSMMON_COLLECTOR_INTERVAL
)
71 log
.exception("Error collecting metrics")
74 class CustomCollector(object):
77 self
.mon_collector
= MonCollector()
89 if __name__
== '__main__':
90 MonPrometheusExporter().run()