diff --git a/mktxp/cli/config/config.py b/mktxp/cli/config/config.py index 0519363..3fc3d7f 100755 --- a/mktxp/cli/config/config.py +++ b/mktxp/cli/config/config.py @@ -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, diff --git a/mktxp/collector/netwatch_collector.py b/mktxp/collector/netwatch_collector.py new file mode 100644 index 0000000..1a072be --- /dev/null +++ b/mktxp/collector/netwatch_collector.py @@ -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']) + diff --git a/mktxp/datasource/netwatch_ds.py b/mktxp/datasource/netwatch_ds.py new file mode 100644 index 0000000..5aa168e --- /dev/null +++ b/mktxp/datasource/netwatch_ds.py @@ -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 + diff --git a/mktxp/flow/collector_registry.py b/mktxp/flow/collector_registry.py index c1b76d9..e5c9e7d 100644 --- a/mktxp/flow/collector_registry.py +++ b/mktxp/flow/collector_registry.py @@ -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) diff --git a/mktxp/flow/router_entry.py b/mktxp/flow/router_entry.py index e4a77d0..e14a5c2 100644 --- a/mktxp/flow/router_entry.py +++ b/mktxp/flow/router_entry.py @@ -36,6 +36,7 @@ class RouterEntry: 'FirewallCollector': 0, 'MonitorCollector': 0, 'POECollector': 0, + 'NetwatchCollector': 0, 'RouteCollector': 0, 'WLANCollector': 0, 'CapsmanCollector': 0, diff --git a/setup.py b/setup.py index a148443..f567b82 100755 --- a/setup.py +++ b/setup.py @@ -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',