updated influx-scripts
parent
4bfc6f1e29
commit
da72f062bb
@ -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();
|
||||
|
||||
$lease = -1;
|
||||
$start = -1;
|
||||
$end = -1;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$pools = array();
|
||||
|
||||
for($range = 0; $range < $num_ranges; $range++)
|
||||
{
|
||||
$clients = 0;
|
||||
|
||||
foreach($match[2] as $timeout) {
|
||||
$end = strtotime($timeout.' UTC');
|
||||
if($end > $dend) $clients++;
|
||||
$range_start = ip2long($ranges[1][$range]);
|
||||
$range_end = ip2long($ranges[2][$range]);
|
||||
foreach($activeleases as $lease)
|
||||
{
|
||||
if( $lease > $range_start && $lease < $range_end )
|
||||
{
|
||||
$clients++;
|
||||
}
|
||||
}
|
||||
|
||||
$data = 'clients,host={{ ansible_hostname }},type=backend value='.$clients;
|
||||
sendflux($data);
|
||||
$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);
|
||||
?>
|
||||
|
Loading…
Reference in New Issue