File PHP errors when port doesn't exist

This commit is contained in:
bergware
2021-06-26 13:52:56 +02:00
parent ef385cec82
commit dd6dcd5c8e

View File

@@ -29,11 +29,15 @@ function ports() {
return $ports;
}
function port_get_contents($port) {
return file_exists($port) ? @file_get_contents($port) : 0;
}
// initialize variables
$time0 = $time = microtime(true);
foreach (ports() as $port) {
$data[$port]['rx'] = (float)file_get_contents("$net/$port/statistics/rx_bytes");
$data[$port]['tx'] = (float)file_get_contents("$net/$port/statistics/tx_bytes");
$data[$port]['rx'] = (float)port_get_contents("$net/$port/statistics/rx_bytes");
$data[$port]['tx'] = (float)port_get_contents("$net/$port/statistics/tx_bytes");
}
// loop
while (true) {
@@ -41,8 +45,8 @@ while (true) {
$ts = $time - $time0;
foreach (ports() as $port) {
// inbound + outbound speed
$rx = (float)file_get_contents("$net/$port/statistics/rx_bytes");
$tx = (float)file_get_contents("$net/$port/statistics/tx_bytes");
$rx = (float)port_get_contents("$net/$port/statistics/rx_bytes");
$tx = (float)port_get_contents("$net/$port/statistics/tx_bytes");
if ($ts > 0) {
$rxd = ($rx-$data[$port]['rx']??0)/$ts*8;
$txd = ($tx-$data[$port]['tx']??0)/$ts*8;
@@ -57,40 +61,35 @@ while (true) {
$data[$port]['tx'] = $tx;
$echo[] = "$port\0$rx_speed\0$tx_speed\0$rxd\0$txd";
// interface general information
$mtu = file_get_contents("$net/$port/mtu");
$link = file_get_contents("$net/$port/carrier")==1;
$mtu = port_get_contents("$net/$port/mtu");
$link = true; //port_get_contents("$net/$port/carrier")==1;
if (substr($port,0,4)=='bond') {
if ($link) {
$bond_mode = str_replace('Bonding Mode: ','',file("/proc/net/bonding/$port",FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)[1]);
$bond_mode = file_exists("/proc/net/bonding/$port") ? str_replace('Bonding Mode: ','',file("/proc/net/bonding/$port",FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)[1]) : '---';
$mode[] = "$bond_mode, mtu $mtu";
} else $mode[] = "bond down";
} elseif ($port=='lo') {
$mode[] = $link ? "loopback" : "not set";
} else {
if ($link) {
$speed = file_get_contents("$net/$port/speed");
$duplex = file_get_contents("$net/$port/duplex");
$speed = port_get_contents("$net/$port/speed");
$duplex = port_get_contents("$net/$port/duplex");
$mode[] = "$speed Mbps, $duplex duplex, mtu $mtu";
} else $mode[] = "interface down";
}
// interface counters
$rxtx[] = "$rx\0$tx";
// interface errors
$rx_errors = file_get_contents("$net/$port/statistics/rx_errors");
$rx_drops = file_get_contents("$net/$port/statistics/rx_dropped");
$rx_fifo = file_get_contents("$net/$port/statistics/rx_fifo_errors");
$tx_errors = file_get_contents("$net/$port/statistics/tx_errors");
$tx_drops = file_get_contents("$net/$port/statistics/tx_dropped");
$tx_fifo = file_get_contents("$net/$port/statistics/tx_fifo_errors");
$rx_errors = port_get_contents("$net/$port/statistics/rx_errors");
$rx_drops = port_get_contents("$net/$port/statistics/rx_dropped");
$rx_fifo = port_get_contents("$net/$port/statistics/rx_fifo_errors");
$tx_errors = port_get_contents("$net/$port/statistics/tx_errors");
$tx_drops = port_get_contents("$net/$port/statistics/tx_dropped");
$tx_fifo = port_get_contents("$net/$port/statistics/tx_fifo_errors");
$stat[] = "Errors: {$rx_errors}<br>Drops: {$rx_drops}<br>Overruns: {$rx_fifo}\0Errors: {$tx_errors}<br>Drops: {$tx_drops}<br>Overruns: {$tx_fifo}";
}
$echo = implode("\n",$echo);
$mode = implode("\0",$mode);
$rxtx = implode("\0",$rxtx);
$stat = implode("\0",$stat);
publish('update3',"$echo\1$mode\1$rxtx\1$stat");
publish('update3',implode("\n",$echo)."\1".implode("\0",$mode)."\1".implode("\0",$rxtx)."\1".implode("\0",$stat));
$time0 = $time;
sleep(1);
$time = microtime(true);