-
Notifications
You must be signed in to change notification settings - Fork 71
/
vnstat.php
211 lines (193 loc) · 6.5 KB
/
vnstat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
//
// vnStat PHP frontend (c)2006-2010 Bjorge Dijkstra ([email protected])
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//
// see file COPYING or at http://www.gnu.org/licenses/gpl.html
// for more information.
//
//
// Valid values for other parameters you can pass to the script.
// Input parameters will always be limited to one of the values listed here.
// If a parameter is not provided or invalid it will revert to the default,
// the first parameter in the list.
//
if (isset($_SERVER['PHP_SELF']))
{
$script = $_SERVER['PHP_SELF'];
}
elseif (isset($_SERVER['SCRIPT_NAME']))
{
$script = $_SERVER['SCRIPT_NAME'];
}
else
{
die('can\'t determine script name!');
}
$page_list = array('s','h','d','m');
$graph_list = array('large','small','none');
$page_title['s'] = T('summary');
$page_title['h'] = T('hours');
$page_title['d'] = T('days');
$page_title['m'] = T('months');
//
// functions
//
function validate_input()
{
global $page, $page_list;
global $iface, $iface_list;
global $graph, $graph_list;
global $colorscheme, $style;
//
// get interface data
//
$page = isset($_GET['page']) ? $_GET['page'] : '';
$iface = isset($_GET['if']) ? $_GET['if'] : '';
$graph = isset($_GET['graph']) ? $_GET['graph'] : '';
$style = isset($_GET['style']) ? $_GET['style'] : '';
if (!in_array($page, $page_list))
{
$page = $page_list[0];
}
if (!in_array($iface, $iface_list))
{
$iface = $iface_list[0];
}
if (!in_array($graph, $graph_list))
{
$graph = $graph_list[0];
}
$tp = "./themes/$style";
if (!is_dir($tp) || !file_exists("$tp/theme.php") || !preg_match('/^[a-z0-9-_]+$/i', $style))
{
$style = DEFAULT_COLORSCHEME;
}
}
function get_vnstat_data($use_label=true)
{
global $iface, $vnstat_bin, $data_dir;
global $hour,$day,$month,$top,$summary;
$vnstat_data = array();
if (!isset($vnstat_bin) || $vnstat_bin == '')
{
if (file_exists("$data_dir/vnstat_dump_$iface"))
{
$vnstat_data = file("$data_dir/vnstat_dump_$iface");
}
}
else
{
$fd = popen("$vnstat_bin --dumpdb -i $iface", "r");
if (is_resource($fd))
{
$buffer = '';
while (!feof($fd)) {
$buffer .= fgets($fd);
}
$vnstat_data = explode("\n", $buffer);
pclose($fd);
}
}
$day = array();
$hour = array();
$month = array();
$top = array();
if (isset($vnstat_data[0]) && strpos($vnstat_data[0], 'Error') !== false) {
return;
}
//
// extract data
//
foreach($vnstat_data as $line)
{
$d = explode(';', trim($line));
if ($d[0] == 'd')
{
$day[$d[1]]['time'] = $d[2];
$day[$d[1]]['rx'] = $d[3] * 1024 + $d[5];
$day[$d[1]]['tx'] = $d[4] * 1024 + $d[6];
$day[$d[1]]['act'] = $d[7];
if ($d[2] != 0 && $use_label)
{
$day[$d[1]]['label'] = strftime(T('datefmt_days'),$d[2]);
$day[$d[1]]['img_label'] = strftime(T('datefmt_days_img'), $d[2]);
}
elseif($use_label)
{
$day[$d[1]]['label'] = '';
$day[$d[1]]['img_label'] = '';
}
}
else if ($d[0] == 'm')
{
$month[$d[1]]['time'] = $d[2];
$month[$d[1]]['rx'] = $d[3] * 1024 + $d[5];
$month[$d[1]]['tx'] = $d[4] * 1024 + $d[6];
$month[$d[1]]['act'] = $d[7];
if ($d[2] != 0 && $use_label)
{
$month[$d[1]]['label'] = strftime(T('datefmt_months'), $d[2]);
$month[$d[1]]['img_label'] = strftime(T('datefmt_months_img'), $d[2]);
}
else if ($use_label)
{
$month[$d[1]]['label'] = '';
$month[$d[1]]['img_label'] = '';
}
}
else if ($d[0] == 'h')
{
$hour[$d[1]]['time'] = $d[2];
$hour[$d[1]]['rx'] = $d[3];
$hour[$d[1]]['tx'] = $d[4];
$hour[$d[1]]['act'] = 1;
if ($d[2] != 0 && $use_label)
{
$st = $d[2] - ($d[2] % 3600);
$et = $st + 3600;
$hour[$d[1]]['label'] = strftime(T('datefmt_hours'), $st).' - '.strftime(T('datefmt_hours'), $et);
$hour[$d[1]]['img_label'] = strftime(T('datefmt_hours_img'), $d[2]);
}
else if ($use_label)
{
$hour[$d[1]]['label'] = '';
$hour[$d[1]]['img_label'] = '';
}
}
else if ($d[0] == 't')
{
$top[$d[1]]['time'] = $d[2];
$top[$d[1]]['rx'] = $d[3] * 1024 + $d[5];
$top[$d[1]]['tx'] = $d[4] * 1024 + $d[6];
$top[$d[1]]['act'] = $d[7];
if($use_label)
{
$top[$d[1]]['label'] = strftime(T('datefmt_top'), $d[2]);
$top[$d[1]]['img_label'] = '';
}
}
else
{
$summary[$d[0]] = isset($d[1]) ? $d[1] : '';
}
}
rsort($day);
rsort($month);
rsort($hour);
}
?>