From 2e85f9325a797977eea9dfea0a925775ddd211d9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Feb 2021 12:49:00 +0100 Subject: Merging upstream version 1.29.0. Signed-off-by: Daniel Baumann --- collectors/python.d.plugin/isc_dhcpd/README.md | 32 ++++++++--- .../python.d.plugin/isc_dhcpd/isc_dhcpd.chart.py | 66 +++++++++++++++++++++- .../python.d.plugin/isc_dhcpd/isc_dhcpd.conf | 7 ++- 3 files changed, 93 insertions(+), 12 deletions(-) (limited to 'collectors/python.d.plugin/isc_dhcpd') diff --git a/collectors/python.d.plugin/isc_dhcpd/README.md b/collectors/python.d.plugin/isc_dhcpd/README.md index f90cd041e..5830bd63e 100644 --- a/collectors/python.d.plugin/isc_dhcpd/README.md +++ b/collectors/python.d.plugin/isc_dhcpd/README.md @@ -1,11 +1,18 @@ -# isc_dhcpd + -Module monitor leases database to show all active leases for given pools. +# ISC DHCP monitoring with Netdata -**Requirements:** +Monitors the leases database to show all active leases for given pools. + +## Requirements - dhcpd leases file MUST BE readable by Netdata - pools MUST BE in CIDR format +- `python-ipaddress` package is needed in Python2 It produces: @@ -21,17 +28,28 @@ It produces: - leases (number of active leases in pool) -## configuration +## Configuration + +Edit the `python.d/isc_dhcpd.conf` configuration file using `edit-config` from the Netdata [config +directory](/docs/configure/nodes.md), which is typically at `/etc/netdata`. + +```bash +cd /etc/netdata # Replace this path with your Netdata config directory, if different +sudo ./edit-config python.d/isc_dhcpd.conf +``` Sample: ```yaml local: - leases_path : '/var/lib/dhcp/dhcpd.leases' - pools : '192.168.3.0/24 192.168.4.0/24 192.168.5.0/24' + leases_path: '/var/lib/dhcp/dhcpd.leases' + pools: + office: '192.168.2.0/24' # name(dimension): pool in CIDR format + wifi: '192.168.3.10-192.168.3.20' # name(dimension): pool in IP Range format + 192.168.4.0/24: '192.168.4.0/24' # name(dimension): pool in CIDR format + wifi-guest: '192.168.5.0/24 192.168.6.10-192.168.6.20' # name(dimension): pool in CIDR + IP Range format ``` -In case of python2 you need to install `py2-ipaddress` to make plugin work. The module will not work If no configuration is given. --- diff --git a/collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.chart.py b/collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.chart.py index a29439251..099c7d4e9 100644 --- a/collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.chart.py +++ b/collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.chart.py @@ -7,9 +7,9 @@ import os import re import time - try: import ipaddress + HAVE_IP_ADDRESS = True except ImportError: HAVE_IP_ADDRESS = False @@ -19,7 +19,6 @@ from copy import deepcopy from bases.FrameworkServices.SimpleService import SimpleService - ORDER = [ 'pools_utilization', 'pools_active_leases', @@ -46,6 +45,19 @@ CHARTS = { } } +POOL_CIDR = "CIDR" +POOL_IP_RANGE = "IP_RANGE" +POOL_UNKNOWN = "UNKNOWN" + +def detect_ip_type(ip): + ip_type = ip.split("-") + if len(ip_type) == 1: + return POOL_CIDR + elif len(ip_type) == 2: + return POOL_IP_RANGE + else: + return POOL_UNKNOWN + class DhcpdLeasesFile: def __init__(self, path): @@ -87,6 +99,32 @@ class Pool: def __init__(self, name, network): self.id = re.sub(r'[:/.-]+', '_', name) self.name = name + + self.networks = list() + for network in network.split(" "): + if not network: + continue + + ip_type = detect_ip_type(ip=network) + if ip_type == POOL_CIDR: + self.networks.append(PoolCIDR(network=network)) + elif ip_type == POOL_IP_RANGE: + self.networks.append(PoolIPRange(ip_range=network)) + else: + raise ValueError('Network ({0}) incorrect syntax, expect CIDR or IPRange format.'.format(network)) + + def num_hosts(self): + return sum([network.num_hosts() for network in self.networks]) + + def __contains__(self, item): + for network in self.networks: + if item in network: + return True + return False + + +class PoolCIDR: + def __init__(self, network): self.network = ipaddress.ip_network(address=u'%s' % network) def num_hosts(self): @@ -96,6 +134,30 @@ class Pool: return item.address in self.network +class PoolIPRange: + def __init__(self, ip_range): + ip_range = ip_range.split("-") + self.networks = list(self._summarize_address_range(ip_range[0], ip_range[1])) + + @staticmethod + def ip_address(ip): + return ipaddress.ip_address(u'%s' % ip) + + def _summarize_address_range(self, first, last): + address_first = self.ip_address(first) + address_last = self.ip_address(last) + return ipaddress.summarize_address_range(address_first, address_last) + + def num_hosts(self): + return sum([network.num_addresses for network in self.networks]) + + def __contains__(self, item): + for network in self.networks: + if item.address in network: + return True + return False + + class Lease: def __init__(self, address, ends, state): self.address = ipaddress.ip_address(address=u'%s' % address) diff --git a/collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.conf b/collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.conf index 8dcb5082f..c700947b4 100644 --- a/collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.conf +++ b/collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.conf @@ -63,9 +63,10 @@ # # leases_path: 'PATH' # the path to dhcpd.leases file # pools: -# office: '192.168.2.0/24' # name(dimension): pool in CIDR format -# wifi: '192.168.3.0/24' # name(dimension): pool in CIDR format -# 192.168.4.0/24: '192.168.4.0/24' # name(dimension): pool in CIDR format +# office: '192.168.2.0/24' # name(dimension): pool in CIDR format +# wifi: '192.168.3.10-192.168.3.20' # name(dimension): pool in IP Range format +# 192.168.4.0/24: '192.168.4.0/24' # name(dimension): pool in CIDR format +# wifi-guest: '192.168.5.0/24 192.168.6.10-192.168.6.20' # name(dimension): pool in CIDR + IP Range format # #----------------------------------------------------------------------- # IMPORTANT notes -- cgit v1.2.3