Commit 21377312 authored by Franco Fichtner's avatar Franco Fichtner

sbin: apply PSR2 style

parent b4845e62
...@@ -4,9 +4,9 @@ require_once("pfsense-utils.inc"); ...@@ -4,9 +4,9 @@ require_once("pfsense-utils.inc");
ini_set("max_execution_time", "0"); ini_set("max_execution_time", "0");
if(empty($argv[1])) { if (empty($argv[1])) {
echo "No modem device given \n"; echo "No modem device given \n";
exit(0); exit(0);
} }
/* Huawei example */ /* Huawei example */
...@@ -19,9 +19,9 @@ $i = 0; ...@@ -19,9 +19,9 @@ $i = 0;
$record = array(); $record = array();
$handle = fopen($device, "r"); $handle = fopen($device, "r");
if(! $handle) { if (! $handle) {
echo "Can not open modem stats device\n"; echo "Can not open modem stats device\n";
exit(1); exit(1);
} }
$record['time'] = 0; $record['time'] = 0;
...@@ -36,49 +36,49 @@ $record['bwdownstream'] = 0; ...@@ -36,49 +36,49 @@ $record['bwdownstream'] = 0;
$record['simstate'] = 0; $record['simstate'] = 0;
$record['service'] = 0; $record['service'] = 0;
while(true) { while (true) {
$string = ""; $string = "";
$string = fgets($handle, 256); $string = fgets($handle, 256);
$elements = array(); $elements = array();
$elements = split(":", $string); $elements = split(":", $string);
$elements[0] = trim($elements[0]); $elements[0] = trim($elements[0]);
$elements[1] = trim($elements[1]); $elements[1] = trim($elements[1]);
switch($elements[0]) { switch ($elements[0]) {
case "^MODE": case "^MODE":
$record['mode'] = $elements[1]; $record['mode'] = $elements[1];
break; break;
case "^SRVST": case "^SRVST":
$record['service'] = $elements[1]; $record['service'] = $elements[1];
break; break;
case "^SIMST": case "^SIMST":
$record['simstate'] = $elements[1]; $record['simstate'] = $elements[1];
break; break;
case "^RSSI": case "^RSSI":
$record['rssi'] = $elements[1]; $record['rssi'] = $elements[1];
break; break;
case "^DSFLOWRPT": case "^DSFLOWRPT":
$items = array(); $items = array();
$items = split(",", $elements[1]); $items = split(",", $elements[1]);
$record['time'] = hexdec($items[0]); $record['time'] = hexdec($items[0]);
$record['upstream'] = round((floatval(hexdec($items[1])) * 8) /1024); $record['upstream'] = round((floatval(hexdec($items[1])) * 8) /1024);
$record['downstream'] = round((floatval(hexdec($items[2])) * 8) /1024); $record['downstream'] = round((floatval(hexdec($items[2])) * 8) /1024);
$record['sent'] = hexdec($items[3]); $record['sent'] = hexdec($items[3]);
$record['received'] = hexdec($items[4]); $record['received'] = hexdec($items[4]);
$record['bwupstream'] = round((floatval(hexdec($items[5])) * 8) /1024); $record['bwupstream'] = round((floatval(hexdec($items[5])) * 8) /1024);
$record['bwdownstream'] = round((floatval(hexdec($items[6])) * 8) /1024); $record['bwdownstream'] = round((floatval(hexdec($items[6])) * 8) /1024);
break; break;
} }
if($i > 10) { if ($i > 10) {
$csv = $header; $csv = $header;
$csv .= implode(",", $record); $csv .= implode(",", $record);
$csv .= "\n"; $csv .= "\n";
file_put_contents($statfile, $csv); file_put_contents($statfile, $csv);
$i = 0; $i = 0;
} }
$i++; $i++;
} }
fclose($handle); fclose($handle);
?> ?>
...@@ -52,60 +52,63 @@ $current_user_count = 0; ...@@ -52,60 +52,63 @@ $current_user_count = 0;
/* tmp file to use to store old data (per interface)*/ /* tmp file to use to store old data (per interface)*/
$tmpfile = '/var/db/captiveportal_online_users'; $tmpfile = '/var/db/captiveportal_online_users';
if(empty($type)) if (empty($type)) {
exit; exit;
}
/* echo the rrd required syntax */ /* echo the rrd required syntax */
echo "N:"; echo "N:";
$result = "NaN"; $result = "NaN";
if ($type == "loggedin") { if ($type == "loggedin") {
$timestamp = 0; $timestamp = 0;
/* Find out the previous user timestamp /* Find out the previous user timestamp
* so we can determine the difference between the current * so we can determine the difference between the current
* and previous user count. If the file is empty return a 0. * and previous user count. If the file is empty return a 0.
*/ */
$fd = @fopen($tmpfile, "r"); $fd = @fopen($tmpfile, "r");
if ($fd) { if ($fd) {
while (!feof($fd)) { while (!feof($fd)) {
$line = trim(fgets($fd)); $line = trim(fgets($fd));
if($line) if ($line) {
$previous_user_timestamp = $line; $previous_user_timestamp = $line;
else } else {
$previous_user_timestamp = 0; $previous_user_timestamp = 0;
} }
} else { }
$previous_user_timestamp = 0; } else {
} $previous_user_timestamp = 0;
@fclose($fd); }
@fclose($fd);
foreach($cpdb as $user) {
$user_ip = $user[2]; foreach ($cpdb as $user) {
// Record the timestamp $user_ip = $user[2];
$timestamp = $user[0]; // Record the timestamp
if ($timestamp > $previous_user_timestamp) $timestamp = $user[0];
$current_user_count = $current_user_count + 1; if ($timestamp > $previous_user_timestamp) {
} $current_user_count = $current_user_count + 1;
}
// Write out the latest timestamp but not if it is empty }
if (!empty($timestamp)) {
$fd = @fopen($tmpfile, "w"); // Write out the latest timestamp but not if it is empty
if ($fd) { if (!empty($timestamp)) {
fwrite($fd, $timestamp); $fd = @fopen($tmpfile, "w");
} if ($fd) {
@fclose($fd); fwrite($fd, $timestamp);
} }
@fclose($fd);
/* If $timestamp is less than or equal to previous_user_timestamp return 0, }
/* If $timestamp is less than or equal to previous_user_timestamp return 0,
* as we only want the 'X' number of users logged in since last RRD poll. * as we only want the 'X' number of users logged in since last RRD poll.
*/ */
if($timestamp <= $previous_user_timestamp) if ($timestamp <= $previous_user_timestamp) {
$result = 0; $result = 0;
else { } else {
$result = $current_user_count; $result = $current_user_count;
} }
} elseif ($type == "concurrent") } elseif ($type == "concurrent")
$result = $no_users; $result = $no_users;
echo "$result"; echo "$result";
...@@ -39,44 +39,49 @@ $notices = array(); ...@@ -39,44 +39,49 @@ $notices = array();
// Check for gmirror.status // Check for gmirror.status
if (file_exists($status_file)) { if (file_exists($status_file)) {
// If it exists, read status in // If it exists, read status in
$previous_mirror_status = unserialize(file_get_contents($status_file)); $previous_mirror_status = unserialize(file_get_contents($status_file));
$previous_mirror_list = array_keys($previous_mirror_status); $previous_mirror_list = array_keys($previous_mirror_status);
sort($previous_mirror_list); sort($previous_mirror_list);
if (count($previous_mirror_status) > 0) { if (count($previous_mirror_status) > 0) {
// Check list of current mirrors vs old mirrors, notify if one has appeared/disappeared // Check list of current mirrors vs old mirrors, notify if one has appeared/disappeared
if ($mirror_list != $previous_mirror_list) if ($mirror_list != $previous_mirror_list) {
$notices[] = sprintf(gettext("List of mirrors changed. Old: (%s) New: (%s)"), implode(", ", $previous_mirror_list), implode(", ", $mirror_list)); $notices[] = sprintf(gettext("List of mirrors changed. Old: (%s) New: (%s)"), implode(", ", $previous_mirror_list), implode(", ", $mirror_list));
}
// For each mirror, check the mirror status, notify if changed // For each mirror, check the mirror status, notify if changed
foreach ($mirror_list as $mirror) { foreach ($mirror_list as $mirror) {
if (is_array($previous_mirror_status[$mirror])) { if (is_array($previous_mirror_status[$mirror])) {
// Notify if the status changed // Notify if the status changed
if ($mirror_status[$mirror]['status'] != $previous_mirror_status[$mirror]['status']) { if ($mirror_status[$mirror]['status'] != $previous_mirror_status[$mirror]['status']) {
$notices[] = sprintf(gettext("Mirror %s status changed from %s to %s."), $mirror, $previous_mirror_status[$mirror]['status'], $mirror_status[$mirror]['status']); $notices[] = sprintf(gettext("Mirror %s status changed from %s to %s."), $mirror, $previous_mirror_status[$mirror]['status'], $mirror_status[$mirror]['status']);
} }
// Notify if the drive count changed // Notify if the drive count changed
if (count($mirror_status[$mirror]['components']) != count($previous_mirror_status[$mirror]['components'])) { if (count($mirror_status[$mirror]['components']) != count($previous_mirror_status[$mirror]['components'])) {
// Notify if the consumer count changed. // Notify if the consumer count changed.
$notices[] = sprintf(gettext("Mirror %s consumer count changed from %d to %d."), $mirror, count($previous_mirror_status[$mirror]['components']), count($mirror_status[$mirror]['components'])); $notices[] = sprintf(gettext("Mirror %s consumer count changed from %d to %d."), $mirror, count($previous_mirror_status[$mirror]['components']), count($mirror_status[$mirror]['components']));
} }
if (strtoupper($mirror_status[$mirror]['status']) == "DEGRADED") { if (strtoupper($mirror_status[$mirror]['status']) == "DEGRADED") {
// Check the drive status as it may be different. // Check the drive status as it may be different.
asort($mirror_status[$mirror]['components']); asort($mirror_status[$mirror]['components']);
asort($previous_mirror_status[$mirror]['components']); asort($previous_mirror_status[$mirror]['components']);
if ($mirror_status[$mirror]['components'] != $previous_mirror_status[$mirror]['components']) if ($mirror_status[$mirror]['components'] != $previous_mirror_status[$mirror]['components']) {
$notices[] = sprintf(gettext("Mirror %s drive status changed. Old: (%s) New: (%s)"), $notices[] = sprintf(
$mirror, gettext("Mirror %s drive status changed. Old: (%s) New: (%s)"),
implode(", ", $previous_mirror_status[$mirror]['components']), $mirror,
implode(", ", $mirror_status[$mirror]['components'])); implode(", ", $previous_mirror_status[$mirror]['components']),
} implode(", ", $mirror_status[$mirror]['components'])
} );
} }
} }
}
}
}
} }
if (count($notices)) { if (count($notices)) {
file_notice("gmirror", implode("\n ", $notices), "GEOM Mirror Status Change", 1); file_notice("gmirror", implode("\n ", $notices), "GEOM Mirror Status Change", 1);
} }
// Write out current status if changed // Write out current status if changed
if ($mirror_status != $previous_mirror_status) if ($mirror_status != $previous_mirror_status) {
file_put_contents($status_file, serialize($mirror_status)); file_put_contents($status_file, serialize($mirror_status));
}
<?php <?php
$leases_file = "/var/dhcpd/var/db/dhcpd6.leases"; $leases_file = "/var/dhcpd/var/db/dhcpd6.leases";
if(!file_exists($leases_file)) { if (!file_exists($leases_file)) {
exit(1); exit(1);
} }
$duid_arr = array(); $duid_arr = array();
foreach(file($leases_file) as $line) { foreach (file($leases_file) as $line) {
// echo "$line"; // echo "$line";
if(preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i ", $line, $duidmatch)) { if (preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i ", $line, $duidmatch)) {
$type = $duidmatch[1]; $type = $duidmatch[1];
$duid = $duidmatch[2]; $duid = $duidmatch[2];
continue; continue;
} }
/* is it active? otherwise just discard */ /* is it active? otherwise just discard */
if(preg_match("/binding state active/i", $line, $activematch)) { if (preg_match("/binding state active/i", $line, $activematch)) {
$active = true; $active = true;
continue; continue;
} }
if(preg_match("/iaaddr[ ]+([0-9a-f:]+)[ ]+/i", $line, $addressmatch)) { if (preg_match("/iaaddr[ ]+([0-9a-f:]+)[ ]+/i", $line, $addressmatch)) {
$ia_na = $addressmatch[1]; $ia_na = $addressmatch[1];
continue; continue;
} }
if(preg_match("/iaprefix[ ]+([0-9a-f:\/]+)[ ]+/i", $line, $prefixmatch)) { if (preg_match("/iaprefix[ ]+([0-9a-f:\/]+)[ ]+/i", $line, $prefixmatch)) {
$ia_pd = $prefixmatch[1]; $ia_pd = $prefixmatch[1];
continue; continue;
} }
/* closing bracket */ /* closing bracket */
if(preg_match("/^}/i ", $line)) { if (preg_match("/^}/i ", $line)) {
switch($type) { switch ($type) {
case "ia-na": case "ia-na":
$duid_arr[$duid][$type] = $ia_na; $duid_arr[$duid][$type] = $ia_na;
break; break;
case "ia-pd": case "ia-pd":
$duid_arr[$duid][$type] = $ia_pd; $duid_arr[$duid][$type] = $ia_pd;
break; break;
} }
unset($type); unset($type);
unset($duid); unset($duid);
unset($active); unset($active);
unset($ia_na); unset($ia_na);
unset($ia_pd); unset($ia_pd);
continue; continue;
} }
} }
$routes = array(); $routes = array();
foreach ($duid_arr as $entry) { foreach ($duid_arr as $entry) {
if($entry['ia-pd'] <> "") { if ($entry['ia-pd'] <> "") {
$routes[$entry['ia-na']] = $entry['ia-pd']; $routes[$entry['ia-na']] = $entry['ia-pd'];
} }
array_shift($duid_arr); array_shift($duid_arr);
} }
// echo "add routes\n"; // echo "add routes\n";
if(count($routes) > 0) { if (count($routes) > 0) {
foreach ($routes as $address => $prefix) { foreach ($routes as $address => $prefix) {
echo "/sbin/route change -inet6 {$prefix} {$address}\n"; echo "/sbin/route change -inet6 {$prefix} {$address}\n";
} }
} }
/* get clog from dhcpd */ /* get clog from dhcpd */
$dhcpdlogfile = "/var/log/dhcpd.log"; $dhcpdlogfile = "/var/log/dhcpd.log";
$clog = array(); $clog = array();
if(file_exists($dhcpdlogfile)) if (file_exists($dhcpdlogfile)) {
exec("clog $dhcpdlogfile", $clog, $ret); exec("clog $dhcpdlogfile", $clog, $ret);
}
if($ret > 0) if ($ret > 0) {
$clog = array(); $clog = array();
}
$expires = array(); $expires = array();
foreach($clog as $line) { foreach ($clog as $line) {
if(preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) { if (preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
if(in_array($expire[1], $routes)) if (in_array($expire[1], $routes)) {
continue; continue;
$expires[$expire[1]] = $expire[1]; }
} $expires[$expire[1]] = $expire[1];
array_shift($clog); }
array_shift($clog);
} }
// echo "remove routes\n"; // echo "remove routes\n";
if(count($expires) > 0) { if (count($expires) > 0) {
foreach ($expires as $prefix) { foreach ($expires as $prefix) {
echo "/sbin/route delete -inet6 {$prefix['prefix']}\n"; echo "/sbin/route delete -inet6 {$prefix['prefix']}\n";
array_shift($expires); array_shift($expires);
} }
} }
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment