2809 |
rexy |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* Copyright (C) 2019 Alexander Marston (alexander.marston@gmail.com)
|
|
|
5 |
*
|
|
|
6 |
* This program is free software: you can redistribute it and/or modify
|
|
|
7 |
* it under the terms of the GNU General Public License as published by
|
|
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
|
|
9 |
* (at your option) any later version.
|
|
|
10 |
*
|
|
|
11 |
* This program is distributed in the hope that it will be useful,
|
|
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
14 |
* GNU General Public License for more details.
|
|
|
15 |
*
|
|
|
16 |
* You should have received a copy of the GNU General Public License
|
|
|
17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
class vnStat {
|
|
|
21 |
protected $executablePath;
|
|
|
22 |
protected $vnstatVersion;
|
|
|
23 |
protected $vnstatJsonVersion;
|
|
|
24 |
protected $vnstatData;
|
|
|
25 |
|
|
|
26 |
public function __construct ($executablePath) {
|
|
|
27 |
if (isset($executablePath)) {
|
|
|
28 |
$this->executablePath = $executablePath;
|
|
|
29 |
|
|
|
30 |
// Execute a command to output a json dump of the vnstat data
|
|
|
31 |
$vnstatStream = popen("$this->executablePath --json", 'r');
|
|
|
32 |
|
|
|
33 |
// Is the stream valid?
|
|
|
34 |
if (is_resource($vnstatStream)) {
|
|
|
35 |
$streamBuffer = '';
|
|
|
36 |
|
|
|
37 |
while (!feof($vnstatStream)) {
|
|
|
38 |
$streamBuffer .= fgets($vnstatStream);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
// Close the handle
|
|
|
42 |
pclose($vnstatStream);
|
|
|
43 |
|
|
|
44 |
$this->processVnstatData($streamBuffer);
|
|
|
45 |
} else {
|
|
|
46 |
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
} else {
|
|
|
51 |
die();
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
private function processVnstatData($vnstatJson) {
|
|
|
56 |
$decodedJson = json_decode($vnstatJson, true);
|
|
|
57 |
|
|
|
58 |
// Check the JSON is valid
|
|
|
59 |
if (json_last_error() != JSON_ERROR_NONE) {
|
|
|
60 |
throw new Exception('JSON is invalid');
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
$this->vnstatData = $decodedJson;
|
|
|
64 |
$this->vnstatVersion = $decodedJson['vnstatversion'];
|
|
|
65 |
$this->vnstatJsonVersion = $decodedJson['jsonversion'];
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public function getVnstatVersion() {
|
|
|
69 |
return $this->vnstatVersion;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public function getVnstatJsonVersion() {
|
|
|
73 |
return $this->vnstatJsonVersion;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function getInterfaces() {
|
|
|
77 |
// Create a placeholder array
|
|
|
78 |
$vnstatInterfaces = [];
|
|
|
79 |
|
|
|
80 |
foreach($this->vnstatData['interfaces'] as $interface) {
|
3106 |
rexy |
81 |
if ($this->vnstatJsonVersion == 1) {
|
2809 |
rexy |
82 |
array_push($vnstatInterfaces, $interface['id']);
|
3106 |
rexy |
83 |
} else {
|
|
|
84 |
array_push($vnstatInterfaces, $interface['name']);
|
|
|
85 |
}
|
2809 |
rexy |
86 |
}
|
|
|
87 |
|
|
|
88 |
return $vnstatInterfaces;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function getInterfaceData($timeperiod, $type, $interface) {
|
|
|
92 |
// If json version equals 1, add an 's' onto the end of each type.
|
|
|
93 |
// e.g. 'top' becomes 'tops'
|
3106 |
rexy |
94 |
$typeAppend = '';
|
2809 |
rexy |
95 |
if ($this->vnstatJsonVersion == 1) {
|
|
|
96 |
$typeAppend = 's';
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// Blank placeholder
|
|
|
100 |
$trafficData = [];
|
3106 |
rexy |
101 |
$i = -1;
|
2809 |
rexy |
102 |
|
|
|
103 |
// Get the array index for the chosen interface
|
3106 |
rexy |
104 |
if ($this->vnstatJsonVersion == 1) {
|
|
|
105 |
$arrayIndex = array_search($interface, array_column($this->vnstatData['interfaces'], 'id'));
|
|
|
106 |
} else {
|
|
|
107 |
$arrayIndex = array_search($interface, array_column($this->vnstatData['interfaces'], 'name'));
|
|
|
108 |
}
|
2809 |
rexy |
109 |
|
|
|
110 |
if ($timeperiod == 'top10') {
|
|
|
111 |
if ($type == 'table') {
|
|
|
112 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['top'.$typeAppend] as $traffic) {
|
|
|
113 |
if (is_array($traffic)) {
|
|
|
114 |
$i++;
|
|
|
115 |
|
|
|
116 |
$trafficData[$i]['label'] = date('d/m/Y', strtotime($traffic['date']['month'] . "/" . $traffic['date']['day'] . "/" . $traffic['date']['year']));;
|
|
|
117 |
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
118 |
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
119 |
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
120 |
$trafficData[$i]['totalraw'] = ($traffic['rx'] + $traffic['tx']);
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
3106 |
rexy |
126 |
if (($this->vnstatJsonVersion > 1) && ($timeperiod == 'five')) {
|
|
|
127 |
if ($type == 'table') {
|
|
|
128 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['fiveminute'] as $traffic) {
|
|
|
129 |
if (is_array($traffic)) {
|
|
|
130 |
$i++;
|
|
|
131 |
|
|
|
132 |
$trafficData[$i]['label'] = date("d/m/Y H:i", mktime($traffic['time']['hour'], $traffic['time']['minute'], 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']));
|
|
|
133 |
$trafficData[$i]['time'] = mktime($traffic['time']['hour'], $traffic['time']['minute'], 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
|
|
|
134 |
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
135 |
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
136 |
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
} else if ($type == 'graph') {
|
|
|
140 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['fiveminute'] as $traffic) {
|
|
|
141 |
if (is_array($traffic)) {
|
|
|
142 |
$i++;
|
|
|
143 |
|
|
|
144 |
$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d, %d, %d)", $traffic['date']['year'], $traffic['date']['month']-1, $traffic['date']['day'], $traffic['time']['hour'], $traffic['time']['minute']);
|
|
|
145 |
$trafficData[$i]['time'] = mktime($traffic['time']['hour'], $traffic['time']['minute'], 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
|
|
|
146 |
$trafficData[$i]['rx'] = kibibytesToBytes($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
147 |
$trafficData[$i]['tx'] = kibibytesToBytes($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
148 |
$trafficData[$i]['total'] = kibibytesToBytes(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
2809 |
rexy |
154 |
if ($timeperiod == 'hourly') {
|
|
|
155 |
if ($type == 'table') {
|
|
|
156 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['hour'.$typeAppend] as $traffic) {
|
|
|
157 |
if (is_array($traffic)) {
|
|
|
158 |
$i++;
|
|
|
159 |
|
|
|
160 |
if ($this->vnstatJsonVersion == 1) {
|
|
|
161 |
$hour = $traffic['id'];
|
|
|
162 |
} else {
|
|
|
163 |
$hour = $traffic['time']['hour'];
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
$trafficData[$i]['label'] = date("d/m/Y H:i", mktime($hour, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']));
|
|
|
167 |
$trafficData[$i]['time'] = mktime($hour, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
|
|
|
168 |
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
169 |
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
170 |
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
|
|
|
175 |
} else if ($type == 'graph') {
|
|
|
176 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['hour'.$typeAppend] as $traffic) {
|
|
|
177 |
if (is_array($traffic)) {
|
|
|
178 |
$i++;
|
|
|
179 |
|
|
|
180 |
if ($this->vnstatJsonVersion == 1) {
|
|
|
181 |
$hour = $traffic['id'];
|
|
|
182 |
} else {
|
|
|
183 |
$hour = $traffic['time']['hour'];
|
|
|
184 |
}
|
|
|
185 |
|
3106 |
rexy |
186 |
$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d, %d)", $traffic['date']['year'], $traffic['date']['month']-1, $traffic['date']['day'], $hour);
|
|
|
187 |
$trafficData[$i]['time'] = mktime($hour, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
|
2809 |
rexy |
188 |
$trafficData[$i]['rx'] = kibibytesToBytes($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
189 |
$trafficData[$i]['tx'] = kibibytesToBytes($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
190 |
$trafficData[$i]['total'] = kibibytesToBytes(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
if ($timeperiod == 'daily') {
|
|
|
197 |
if ($type == 'table') {
|
|
|
198 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['day'.$typeAppend] as $traffic) {
|
|
|
199 |
if (is_array($traffic)) {
|
|
|
200 |
$i++;
|
|
|
201 |
|
|
|
202 |
$trafficData[$i]['label'] = date('d/m/Y', mktime(0, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']));
|
3106 |
rexy |
203 |
$trafficData[$i]['time'] = mktime(0, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
|
2809 |
rexy |
204 |
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
205 |
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
206 |
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
} else if ($type == 'graph') {
|
|
|
210 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['day'.$typeAppend] as $traffic) {
|
|
|
211 |
if (is_array($traffic)) {
|
|
|
212 |
$i++;
|
|
|
213 |
|
3106 |
rexy |
214 |
$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d)", $traffic['date']['year'], $traffic['date']['month']-1, $traffic['date']['day']);
|
|
|
215 |
$trafficData[$i]['time'] = mktime(0, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
|
2809 |
rexy |
216 |
$trafficData[$i]['rx'] = kibibytesToBytes($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
217 |
$trafficData[$i]['tx'] = kibibytesToBytes($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
218 |
$trafficData[$i]['total'] = kibibytesToBytes(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
if ($timeperiod == 'monthly') {
|
|
|
225 |
if ($type == 'table') {
|
|
|
226 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['month'.$typeAppend] as $traffic) {
|
|
|
227 |
if (is_array($traffic)) {
|
|
|
228 |
$i++;
|
|
|
229 |
|
|
|
230 |
$trafficData[$i]['label'] = date('F Y', mktime(0, 0, 0, $traffic['date']['month'], 10, $traffic['date']['year']));
|
3106 |
rexy |
231 |
$trafficData[$i]['time'] = mktime(0, 0, 0, $traffic['date']['month'], 10, $traffic['date']['year']);
|
2809 |
rexy |
232 |
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
233 |
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
234 |
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
} else if ($type == 'graph') {
|
|
|
238 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['month'.$typeAppend] as $traffic) {
|
|
|
239 |
if (is_array($traffic)) {
|
|
|
240 |
$i++;
|
|
|
241 |
|
3106 |
rexy |
242 |
$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d)", $traffic['date']['year'], $traffic['date']['month'] - 1, 10);
|
|
|
243 |
$trafficData[$i]['time'] = mktime(0, 0, 0, $traffic['date']['month'], 10, $traffic['date']['year']);
|
2809 |
rexy |
244 |
$trafficData[$i]['rx'] = kibibytesToBytes($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
245 |
$trafficData[$i]['tx'] = kibibytesToBytes($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
246 |
$trafficData[$i]['total'] = kibibytesToBytes(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
247 |
}
|
|
|
248 |
}
|
|
|
249 |
}
|
|
|
250 |
}
|
|
|
251 |
|
3106 |
rexy |
252 |
if ($timeperiod != 'top10') {
|
|
|
253 |
usort($trafficData, 'sortingFunction');
|
|
|
254 |
}
|
|
|
255 |
|
2809 |
rexy |
256 |
if ($type == 'graph') {
|
|
|
257 |
// Get the largest value and then prefix (B, KB, MB, GB, etc)
|
|
|
258 |
$trafficLargestValue = getLargestValue($trafficData);
|
3106 |
rexy |
259 |
$trafficScale = getScale($trafficLargestValue);
|
|
|
260 |
$trafficLargestPrefix = getLargestPrefix($trafficScale);
|
|
|
261 |
$trafficBase = getBaseValue($trafficData, $trafficScale);
|
|
|
262 |
if (($trafficBase < .0099) && ($trafficScale >= 1))
|
|
|
263 |
{
|
|
|
264 |
$trafficScale = $trafficScale - 1;
|
|
|
265 |
$trafficLargestPrefix = getLargestPrefix($trafficScale);
|
|
|
266 |
$trafficBase = getBaseValue($trafficData, $trafficScale);
|
|
|
267 |
}
|
2809 |
rexy |
268 |
|
3106 |
rexy |
269 |
foreach($trafficData as &$value) {
|
|
|
270 |
$value['rx'] = formatBytesTo($value['rx'], $trafficScale);
|
|
|
271 |
$value['tx'] = formatBytesTo($value['tx'], $trafficScale);
|
|
|
272 |
$value['total'] = formatBytesTo($value['total'], $trafficScale);
|
2809 |
rexy |
273 |
}
|
3106 |
rexy |
274 |
|
|
|
275 |
unset($value);
|
|
|
276 |
$trafficData[0]['delimiter'] = $trafficLargestPrefix;
|
|
|
277 |
$trafficData[0]['base'] = $trafficBase;
|
2809 |
rexy |
278 |
}
|
|
|
279 |
|
|
|
280 |
return $trafficData;
|
|
|
281 |
}
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
?>
|