You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.7 KiB

  1. <?php
  2. require '../../includes/csrf.php';
  3. if (filter_input(INPUT_GET, 'tu') == 'h') {
  4. header('X-Content-Type-Options: nosniff');
  5. header('Content-Type: application/json');
  6. $data_template = array(
  7. 0 => array('date' => '00:00', 'rx' => 0, 'tx' => 0),
  8. 1 => array('date' => '01:00', 'rx' => 0, 'tx' => 0),
  9. 2 => array('date' => '02:00', 'rx' => 0, 'tx' => 0),
  10. 3 => array('date' => '03:00', 'rx' => 0, 'tx' => 0),
  11. 4 => array('date' => '04:00', 'rx' => 0, 'tx' => 0),
  12. 5 => array('date' => '05:00', 'rx' => 0, 'tx' => 0),
  13. 6 => array('date' => '06:00', 'rx' => 0, 'tx' => 0),
  14. 7 => array('date' => '07:00', 'rx' => 0, 'tx' => 0),
  15. 8 => array('date' => '08:00', 'rx' => 0, 'tx' => 0),
  16. 9 => array('date' => '09:00', 'rx' => 0, 'tx' => 0),
  17. 10 => array('date' => '10:00', 'rx' => 0, 'tx' => 0),
  18. 11 => array('date' => '11:00', 'rx' => 0, 'tx' => 0),
  19. 12 => array('date' => '12:00', 'rx' => 0, 'tx' => 0),
  20. 13 => array('date' => '13:00', 'rx' => 0, 'tx' => 0),
  21. 14 => array('date' => '14:00', 'rx' => 0, 'tx' => 0),
  22. 15 => array('date' => '15:00', 'rx' => 0, 'tx' => 0),
  23. 16 => array('date' => '16:00', 'rx' => 0, 'tx' => 0),
  24. 17 => array('date' => '17:00', 'rx' => 0, 'tx' => 0),
  25. 18 => array('date' => '18:00', 'rx' => 0, 'tx' => 0),
  26. 19 => array('date' => '19:00', 'rx' => 0, 'tx' => 0),
  27. 20 => array('date' => '20:00', 'rx' => 0, 'tx' => 0),
  28. 21 => array('date' => '21:00', 'rx' => 0, 'tx' => 0),
  29. 22 => array('date' => '22:00', 'rx' => 0, 'tx' => 0),
  30. 23 => array('date' => '23:00', 'rx' => 0, 'tx' => 0)
  31. );
  32. exec(sprintf('vnstat -i %s --json h', escapeshellarg($interface)), $jsonstdoutvnstat, $exitcodedaily);
  33. if ($exitcodedaily !== 0) {
  34. exit('vnstat error');
  35. }
  36. $datasizeunits = filter_input(INPUT_GET, 'dsu');
  37. $dsu_factor = $datasizeunits == "mb" ? 1024 * 1024 : 1024;
  38. $jsonobj = json_decode($jsonstdoutvnstat[0], true)['interfaces'][0];
  39. $jsonData = $jsonobj['traffic']['hour'];
  40. for ($i = count($jsonData) - 1; $i >= 0 && $i >= count($jsonData)-25; --$i) {
  41. $data_template[$jsonData[$i]['time']['hour']]['rx'] = round($jsonData[$i]['rx'] / $dsu_factor, 0);
  42. $data_template[$jsonData[$i]['time']['hour']]['tx'] = round($jsonData[$i]['tx'] / $dsu_factor, 0);
  43. }
  44. $data = array();
  45. $hour = $jsonobj['updated']['time']['hour'];
  46. foreach ($data_template as $key => $value) {
  47. if ($key > $hour) {
  48. array_push($data, $value);
  49. }
  50. }
  51. foreach ($data_template as $key => $value) {
  52. if ($key <= $hour) {
  53. array_push($data, $value);
  54. }
  55. }
  56. echo json_encode($data);
  57. exit(0);
  58. }