2018-09-10 16:53:05 +02:00
|
|
|
<?php
|
2019-08-06 21:34:58 +02:00
|
|
|
|
2020-02-15 18:57:46 +01:00
|
|
|
require '../../includes/csrf.php';
|
2019-08-06 21:34:58 +02:00
|
|
|
|
2018-09-10 16:53:05 +02:00
|
|
|
require_once '../../includes/config.php';
|
2018-09-10 21:41:35 +02:00
|
|
|
|
2018-09-10 16:53:05 +02:00
|
|
|
$interface = filter_input(INPUT_GET, 'inet', FILTER_SANITIZE_SPECIAL_CHARS);
|
|
|
|
if (empty($interface)) {
|
|
|
|
// Use first interface if inet parameter not provided.
|
|
|
|
exec("ip -o link show | awk -F ': ' '{print $2}' | grep -v lo ", $interfacesWlo);
|
|
|
|
if (count($interfacesWlo) > 0) {
|
|
|
|
$interface = $interfacesWlo[0];
|
|
|
|
} else {
|
|
|
|
exit('No network interfaces found.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
define('IFNAMSIZ', 16);
|
|
|
|
if (strlen($interface) > IFNAMSIZ) {
|
|
|
|
exit('Interface name too long.');
|
|
|
|
} elseif(!preg_match('/^[a-zA-Z0-9]+$/', $interface)) {
|
|
|
|
exit('Invalid interface name.');
|
|
|
|
}
|
|
|
|
|
2019-02-27 20:42:36 +01:00
|
|
|
require_once './get_bandwidth_hourly.php';
|
|
|
|
|
2020-02-15 18:57:46 +01:00
|
|
|
exec(
|
|
|
|
sprintf('vnstat -i %s --json ', escapeshellarg($interface)), $jsonstdoutvnstat,
|
|
|
|
$exitcodedaily
|
|
|
|
);
|
2018-09-10 16:53:05 +02:00
|
|
|
if ($exitcodedaily !== 0) {
|
2020-02-15 18:57:46 +01:00
|
|
|
exit('vnstat error');
|
2018-09-10 16:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$jsonobj = json_decode($jsonstdoutvnstat[0], true);
|
|
|
|
$timeunits = filter_input(INPUT_GET, 'tu');
|
|
|
|
if ($timeunits === 'm') {
|
|
|
|
// months
|
2021-11-21 19:45:01 +01:00
|
|
|
$jsonData = $jsonobj['interfaces'][0]['traffic']['month'];
|
2018-09-10 16:53:05 +02:00
|
|
|
} else {
|
|
|
|
// default: days
|
2021-11-21 19:45:01 +01:00
|
|
|
$jsonData = $jsonobj['interfaces'][0]['traffic']['day'];
|
2018-09-10 16:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$datasizeunits = filter_input(INPUT_GET, 'dsu');
|
2022-04-22 19:06:05 +02:00
|
|
|
$dsu_factor = $datasizeunits == "mb" ? 1024 * 1024 : 1024;
|
2018-09-10 16:53:05 +02:00
|
|
|
header('X-Content-Type-Options: nosniff');
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
echo '[ ';
|
|
|
|
$firstelm = true;
|
|
|
|
for ($i = count($jsonData) - 1; $i >= 0; --$i) {
|
|
|
|
if ($timeunits === 'm') {
|
2020-02-15 18:57:46 +01:00
|
|
|
$dt = DateTime::createFromFormat(
|
|
|
|
'Y n', $jsonData[$i]['date']['year'].' '.
|
|
|
|
$jsonData[$i]['date']['month']
|
|
|
|
);
|
2018-09-10 16:53:05 +02:00
|
|
|
} else {
|
2020-02-15 18:57:46 +01:00
|
|
|
$dt = DateTime::createFromFormat(
|
|
|
|
'Y n j', $jsonData[$i]['date']['year'].' '.
|
2018-09-10 16:53:05 +02:00
|
|
|
$jsonData[$i]['date']['month'].' '.
|
2020-02-15 18:57:46 +01:00
|
|
|
$jsonData[$i]['date']['day']
|
|
|
|
);
|
2018-09-10 16:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($firstelm) {
|
|
|
|
$firstelm = false;
|
|
|
|
} else {
|
|
|
|
echo ',';
|
|
|
|
}
|
|
|
|
|
2022-04-22 19:06:05 +02:00
|
|
|
$datasend = round($jsonData[$i]['tx'] / $dsu_factor, 0);
|
|
|
|
$datareceived = round($jsonData[$i]['rx'] / $dsu_factor, 0);
|
2018-09-10 16:53:05 +02:00
|
|
|
|
|
|
|
if ($timeunits === 'm') {
|
|
|
|
echo '{ "date": "' , $dt->format('Y-m') , '", "rx": "' , $datareceived ,
|
|
|
|
'", "tx": "' , $datasend , '" }';
|
|
|
|
} else {
|
|
|
|
echo '{ "date": "' , $dt->format('Y-m-d') , '", "rx": "' , $datareceived ,
|
|
|
|
'", "tx": "' , $datasend , '" }';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo ' ]';
|
|
|
|
|
|
|
|
|