Make this code less memory hungry and fix route command generation

This commit is contained in:
Ermal LUÇI 2015-01-08 23:57:47 +01:00
parent ac8f75f1e0
commit d97dd42437

View File

@ -5,10 +5,12 @@ if(!file_exists($leases_file)) {
exit(1);
}
$fd = fopen($leases_file, 'r');
$duid_arr = array();
foreach(file($leases_file) as $line) {
while (( $line = fgets($fd, 4096)) !== false) {
// echo "$line";
if(preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i ", $line, $duidmatch)) {
if(preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i", $line, $duidmatch)) {
$type = $duidmatch[1];
$duid = $duidmatch[2];
continue;
@ -31,7 +33,7 @@ foreach(file($leases_file) as $line) {
}
/* closing bracket */
if(preg_match("/^}/i ", $line)) {
if(preg_match("/^}/i", $line)) {
switch($type) {
case "ia-na":
$duid_arr[$duid][$type] = $ia_na;
@ -48,13 +50,13 @@ foreach(file($leases_file) as $line) {
continue;
}
}
fclose($fd);
$routes = array();
foreach ($duid_arr as $entry) {
if($entry['ia-pd'] <> "") {
if(!empty($entry['ia-pd'])) {
$routes[$entry['ia-na']] = $entry['ia-pd'];
}
array_shift($duid_arr);
}
// echo "add routes\n";
@ -66,28 +68,24 @@ if(count($routes) > 0) {
/* get clog from dhcpd */
$dhcpdlogfile = "/var/log/dhcpd.log";
$clog = array();
if(file_exists($dhcpdlogfile))
exec("clog $dhcpdlogfile", $clog, $ret);
if($ret > 0)
$clog = array();
$expires = array();
foreach($clog as $line) {
if(preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
if(in_array($expire[1], $routes))
continue;
$expires[$expire[1]] = $expire[1];
if(file_exists($dhcpdlogfile)) {
$fd = popen("clog $dhcpdlogfile", 'r');
while (($line = fgets($fd)) !== false) {
//echo $line;
if(preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
if(in_array($expire[1], $routes))
continue;
$expires[$expire[1]] = $expire[1];
}
}
array_shift($clog);
pclose($fd);
}
// echo "remove routes\n";
if(count($expires) > 0) {
foreach ($expires as $prefix) {
echo "/sbin/route delete -inet6 {$prefix['prefix']}\n";
array_shift($expires);
}
}