PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/rutorrent/home/req/vnstat.php

https://gitlab.com/billyprice1/QuickBox
PHP | 136 lines | 108 code | 15 blank | 13 comment | 38 complexity | 403509da0758ad7fa401ee733c90a5e8 MD5 | raw file
  1. <?php
  2. // Valid values for other parameters you can pass to the script.
  3. // Input parameters will always be limited to one of the values listed here.
  4. // If a parameter is not provided or invalid it will revert to the default,
  5. // the first parameter in the list.
  6. if (isset($_SERVER['PHP_SELF'])) {
  7. $script = $_SERVER['PHP_SELF'];
  8. } elseif (isset($_SERVER['SCRIPT_NAME'])) {
  9. $script = $_SERVER['SCRIPT_NAME'];
  10. } else {
  11. die('can\'t determine script name!');
  12. }
  13. $page_list = array('s','h','d','m');
  14. $page_title['s'] = T('summary');
  15. $page_title['h'] = T('hours');
  16. $page_title['d'] = T('days');
  17. $page_title['m'] = T('months');
  18. //
  19. // functions
  20. //
  21. function validate_input() {
  22. global $page, $page_list;
  23. global $iface, $iface_list;
  24. //
  25. // get interface data
  26. //
  27. $page = isset($_GET['page']) ? $_GET['page'] : '';
  28. $iface = isset($_GET['if']) ? $_GET['if'] : '';
  29. if (!in_array($page, $page_list)) {
  30. $page = $page_list[0];
  31. }
  32. if (!in_array($iface, $iface_list)) {
  33. $iface = $iface_list[0];
  34. }
  35. }
  36. function get_vnstat_data($use_label=true) {
  37. global $iface, $vnstat_bin, $data_dir;
  38. global $hour,$day,$month,$top,$summary;
  39. if (!isset($vnstat_bin) || $vnstat_bin == '') {
  40. if (file_exists("$data_dir/vnstat_dump_$iface")) {
  41. $vnstat_data = file("$data_dir/vnstat_dump_$iface");
  42. } else {
  43. $vnstat_data = array();
  44. }
  45. } else {
  46. $fd = popen("$vnstat_bin --dumpdb -i $iface", "r");
  47. $buffer = '';
  48. while (!feof($fd)) {
  49. $buffer .= fgets($fd);
  50. }
  51. $vnstat_data = explode("\n", $buffer);
  52. pclose($fd);
  53. }
  54. $day = array();
  55. $hour = array();
  56. $month = array();
  57. $top = array();
  58. if (strpos($vnstat_data[0], 'Error') !== false) {
  59. return;
  60. }
  61. //
  62. // extract data
  63. //
  64. foreach($vnstat_data as $line) {
  65. $d = explode(';', trim($line));
  66. if ($d[0] == 'd') {
  67. $day[$d[1]]['time'] = $d[2];
  68. $day[$d[1]]['rx'] = $d[3] * 1024 + $d[5];
  69. $day[$d[1]]['tx'] = $d[4] * 1024 + $d[6];
  70. $day[$d[1]]['act'] = $d[7];
  71. if ($d[2] != 0 && $use_label) {
  72. $day[$d[1]]['label'] = strftime(T('datefmt_days'),$d[2]);
  73. $day[$d[1]]['img_label'] = strftime(T('datefmt_days_img'), $d[2]);
  74. } elseif($use_label) {
  75. $day[$d[1]]['label'] = '';
  76. $day[$d[1]]['img_label'] = '';
  77. }
  78. } else if ($d[0] == 'm') {
  79. $month[$d[1]]['time'] = $d[2];
  80. $month[$d[1]]['rx'] = $d[3] * 1024 + $d[5];
  81. $month[$d[1]]['tx'] = $d[4] * 1024 + $d[6];
  82. $month[$d[1]]['act'] = $d[7];
  83. if ($d[2] != 0 && $use_label) {
  84. $month[$d[1]]['label'] = strftime(T('datefmt_months'), $d[2]);
  85. $month[$d[1]]['img_label'] = strftime(T('datefmt_months_img'), $d[2]);
  86. } else if ($use_label) {
  87. $month[$d[1]]['label'] = '';
  88. $month[$d[1]]['img_label'] = '';
  89. }
  90. } else if ($d[0] == 'h') {
  91. $hour[$d[1]]['time'] = $d[2];
  92. $hour[$d[1]]['rx'] = $d[3];
  93. $hour[$d[1]]['tx'] = $d[4];
  94. $hour[$d[1]]['act'] = 1;
  95. if ($d[2] != 0 && $use_label) {
  96. $st = $d[2] - ($d[2] % 3600);
  97. $et = $st + 3600;
  98. $hour[$d[1]]['label'] = strftime(T('datefmt_hours'), $st).' - '.strftime(T('datefmt_hours'), $et);
  99. $hour[$d[1]]['img_label'] = strftime(T('datefmt_hours_img'), $d[2]);
  100. } else if ($use_label) {
  101. $hour[$d[1]]['label'] = '';
  102. $hour[$d[1]]['img_label'] = '';
  103. }
  104. } else if ($d[0] == 't') {
  105. $top[$d[1]]['time'] = $d[2];
  106. $top[$d[1]]['rx'] = $d[3] * 1024 + $d[5];
  107. $top[$d[1]]['tx'] = $d[4] * 1024 + $d[6];
  108. $top[$d[1]]['act'] = $d[7];
  109. if($use_label) {
  110. $top[$d[1]]['label'] = strftime(T('datefmt_top'), $d[2]);
  111. $top[$d[1]]['img_label'] = '';
  112. }
  113. } else {
  114. $summary[$d[0]] = isset($d[1]) ? $d[1] : '';
  115. }
  116. }
  117. rsort($day);
  118. rsort($month);
  119. rsort($hour);
  120. }
  121. ?>