diff options
Diffstat (limited to 'roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2')
-rw-r--r-- | roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2 | 70 |
1 files changed, 60 insertions, 10 deletions
diff --git a/roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2 b/roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2 index 7b81328..4d3773b 100644 --- a/roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2 +++ b/roles/install_monitoring/templates/ffmyk-influx/dhcp.php.j2 @@ -1,23 +1,73 @@ <?php +date_default_timezone_set('UTC'); require('func.php'); - $data = shell_exec("grep -e 'lease [[:digit:]\.]\+ {' -e '\s\+ends' /var/lib/dhcp/dhcpd.leases"); +$dhcp_config = file_get_contents('/etc/dhcpd.conf'); - preg_match_all('/lease ([\d\.]+) \{\n\s+ends \d+ (\d{4}\/\d{2}\/\d{2} \d+:\d{2}:\d{2});/s', $data, $match); +$num_ranges = preg_match_all('/range[\s]+([\d]+\.[\d]+\.[\d]+\.[\d]+)[\s]+([\d]+\.[\d]+\.[\d]+\.[\d]+)/', $dhcp_config, $ranges); - unset($data, $match[0]); +$lease_file_handle = fopen("/var/lib/dhcp/dhcpd.leases", "r"); - $dend = time()-120; +$activeleases = array(); - $clients = 0; +$lease = -1; +$start = -1; +$end = -1; - foreach($match[2] as $timeout) { - $end = strtotime($timeout.' UTC'); - if($end > $dend) $clients++; +while(($line = fgets($lease_file_handle)) !== false) +{ + if(preg_match('/lease ([\d]+\.[\d]+\.[\d]+\.[\d]+)/', $line, $match)) + { + $lease = ip2long($match[1]); + continue; + } + elseif(preg_match('/starts \d ([\d]{4})\/([\d]{2})\/([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})/', $line, $match)) + { + $start = mktime($match[4], $match[5], $match[6], $match[2], $match[3], $match[1]); + continue; + } + elseif(preg_match('/ends \d ([\d]{4})\/([\d]{2})\/([\d]{2}) ([\d]{2}):([\d]{2}):([\d]{2})/', $line, $match)) + { + $end = mktime($match[4], $match[5], $match[6], $match[2], $match[3], $match[1]); + if($lease > 0 && $start > 0 && $end > 0) + { + if( $start < time() && $end > time() ) + { + $activeleases[$lease] = $lease; + $lease = -1; + $start = -1; + $end = -1; + } } + } +} - $data = 'clients,host={{ ansible_hostname }},type=backend value='.$clients; - sendflux($data); +$pools = array(); +for($range = 0; $range < $num_ranges; $range++) +{ + $clients = 0; + + $range_start = ip2long($ranges[1][$range]); + $range_end = ip2long($ranges[2][$range]); + foreach($activeleases as $lease) + { + if( $lease > $range_start && $lease < $range_end ) + { + $clients++; + } + } + + $pools[$range_start] = $clients; +} + +$data = ""; + +foreach($pools as $range => $clients) +{ + $data .= 'clients,host={{ ansible_hostname }},pool='.long2ip($range).',type=backend value='.$clients."\n"; +} + +sendflux($data); ?> |