diff --git a/mktxp/cli/config/config.py b/mktxp/cli/config/config.py index b4ad559..87d2e7d 100755 --- a/mktxp/cli/config/config.py +++ b/mktxp/cli/config/config.py @@ -39,6 +39,7 @@ class MKTXPConfigKeys: FE_DHCP_KEY = 'dhcp' FE_DHCP_LEASE_KEY = 'dhcp_lease' FE_DHCP_POOL_KEY = 'pool' + FE_IP_CONNECTIONS_KEY = 'connections' FE_INTERFACE_KEY = 'interface' FE_FIREWALL_KEY = 'firewall' FE_MONITOR_KEY = 'monitor' @@ -81,7 +82,7 @@ class MKTXPConfigKeys: BOOLEAN_KEYS_NO = {ENABLED_KEY, SSL_KEY, NO_SSL_CERTIFICATE, SSL_CERTIFICATE_VERIFY} # 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, + BOOLEAN_KEYS_YES = {FE_DHCP_KEY, FE_DHCP_LEASE_KEY, FE_DHCP_POOL_KEY, FE_IP_CONNECTIONS_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_NETWATCH_KEY} @@ -100,7 +101,7 @@ class ConfigEntry: MKTXPConfigKeys.SSL_KEY, MKTXPConfigKeys.NO_SSL_CERTIFICATE, MKTXPConfigKeys.SSL_CERTIFICATE_VERIFY, 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_MONITOR_KEY, MKTXPConfigKeys.FE_ROUTE_KEY, MKTXPConfigKeys.FE_WIRELESS_KEY, MKTXPConfigKeys.FE_WIRELESS_CLIENTS_KEY, MKTXPConfigKeys.FE_IP_CONNECTIONS_KEY, 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, diff --git a/mktxp/cli/config/mktxp.conf b/mktxp/cli/config/mktxp.conf index ef4c779..ac26534 100644 --- a/mktxp/cli/config/mktxp.conf +++ b/mktxp/cli/config/mktxp.conf @@ -26,6 +26,7 @@ dhcp = True # DHCP general metrics dhcp_lease = True # DHCP lease metrics + connections = True # IP connections metrics pool = True # Pool metrics interface = True # Interfaces traffic metrics firewall = True # Firewall rules traffic metrics diff --git a/mktxp/collector/connection_collector.py b/mktxp/collector/connection_collector.py new file mode 100644 index 0000000..5b1646f --- /dev/null +++ b/mktxp/collector/connection_collector.py @@ -0,0 +1,31 @@ +# 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.datasource.connection_ds import IPConnectionDatasource + + +class IPConnectionCollector(BaseCollector): + ''' IP Connection Metrics collector + ''' + @staticmethod + def collect(router_entry): + if not router_entry.config_entry.connections: + return + + connection_records = IPConnectionDatasource.metric_records(router_entry, metric_labels = []) + if connection_records: + + connection_metrics = BaseCollector.gauge_collector('ip_connections_total', 'Number of IP connections', connection_records, 'count',) + yield connection_metrics diff --git a/mktxp/datasource/connection_ds.py b/mktxp/datasource/connection_ds.py new file mode 100644 index 0000000..3f8800e --- /dev/null +++ b/mktxp/datasource/connection_ds.py @@ -0,0 +1,33 @@ +# 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 IPConnectionDatasource: + ''' IP connections data provider + ''' + @staticmethod + def metric_records(router_entry, *, metric_labels = []): + try: + answer = router_entry.api_connection.router_api().get_binary_resource('/ip/firewall/connection/').call('print', {'count-only': b''}) + # answer looks and feels like an empty list: [], but it has a special attribute `done_message` + done_message = answer.done_message + # `done_msg` is a dict with the return code as a key - which is the count that we are looking for + cnt = done_message['ret'].decode() + records = [{'count': cnt}] + return BaseDSProcessor.trimmed_records(router_entry, router_records = records, metric_labels = metric_labels) + except Exception as exc: + print(f'Error getting IP connection 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 e5c9e7d..caf1029 100644 --- a/mktxp/flow/collector_registry.py +++ b/mktxp/flow/collector_registry.py @@ -13,8 +13,8 @@ from collections import OrderedDict -from mktxp.cli.config.config import MKTXPConfigKeys from mktxp.collector.dhcp_collector import DHCPCollector +from mktxp.collector.connection_collector import IPConnectionCollector from mktxp.collector.interface_collector import InterfaceCollector from mktxp.collector.health_collector import HealthCollector from mktxp.collector.identity_collector import IdentityCollector @@ -45,6 +45,7 @@ class CollectorRegistry: self.register('HealthCollector', HealthCollector.collect) self.register('DHCPCollector', DHCPCollector.collect) + self.register('IPConnectionCollector', IPConnectionCollector.collect) self.register('PoolCollector', PoolCollector.collect) self.register('InterfaceCollector', InterfaceCollector.collect) diff --git a/mktxp/flow/router_entry.py b/mktxp/flow/router_entry.py index e14a5c2..fd6d6c3 100644 --- a/mktxp/flow/router_entry.py +++ b/mktxp/flow/router_entry.py @@ -32,6 +32,7 @@ class RouterEntry: 'HealthCollector': 0, 'DHCPCollector': 0, 'PoolCollector': 0, + 'IPConnectionCollector': 0, 'InterfaceCollector': 0, 'FirewallCollector': 0, 'MonitorCollector': 0,