Skip to content

Commit

Permalink
dual ip stack, pool
Browse files Browse the repository at this point in the history
  • Loading branch information
akpw committed May 3, 2024
1 parent e2443d9 commit 2516a0e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ The default configuration file comes with a sample configuration, making it easy
[default]
# this affects configuration of all routers, unless overloaded on their specific levels
enabled = True # turns metrics collection for this RouterOS device on / off
hostname = localhost # RouterOS IP address
port = 8728 # RouterOS IP Port
Expand All @@ -83,14 +83,14 @@ The default configuration file comes with a sample configuration, making it easy
interface = True # Interfaces traffic metrics
route = True # IPv4 Routes metrics
pool = True # Pool metrics
pool = True # IPv4 Pool metrics
firewall = True # IPv4 Firewall rules traffic metrics
neighbor = True # Reachable IPv4 Neighbors
neighbor = True # IPv4 Reachable Neighbors
ipv6_route = False # IPv6 Routes metrics
ipv6_pool = False # IPv6 Pool metrics
ipv6_firewall = False # IPv6 Firewall rules traffic metrics
ipv6_neighbor = False # Reachable IPv6 Neighbors
ipv6_neighbor = False # IPv6 Reachable Neighbors
poe = True # POE metrics
monitor = True # Interface monitor metrics
Expand Down
6 changes: 3 additions & 3 deletions mktxp/cli/config/mktxp.conf
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
interface = True # Interfaces traffic metrics

route = True # IPv4 Routes metrics
pool = True # Pool metrics
pool = True # IPv4 Pool metrics
firewall = True # IPv4 Firewall rules traffic metrics
neighbor = True # Reachable IPv4 Neighbors
neighbor = True # IPv4 Reachable Neighbors

ipv6_route = False # IPv6 Routes metrics
ipv6_pool = False # IPv6 Pool metrics
ipv6_firewall = False # IPv6 Firewall rules traffic metrics
ipv6_neighbor = False # Reachable IPv6 Neighbors
ipv6_neighbor = False # IPv6 Reachable Neighbors

poe = True # POE metrics
monitor = True # Interface monitor metrics
Expand Down
20 changes: 15 additions & 5 deletions mktxp/collector/pool_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,27 @@ class PoolCollector(BaseCollector):
'''
@staticmethod
def collect(router_entry):
if not router_entry.config_entry.pool:
return
# ~*~*~*~*~*~ IPv4 ~*~*~*~*~*~
if router_entry.config_entry.pool:
yield from PoolCollector._process_ip_stack(router_entry)

# ~*~*~*~*~*~ IPv6 ~*~*~*~*~*~
if router_entry.config_entry.ipv6_pool:
yield from PoolCollector._process_ip_stack(router_entry, ipv6=True)

# helpers
@staticmethod
def _process_ip_stack(router_entry, ipv6=False):
ip_stack = 'ipv6' if ipv6 else 'ipv4'

# initialize all pool counts, including those currently not used
pool_records = PoolMetricsDataSource.metric_records(router_entry, metric_labels = ['name'])
pool_records = PoolMetricsDataSource.metric_records(router_entry, metric_labels = ['name'], ipv6=ipv6)
if pool_records:
pool_used_labels = ['pool']
pool_used_counts = {pool_record['name']: 0 for pool_record in pool_records}

# for pools in usage, calculate the current numbers
pool_used_records = PoolUsedMetricsDataSource.metric_records(router_entry, metric_labels = pool_used_labels)
pool_used_records = PoolUsedMetricsDataSource.metric_records(router_entry, metric_labels = pool_used_labels, ipv6=ipv6)
for pool_used_record in pool_used_records:
pool_used_counts[pool_used_record['pool']] = pool_used_counts.get(pool_used_record['pool'], 0) + 1

Expand All @@ -42,5 +52,5 @@ def collect(router_entry):
'pool': key, 'count': value} for key, value in pool_used_counts.items()]

# yield used-per-pool metrics
used_per_pool_metrics = BaseCollector.gauge_collector('ip_pool_used', 'Number of used addresses per IP pool', used_per_pool_records, 'count', ['pool'])
used_per_pool_metrics = BaseCollector.gauge_collector(f'ip_pool_used{"_ipv6" if ipv6 else ""}', f'Number of used addresses per IP pool ({ip_stack.upper()})', used_per_pool_records, 'count', ['pool'])
yield used_per_pool_metrics
14 changes: 8 additions & 6 deletions mktxp/datasource/pool_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ class PoolMetricsDataSource:
''' Pool Metrics data provider
'''
@staticmethod
def metric_records(router_entry, *, metric_labels = None):
def metric_records(router_entry, *, metric_labels = None, ipv6 = False):
ip_stack = 'ipv6' if ipv6 else 'ip'
if metric_labels is None:
metric_labels = []
try:
pool_records = router_entry.api_connection.router_api().get_resource('/ip/pool').get()
pool_records = router_entry.api_connection.router_api().get_resource(f'/{ip_stack}/pool').get()
return BaseDSProcessor.trimmed_records(router_entry, router_records = pool_records, metric_labels = metric_labels)
except Exception as exc:
print(f'Error getting pool info from router {router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}')
print(f'Error getting {"IPv6" if ipv6 else "IPv4"} pool info from router {router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}')
return None


class PoolUsedMetricsDataSource:
''' Pool/Used Metrics data provider
'''
@staticmethod
def metric_records(router_entry, *, metric_labels = None):
def metric_records(router_entry, *, metric_labels = None, ipv6 = False):
ip_stack = 'ipv6' if ipv6 else 'ip'
if metric_labels is None:
metric_labels = []
try:
pool_used_records = router_entry.api_connection.router_api().get_resource('/ip/pool/used').get()
pool_used_records = router_entry.api_connection.router_api().get_resource(f'/{ip_stack}/pool/used').get()
return BaseDSProcessor.trimmed_records(router_entry, router_records = pool_used_records, metric_labels = metric_labels)
except Exception as exc:
print(f'Error getting pool used info from router {router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}')
print(f'Error getting {"IPv6" if ipv6 else "IPv4"} pool used info from router {router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}')
return None

0 comments on commit 2516a0e

Please sign in to comment.