#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2021, Lime Technology
 * Copyright 2012-2021, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
$docroot = '/usr/local/emhttp';
$net     = '/sys/class/net';
$bond    = '/proc/net/bonding';
$data    = [];

require_once "$docroot/webGui/include/Helpers.php";
require_once "$docroot/webGui/include/publish.php";

// satisfy translations (not used)
if (!function_exists('_')) {
  function _($text) {return $text;}
}
function ports() {
  global $net;
  exec("ls $net|grep -Po '^(lo|(bond|eth)\d+)$'",$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)port_get_contents("$net/$port/statistics/rx_bytes");
  $data[$port]['tx'] = (float)port_get_contents("$net/$port/statistics/tx_bytes");
}
// loop
while (true) {
  $echo = $mode = $rxtx = $stat = [];
  $ts = $time - $time0;
  foreach (ports() as $port) {
    // inbound + outbound speed
    $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;
      $rx_speed = my_scale($rxd,$unit,1,-1).' '.str_replace('B','b',$unit).'ps';
      $tx_speed = my_scale($txd,$unit,1,-1).' '.str_replace('B','b',$unit).'ps';
    } else {
      $rxd = $txd = 0;
      $rx_speed = '---';
      $tx_speed = '---';
    }
    $data[$port]['rx'] = $rx;
    $data[$port]['tx'] = $tx;
    $echo[] = "$port\0$rx_speed\0$tx_speed\0$rxd\0$txd";
    // interface general information
    $mtu = port_get_contents("$net/$port/mtu");
    $link = port_get_contents("$net/$port/carrier")==1;
    if (substr($port,0,4)=='bond') {
      if ($link) {
        $bond_mode = file_exists("$bond/$port") ? str_replace('Bonding Mode: ','',@file("$bond/$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 = 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 = 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}";
  }

  publish('update3',implode("\n",$echo)."\1".implode("\0",$mode)."\1".implode("\0",$rxtx)."\1".implode("\0",$stat));
  $time0 = $time;
  sleep(1);
  $time = microtime(true);
}
