diff options
author | Javier Pastor <vsc55@users.noreply.github.com> | 2020-08-17 10:32:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-17 11:32:44 +0300 |
commit | 5541187fcf745eb381c9cc90691b1265a37563d0 (patch) | |
tree | eb0279d9c156c30bcd847ff55ad70b9035ba4841 /collectors/python.d.plugin/isc_dhcpd | |
parent | 43c4d1edaaefd38c5597d6a83a17cdc219ff395e (diff) |
python.d/isc_dhcpd: add support for ip ranges (#9755)
Diffstat (limited to 'collectors/python.d.plugin/isc_dhcpd')
-rw-r--r-- | collectors/python.d.plugin/isc_dhcpd/README.md | 10 | ||||
-rw-r--r-- | collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.chart.py | 63 | ||||
-rw-r--r-- | collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.conf | 7 |
3 files changed, 74 insertions, 6 deletions
diff --git a/collectors/python.d.plugin/isc_dhcpd/README.md b/collectors/python.d.plugin/isc_dhcpd/README.md index a001173ff5..9195ad2e1e 100644 --- a/collectors/python.d.plugin/isc_dhcpd/README.md +++ b/collectors/python.d.plugin/isc_dhcpd/README.md @@ -12,6 +12,7 @@ Monitors the leases database to show all active leases for given pools. - dhcpd leases file MUST BE readable by Netdata - pools MUST BE in CIDR format +- `python-ipaddress` package is needed in Python2 It produces: @@ -41,11 +42,14 @@ 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 c9e10af2e3..099c7d4e93 100644 --- a/collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.chart.py +++ b/collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.chart.py @@ -45,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): @@ -86,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): @@ -95,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 8dcb5082f2..c700947b4c 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 |