mirror of
https://github.com/KevinMidboe/mktxp-no-cli.git
synced 2025-12-08 20:38:48 +00:00
firewall/mktxp colllectors, fixes/optimizations
This commit is contained in:
@@ -11,47 +11,16 @@
|
||||
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
## GNU General Public License for more details.
|
||||
|
||||
from mktxp.collectors.base_collector import BaseCollector
|
||||
|
||||
import speedtest
|
||||
from datetime import datetime
|
||||
from multiprocessing import Pool
|
||||
from prometheus_client import Gauge
|
||||
from mktxp.cli.config.config import config_handler
|
||||
|
||||
result_list = [{'download': 0, 'upload': 0, 'ping': 0}]
|
||||
def get_result(bandwidth_dict):
|
||||
result_list.append(bandwidth_dict)
|
||||
|
||||
class MKTXPCollector:
|
||||
''' MKTXP collector
|
||||
'''
|
||||
def __init__(self):
|
||||
self.pool = Pool()
|
||||
self.last_call_timestamp = 0
|
||||
self.gauge_bandwidth = Gauge('mktxp_internet_bandwidth', 'Internet bandwidth in bits per second', ['direction'])
|
||||
self.gauge_latency = Gauge('mktxp_internet_latency', 'Internet bandwidth latency in milliseconds')
|
||||
|
||||
def collect(self):
|
||||
if result_list:
|
||||
bandwidth_dict = result_list.pop(0)
|
||||
self.gauge_bandwidth.labels('download').set(bandwidth_dict["download"])
|
||||
self.gauge_bandwidth.labels('upload').set(bandwidth_dict["upload"])
|
||||
self.gauge_latency.set(bandwidth_dict["ping"])
|
||||
|
||||
ts = datetime.now().timestamp()
|
||||
if (ts - self.last_call_timestamp) > config_handler._entry().bandwidth_test_interval:
|
||||
self.pool.apply_async(MKTXPCollector.bandwidth_worker, callback=get_result)
|
||||
self.last_call_timestamp = ts
|
||||
|
||||
def __del__(self):
|
||||
self.pool.close()
|
||||
self.pool.join()
|
||||
|
||||
class MKTXPCollector(BaseCollector):
|
||||
''' System Identity Metrics collector
|
||||
'''
|
||||
@staticmethod
|
||||
def bandwidth_worker():
|
||||
bandwidth_test = speedtest.Speedtest()
|
||||
bandwidth_test.get_best_server()
|
||||
bandwidth_test.download()
|
||||
bandwidth_test.upload()
|
||||
return bandwidth_test.results.dict()
|
||||
def collect(router_metric):
|
||||
mktxp_records = router_metric.mktxp_records()
|
||||
if mktxp_records:
|
||||
mktxp_duration_metric = BaseCollector.counter_collector('collection_time', 'Total time spent collecting metrics in milliseconds', mktxp_records, 'duration', ['name'])
|
||||
yield mktxp_duration_metric
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user