summaryrefslogtreecommitdiffstats
path: root/collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.chart.py
diff options
context:
space:
mode:
Diffstat (limited to 'collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.chart.py')
-rw-r--r--collectors/python.d.plugin/isc_dhcpd/isc_dhcpd.chart.py66
1 files changed, 64 insertions, 2 deletions
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)