vnstat.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. //
  3. // vnStat PHP frontend (c)2006-2010 Bjorge Dijkstra (bjd@jooz.net)
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. //
  19. //
  20. // see file COPYING or at http://www.gnu.org/licenses/gpl.html
  21. // for more information.
  22. //
  23. //
  24. // Valid values for other parameters you can pass to the script.
  25. // Input parameters will always be limited to one of the values listed here.
  26. // If a parameter is not provided or invalid it will revert to the default,
  27. // the first parameter in the list.
  28. //
  29. if (isset($_SERVER['PHP_SELF']))
  30. {
  31. $script = $_SERVER['PHP_SELF'];
  32. }
  33. elseif (isset($_SERVER['SCRIPT_NAME']))
  34. {
  35. $script = $_SERVER['SCRIPT_NAME'];
  36. }
  37. else
  38. {
  39. die('can\'t determine script name!');
  40. }
  41. $page_list = array('s','h','d','m');
  42. $graph_list = array('large','small','none');
  43. $page_title['s'] = T('summary');
  44. $page_title['h'] = T('hours');
  45. $page_title['d'] = T('days');
  46. $page_title['m'] = T('months');
  47. //
  48. // functions
  49. //
  50. function validate_input()
  51. {
  52. global $page, $page_list;
  53. global $iface, $iface_list;
  54. global $graph, $graph_list;
  55. global $colorscheme, $style;
  56. //
  57. // get interface data
  58. //
  59. $page = isset($_GET['page']) ? $_GET['page'] : '';
  60. $iface = isset($_GET['if']) ? $_GET['if'] : '';
  61. $graph = isset($_GET['graph']) ? $_GET['graph'] : '';
  62. $style = isset($_GET['style']) ? $_GET['style'] : '';
  63. if (!in_array($page, $page_list))
  64. {
  65. $page = $page_list[0];
  66. }
  67. if (!in_array($iface, $iface_list))
  68. {
  69. $iface = $iface_list[0];
  70. }
  71. if (!in_array($graph, $graph_list))
  72. {
  73. $graph = $graph_list[0];
  74. }
  75. $tp = "./themes/$style";
  76. if (!is_dir($tp) || !file_exists("$tp/theme.php") || !preg_match('/^[a-z0-9-_]+$/i', $style))
  77. {
  78. $style = DEFAULT_COLORSCHEME;
  79. }
  80. }
  81. function get_vnstat_data($use_label=true)
  82. {
  83. global $iface, $vnstat_bin, $data_dir;
  84. global $hour,$day,$month,$top,$summary;
  85. $vnstat_data = array();
  86. if (!isset($vnstat_bin) || $vnstat_bin == '')
  87. {
  88. if (file_exists("$data_dir/vnstat_dump_$iface"))
  89. {
  90. $file_data = file_get_contents("$data_dir/vnstat_dump_$iface");
  91. $vnstat_data = json_decode($file_data, TRUE);
  92. }
  93. }
  94. else
  95. {
  96. // FIXME: use mode and limit parameter to reduce data that needs to be parsed
  97. $fd = popen("$vnstat_bin --json -i $iface", "r");
  98. if (is_resource($fd))
  99. {
  100. $buffer = '';
  101. while (!feof($fd)) {
  102. $buffer .= fgets($fd);
  103. }
  104. pclose($fd);
  105. $vnstat_data = json_decode($buffer, TRUE);
  106. }
  107. }
  108. $day = array();
  109. $hour = array();
  110. $month = array();
  111. $top = array();
  112. if (!isset($vnstat_data) || !isset($vnstat_data['vnstatversion'])) {
  113. return;
  114. }
  115. $iface_data = $vnstat_data['interfaces'][0];
  116. $traffic_data = $iface_data['traffic'];
  117. // data are grouped for hour, day, month, ... and a data entry looks like this:
  118. // [0] => Array
  119. // (
  120. // [id] => 48032
  121. // [date] => Array
  122. // (
  123. // [year] => 2020
  124. // [month] => 8
  125. // [day] => 23
  126. // )
  127. // [time] => Array
  128. // (
  129. // [hour] => 16
  130. // [minute] => 0
  131. // )
  132. // [rx] => 2538730
  133. // [tx] => 2175640
  134. // )
  135. // per-day data
  136. // FIXME: instead of using array_reverse, sorting by date/time keys would be more reliable
  137. $day_data = array_reverse($traffic_data['day']);
  138. for($i = 0; $i < min(30, count($day_data)); $i++) {
  139. $d = $day_data[$i];
  140. $ts = mktime(0, 0, 0, $d['date']['month'], $d['date']['day'], $d['date']['year']);
  141. $day[$i]['time'] = $ts;
  142. $day[$i]['rx'] = $d['rx'] / 1024;
  143. $day[$i]['tx'] = $d['tx'] / 1024;
  144. $day[$i]['act'] = 1;
  145. if($use_label) {
  146. $day[$i]['label'] = strftime(T('datefmt_days'), $ts);
  147. $day[$i]['img_label'] = strftime(T('datefmt_days_img'), $ts);
  148. }
  149. }
  150. // per-month data
  151. $month_data = array_reverse($traffic_data['month']);
  152. for($i = 0; $i < min(12, count($month_data)); $i++) {
  153. $d = $month_data[$i];
  154. $ts = mktime(0, 0, 0, $d['date']['month']+1, 0, $d['date']['year']);
  155. $month[$i]['time'] = $ts;
  156. $month[$i]['rx'] = $d['rx'] / 1024;
  157. $month[$i]['tx'] = $d['tx'] / 1024;
  158. $month[$i]['act'] = 1;
  159. if($use_label) {
  160. $month[$i]['label'] = strftime(T('datefmt_months'), $ts);
  161. $month[$i]['img_label'] = strftime(T('datefmt_months_img'), $ts);
  162. }
  163. }
  164. // per-hour data
  165. $hour_data = array_reverse($traffic_data['hour']);
  166. for($i = 0; $i < min(24, count($hour_data)); $i++) {
  167. $d = $hour_data[$i];
  168. $ts = mktime($d['time']['hour'], $d['time']['minute'], 0, $d['date']['month'], $d['date']['day'], $d['date']['year']);
  169. $hour[$i]['time'] = $ts;
  170. $hour[$i]['rx'] = $d['rx'] / 1024;
  171. $hour[$i]['tx'] = $d['tx'] / 1024;
  172. $hour[$i]['act'] = 1;
  173. if($use_label) {
  174. $hour[$i]['label'] = strftime(T('datefmt_hours'), $ts);
  175. $hour[$i]['img_label'] = strftime(T('datefmt_hours_img'), $ts);
  176. }
  177. }
  178. // top10 days data
  179. $top10_data = $traffic_data['top'];
  180. for($i = 0; $i < min(10, count($top10_data)); $i++) {
  181. $d = $top10_data[$i];
  182. $ts = mktime(0, 0, 0, $d['date']['month'], $d['date']['day'], $d['date']['year']);
  183. $top[$i]['time'] = $ts;
  184. $top[$i]['rx'] = $d['rx'] / 1024;
  185. $top[$i]['tx'] = $d['tx'] / 1024;
  186. $top[$i]['act'] = 1;
  187. if($use_label) {
  188. $top[$i]['label'] = strftime(T('datefmt_top'), $ts);
  189. $top[$i]['img_label'] = '';
  190. }
  191. }
  192. // summary data from old dumpdb command
  193. // all time total received/transmitted MB
  194. $summary['totalrx'] = $traffic_data['total']['rx'] / 1024 / 1024;
  195. $summary['totaltx'] = $traffic_data['total']['tx'] / 1024 / 1024;
  196. // FIXME: used to be "total rx kB counter" from dumpdb, no idea how to get those
  197. $summary['totalrxk'] = 0;
  198. $summary['totaltxk'] = 0;
  199. $summary['interface'] = $iface_data['name'];
  200. }
  201. ?>