2014-06-18 04:11:11 -04:00
|
|
|
<?php
|
2015-07-07 09:57:41 -04:00
|
|
|
require '../autoload.php';
|
2014-06-18 04:11:11 -04:00
|
|
|
|
2014-12-19 12:06:37 -05:00
|
|
|
$datas = array();
|
|
|
|
$network = array();
|
2014-06-18 04:11:11 -04:00
|
|
|
|
2014-12-19 12:06:37 -05:00
|
|
|
// Possible commands for ifconfig and ip
|
|
|
|
$commands = array(
|
|
|
|
'ifconfig' => array('ifconfig', '/sbin/ifconfig', '/usr/bin/ifconfig', '/usr/sbin/ifconfig'),
|
|
|
|
'ip' => array('ip', '/bin/ip', '/sbin/ip', '/usr/bin/ip', '/usr/sbin/ip'),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Returns command line for retreive interfaces
|
|
|
|
function getInterfacesCommand($commands)
|
|
|
|
{
|
|
|
|
$ifconfig = Misc::whichCommand($commands['ifconfig'], ' | awk -F \'[/ |: ]\' \'{print $1}\' | sed -e \'/^$/d\'');
|
|
|
|
|
|
|
|
if (!empty($ifconfig))
|
|
|
|
{
|
|
|
|
return $ifconfig;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$ip_cmd = Misc::whichCommand($commands['ip'], ' -V', false);
|
|
|
|
|
|
|
|
if (!empty($ip_cmd))
|
|
|
|
{
|
|
|
|
return $ip_cmd.' -oneline link show | awk \'{print $2}\' | sed "s/://"';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns command line for retreive IP address from interface name
|
|
|
|
function getIpCommand($commands, $interface)
|
|
|
|
{
|
|
|
|
$ifconfig = Misc::whichCommand($commands['ifconfig'], ' '.$interface.' | awk \'/inet / {print $2}\' | cut -d \':\' -f2');
|
|
|
|
|
|
|
|
if (!empty($ifconfig))
|
|
|
|
{
|
|
|
|
return $ifconfig;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$ip_cmd = Misc::whichCommand($commands['ip'], ' -V', false);
|
|
|
|
|
|
|
|
if (!empty($ip_cmd))
|
|
|
|
{
|
|
|
|
return 'for family in inet inet6; do '.
|
|
|
|
$ip_cmd.' -oneline -family $family addr show '.$interface.' | grep -v fe80 | awk \'{print $4}\' | sed "s/\/.*//"; ' .
|
|
|
|
'done';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$getInterfaces_cmd = getInterfacesCommand($commands);
|
|
|
|
|
|
|
|
if (is_null($getInterfaces_cmd) || !(exec($getInterfaces_cmd, $getInterfaces)))
|
2014-06-18 04:11:11 -04:00
|
|
|
{
|
|
|
|
$datas[] = array('interface' => 'N.A', 'ip' => 'N.A');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-11 12:37:32 -05:00
|
|
|
foreach ($getInterfaces as $name)
|
|
|
|
{
|
|
|
|
$ip = null;
|
|
|
|
|
2014-12-19 12:06:37 -05:00
|
|
|
$getIp_cmd = getIpCommand($commands, $name);
|
2014-11-11 12:37:32 -05:00
|
|
|
|
2014-12-19 12:06:37 -05:00
|
|
|
if (is_null($getIp_cmd) || !(exec($getIp_cmd, $ip)))
|
|
|
|
{
|
|
|
|
$network[] = array(
|
|
|
|
'name' => $name,
|
|
|
|
'ip' => 'N.A',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!isset($ip[0]))
|
|
|
|
$ip[0] = '';
|
|
|
|
|
|
|
|
$network[] = array(
|
|
|
|
'name' => $name,
|
|
|
|
'ip' => $ip[0],
|
|
|
|
);
|
|
|
|
}
|
2014-11-11 12:37:32 -05:00
|
|
|
}
|
2014-06-18 04:11:11 -04:00
|
|
|
|
2014-11-11 12:37:32 -05:00
|
|
|
foreach ($network as $interface)
|
2014-06-18 04:11:11 -04:00
|
|
|
{
|
|
|
|
// Get transmit and receive datas by interface
|
2014-11-11 12:37:32 -05:00
|
|
|
exec('cat /sys/class/net/'.$interface['name'].'/statistics/tx_bytes', $getBandwidth_tx);
|
|
|
|
exec('cat /sys/class/net/'.$interface['name'].'/statistics/rx_bytes', $getBandwidth_rx);
|
2014-06-18 04:11:11 -04:00
|
|
|
|
|
|
|
$datas[] = array(
|
2014-11-11 12:37:32 -05:00
|
|
|
'interface' => $interface['name'],
|
|
|
|
'ip' => $interface['ip'],
|
2014-06-18 04:11:11 -04:00
|
|
|
'transmit' => Misc::getSize($getBandwidth_tx[0]),
|
|
|
|
'receive' => Misc::getSize($getBandwidth_rx[0]),
|
|
|
|
);
|
|
|
|
|
|
|
|
unset($getBandwidth_tx, $getBandwidth_rx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
echo json_encode($datas);
|