From a295cf87c021093432425e8152c498d870da9f79 Mon Sep 17 00:00:00 2001 From: Benjamin Leblond Date: Wed, 27 Feb 2019 20:42:36 +0100 Subject: [PATCH 1/4] new feature data usage hourly --- ajax/bandwidth/get_bandwidth.php | 2 + ajax/bandwidth/get_bandwidth_hourly.php | 63 +++++++++++++++++++++++++ includes/data_usage.php | 35 +++++++++++--- js/bandwidthcharts.js | 12 +++-- 4 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 ajax/bandwidth/get_bandwidth_hourly.php diff --git a/ajax/bandwidth/get_bandwidth.php b/ajax/bandwidth/get_bandwidth.php index ea0c103e..a784b4d7 100644 --- a/ajax/bandwidth/get_bandwidth.php +++ b/ajax/bandwidth/get_bandwidth.php @@ -26,6 +26,8 @@ if (strlen($interface) > IFNAMSIZ) { exit('Invalid interface name.'); } +require_once './get_bandwidth_hourly.php'; + exec(sprintf('vnstat -i %s --json ', escapeshellarg($interface)), $jsonstdoutvnstat, $exitcodedaily); if ($exitcodedaily !== 0) { diff --git a/ajax/bandwidth/get_bandwidth_hourly.php b/ajax/bandwidth/get_bandwidth_hourly.php new file mode 100644 index 00000000..5c8276df --- /dev/null +++ b/ajax/bandwidth/get_bandwidth_hourly.php @@ -0,0 +1,63 @@ + array('date' => '00:00', 'rx' => 0, 'tx' => 0), + 1 => array('date' => '01:00', 'rx' => 0, 'tx' => 0), + 2 => array('date' => '02:00', 'rx' => 0, 'tx' => 0), + 3 => array('date' => '03:00', 'rx' => 0, 'tx' => 0), + 4 => array('date' => '04:00', 'rx' => 0, 'tx' => 0), + 5 => array('date' => '05:00', 'rx' => 0, 'tx' => 0), + 6 => array('date' => '06:00', 'rx' => 0, 'tx' => 0), + 7 => array('date' => '07:00', 'rx' => 0, 'tx' => 0), + 8 => array('date' => '08:00', 'rx' => 0, 'tx' => 0), + 9 => array('date' => '09:00', 'rx' => 0, 'tx' => 0), + 10 => array('date' => '10:00', 'rx' => 0, 'tx' => 0), + 11 => array('date' => '11:00', 'rx' => 0, 'tx' => 0), + 12 => array('date' => '12:00', 'rx' => 0, 'tx' => 0), + 13 => array('date' => '13:00', 'rx' => 0, 'tx' => 0), + 14 => array('date' => '14:00', 'rx' => 0, 'tx' => 0), + 15 => array('date' => '15:00', 'rx' => 0, 'tx' => 0), + 16 => array('date' => '16:00', 'rx' => 0, 'tx' => 0), + 17 => array('date' => '17:00', 'rx' => 0, 'tx' => 0), + 18 => array('date' => '18:00', 'rx' => 0, 'tx' => 0), + 19 => array('date' => '19:00', 'rx' => 0, 'tx' => 0), + 20 => array('date' => '20:00', 'rx' => 0, 'tx' => 0), + 21 => array('date' => '21:00', 'rx' => 0, 'tx' => 0), + 22 => array('date' => '22:00', 'rx' => 0, 'tx' => 0), + 23 => array('date' => '23:00', 'rx' => 0, 'tx' => 0) + ); + + + + + exec(sprintf('vnstat -i %s --json h', escapeshellarg($interface)), $jsonstdoutvnstat, $exitcodedaily); + if ($exitcodedaily !== 0) { + exit('vnstat error'); + } + + $jsonobj = json_decode($jsonstdoutvnstat[0], true)['interfaces'][0]; + $jsonData = $jsonobj['traffic']['hours']; + for ($i = count($jsonData) - 1; $i >= 0; --$i) { + $data_template[$jsonData[$i]['id']]['rx'] = round($jsonData[$i]['rx'] / 1024, 0); + $data_template[$jsonData[$i]['id']]['tx'] = round($jsonData[$i]['tx'] / 1024, 0); + } + + $data = array(); + $hour = $jsonobj['updated']['time']['hour']; + foreach ($data_template as $key => $value) { + if ($key > $hour) { + array_push($data, $value); + } + } + foreach ($data_template as $key => $value) { + if ($key <= $hour) { + array_push($data, $value); + } + } + echo json_encode($data); + exit(0); +} diff --git a/includes/data_usage.php b/includes/data_usage.php index db2e4cb4..6436dc3f 100755 --- a/includes/data_usage.php +++ b/includes/data_usage.php @@ -5,6 +5,7 @@ */ function DisplayDataUsage(&$extraFooterScripts) { +exec("ip -o link show | awk -F ': ' '{print $2}' | grep -v lo ", $interfacesWlo); ?>
@@ -16,27 +17,48 @@ function DisplayDataUsage(&$extraFooterScripts)
-
+
+
+
+

+ + + +
+
+
+
+
+

- +
@@ -53,7 +75,6 @@ foreach ($interfacesWlo as $interface) { echo ' ' , PHP_EOL; } - ?>