Netwatch collector

This commit is contained in:
Arseniy Kuznetsov
2021-04-18 13:09:41 +02:00
parent 8c64fdf6bb
commit 11ab63d59e
6 changed files with 86 additions and 3 deletions

View File

@@ -48,6 +48,7 @@ class MKTXPConfigKeys:
FE_CAPSMAN_KEY = 'capsman'
FE_CAPSMAN_CLIENTS_KEY = 'capsman_clients'
FE_POE_KEY = 'poe'
FE_NETWATCH_KEY = 'netwatch'
MKTXP_SOCKET_TIMEOUT = 'socket_timeout'
MKTXP_SOCKET_TIMEOUT = 'socket_timeout'
@@ -82,7 +83,7 @@ class MKTXPConfigKeys:
# Feature keys enabled by default
BOOLEAN_KEYS_YES = {FE_DHCP_KEY, FE_DHCP_LEASE_KEY, FE_DHCP_POOL_KEY, FE_INTERFACE_KEY, FE_FIREWALL_KEY,
FE_MONITOR_KEY, FE_ROUTE_KEY, MKTXP_USE_COMMENTS_OVER_NAMES,
FE_WIRELESS_KEY, FE_WIRELESS_CLIENTS_KEY, FE_CAPSMAN_KEY, FE_CAPSMAN_CLIENTS_KEY, FE_POE_KEY}
FE_WIRELESS_KEY, FE_WIRELESS_CLIENTS_KEY, FE_CAPSMAN_KEY, FE_CAPSMAN_CLIENTS_KEY, FE_POE_KEY, FE_NETWATCH_KEY}
SYSTEM_BOOLEAN_KEYS_YES = (MKTXP_BANDWIDTH_KEY,)
@@ -100,7 +101,7 @@ class ConfigEntry:
MKTXPConfigKeys.FE_DHCP_KEY, MKTXPConfigKeys.FE_DHCP_LEASE_KEY, MKTXPConfigKeys.FE_DHCP_POOL_KEY, MKTXPConfigKeys.FE_INTERFACE_KEY, MKTXPConfigKeys.FE_FIREWALL_KEY,
MKTXPConfigKeys.FE_MONITOR_KEY, MKTXPConfigKeys.FE_ROUTE_KEY, MKTXPConfigKeys.FE_WIRELESS_KEY, MKTXPConfigKeys.FE_WIRELESS_CLIENTS_KEY,
MKTXPConfigKeys.FE_CAPSMAN_KEY, MKTXPConfigKeys.FE_CAPSMAN_CLIENTS_KEY, MKTXPConfigKeys.FE_POE_KEY, MKTXPConfigKeys.MKTXP_USE_COMMENTS_OVER_NAMES
MKTXPConfigKeys.FE_CAPSMAN_KEY, MKTXPConfigKeys.FE_CAPSMAN_CLIENTS_KEY, MKTXPConfigKeys.FE_POE_KEY, MKTXPConfigKeys.FE_NETWATCH_KEY, MKTXPConfigKeys.MKTXP_USE_COMMENTS_OVER_NAMES
])
MKTXPSystemEntry = namedtuple('MKTXPSystemEntry', [MKTXPConfigKeys.PORT_KEY, MKTXPConfigKeys.MKTXP_SOCKET_TIMEOUT,
MKTXPConfigKeys.MKTXP_INITIAL_DELAY, MKTXPConfigKeys.MKTXP_MAX_DELAY,

View File

@@ -0,0 +1,35 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
from mktxp.collector.base_collector import BaseCollector
from mktxp.flow.processor.output import BaseOutputProcessor
from mktxp.datasource.netwatch_ds import NetwatchMetricsDataSource
class NetwatchCollector(BaseCollector):
''' Netwatch Metrics collector
'''
@staticmethod
def collect(router_entry):
if not router_entry.config_entry.netwatch:
return
netwatch_labels = ['host', 'timeout', 'interval', 'since', 'status', 'comment', 'name']
netwatch_records = NetwatchMetricsDataSource.metric_records(router_entry, metric_labels = netwatch_labels)
if netwatch_records:
yield BaseCollector.info_collector('netwatch', 'Netwatch Info Metrics', netwatch_records, netwatch_labels)
yield BaseCollector.gauge_collector('netwatch', 'Netwatch Status Metrics', netwatch_records, 'status', ['name'])

View File

@@ -0,0 +1,44 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
from mktxp.datasource.base_ds import BaseDSProcessor
class NetwatchMetricsDataSource:
''' Netwatch Metrics data provider
'''
@staticmethod
def metric_records(router_entry, *, metric_labels = []):
try:
netwatch_records = router_entry.api_connection.router_api().get_resource('/tool/netwatch').get(disabled='false')
if 'name' in metric_labels:
for netwatch_record in netwatch_records:
comment = netwatch_record.get('comment')
host = netwatch_record.get('host')
if comment:
netwatch_record['name'] = f'{host} ({comment[0:20]})' if not router_entry.config_entry.use_comments_over_names else comment
else:
netwatch_record['name'] = host
# translation rules
translation_table = {}
if 'status' in metric_labels:
translation_table['status'] = lambda value: '1' if value == 'up' else '0'
return BaseDSProcessor.trimmed_records(router_entry, router_records = netwatch_records, translation_table = translation_table, metric_labels = metric_labels)
except Exception as exc:
print(f'Error getting Netwatch info from router{router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}')
return None

View File

@@ -20,6 +20,7 @@ from mktxp.collector.health_collector import HealthCollector
from mktxp.collector.identity_collector import IdentityCollector
from mktxp.collector.monitor_collector import MonitorCollector
from mktxp.collector.poe_collector import POECollector
from mktxp.collector.netwatch_collector import NetwatchCollector
from mktxp.collector.pool_collector import PoolCollector
from mktxp.collector.resource_collector import SystemResourceCollector
from mktxp.collector.route_collector import RouteCollector
@@ -50,6 +51,7 @@ class CollectorRegistry:
self.register('FirewallCollector', FirewallCollector.collect)
self.register('MonitorCollector', MonitorCollector.collect)
self.register('POECollector', POECollector.collect)
self.register('NetwatchCollector', NetwatchCollector.collect)
self.register('RouteCollector', RouteCollector.collect)
self.register('WLANCollector', WLANCollector.collect)

View File

@@ -36,6 +36,7 @@ class RouterEntry:
'FirewallCollector': 0,
'MonitorCollector': 0,
'POECollector': 0,
'NetwatchCollector': 0,
'RouteCollector': 0,
'WLANCollector': 0,
'CapsmanCollector': 0,

View File

@@ -20,7 +20,7 @@ with open(path.join(pkg_dir, 'README.md'), encoding='utf-8') as f:
setup(
name='mktxp',
version='0.29',
version='0.30',
url='https://github.com/akpw/mktxp',