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) {
|
|
|
81 |
array_push($vnstatInterfaces, $interface['id']);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
return $vnstatInterfaces;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public function getInterfaceData($timeperiod, $type, $interface) {
|
|
|
88 |
// If json version equals 1, add an 's' onto the end of each type.
|
|
|
89 |
// e.g. 'top' becomes 'tops'
|
|
|
90 |
if ($this->vnstatJsonVersion == 1) {
|
|
|
91 |
$typeAppend = 's';
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
// Blank placeholder
|
|
|
95 |
$trafficData = [];
|
|
|
96 |
|
|
|
97 |
// Get the array index for the chosen interface
|
|
|
98 |
$arrayIndex = array_search($interface, array_column($this->vnstatData['interfaces'], 'id'));
|
|
|
99 |
|
|
|
100 |
if ($timeperiod == 'top10') {
|
|
|
101 |
if ($type == 'table') {
|
|
|
102 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['top'.$typeAppend] as $traffic) {
|
|
|
103 |
if (is_array($traffic)) {
|
|
|
104 |
$i++;
|
|
|
105 |
|
|
|
106 |
$trafficData[$i]['label'] = date('d/m/Y', strtotime($traffic['date']['month'] . "/" . $traffic['date']['day'] . "/" . $traffic['date']['year']));;
|
|
|
107 |
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
108 |
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
109 |
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
110 |
$trafficData[$i]['totalraw'] = ($traffic['rx'] + $traffic['tx']);
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if ($timeperiod == 'hourly') {
|
|
|
117 |
if ($type == 'table') {
|
|
|
118 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['hour'.$typeAppend] as $traffic) {
|
|
|
119 |
if (is_array($traffic)) {
|
|
|
120 |
$i++;
|
|
|
121 |
|
|
|
122 |
if ($this->vnstatJsonVersion == 1) {
|
|
|
123 |
$hour = $traffic['id'];
|
|
|
124 |
} else {
|
|
|
125 |
$hour = $traffic['time']['hour'];
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
$trafficData[$i]['label'] = date("d/m/Y H:i", mktime($hour, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']));
|
|
|
129 |
$trafficData[$i]['time'] = mktime($hour, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']);
|
|
|
130 |
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
131 |
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
132 |
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
|
3091 |
rexy |
136 |
// usort($trafficData, sortingFunction);
|
2809 |
rexy |
137 |
|
|
|
138 |
} else if ($type == 'graph') {
|
|
|
139 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['hour'.$typeAppend] as $traffic) {
|
|
|
140 |
if (is_array($traffic)) {
|
|
|
141 |
$i++;
|
|
|
142 |
|
|
|
143 |
if ($this->vnstatJsonVersion == 1) {
|
|
|
144 |
$hour = $traffic['id'];
|
|
|
145 |
} else {
|
|
|
146 |
$hour = $traffic['time']['hour'];
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d, %d, %d, %d)", $traffic['date']['year'], $traffic['date']['month']-1, $traffic['date']['day'], $hour, 0, 0);
|
|
|
150 |
$trafficData[$i]['rx'] = kibibytesToBytes($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
151 |
$trafficData[$i]['tx'] = kibibytesToBytes($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
152 |
$trafficData[$i]['total'] = kibibytesToBytes(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
if ($timeperiod == 'daily') {
|
|
|
159 |
if ($type == 'table') {
|
|
|
160 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['day'.$typeAppend] as $traffic) {
|
|
|
161 |
if (is_array($traffic)) {
|
|
|
162 |
$i++;
|
|
|
163 |
|
|
|
164 |
$trafficData[$i]['label'] = date('d/m/Y', mktime(0, 0, 0, $traffic['date']['month'], $traffic['date']['day'], $traffic['date']['year']));
|
|
|
165 |
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
166 |
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
167 |
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
} else if ($type == 'graph') {
|
|
|
171 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['day'.$typeAppend] as $traffic) {
|
|
|
172 |
if (is_array($traffic)) {
|
|
|
173 |
$i++;
|
|
|
174 |
|
|
|
175 |
$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d, %d, %d, %d)", $traffic['date']['year'], $traffic['date']['month']-1, $traffic['date']['day'], 0, 0, 0);
|
|
|
176 |
$trafficData[$i]['rx'] = kibibytesToBytes($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
177 |
$trafficData[$i]['tx'] = kibibytesToBytes($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
178 |
$trafficData[$i]['total'] = kibibytesToBytes(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
if ($timeperiod == 'monthly') {
|
|
|
185 |
if ($type == 'table') {
|
|
|
186 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['month'.$typeAppend] as $traffic) {
|
|
|
187 |
if (is_array($traffic)) {
|
|
|
188 |
$i++;
|
|
|
189 |
|
|
|
190 |
$trafficData[$i]['label'] = date('F Y', mktime(0, 0, 0, $traffic['date']['month'], 10, $traffic['date']['year']));
|
|
|
191 |
$trafficData[$i]['rx'] = formatSize($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
192 |
$trafficData[$i]['tx'] = formatSize($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
193 |
$trafficData[$i]['total'] = formatSize(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
} else if ($type == 'graph') {
|
|
|
197 |
foreach ($this->vnstatData['interfaces'][$arrayIndex]['traffic']['month'.$typeAppend] as $traffic) {
|
|
|
198 |
if (is_array($traffic)) {
|
|
|
199 |
$i++;
|
|
|
200 |
|
|
|
201 |
$trafficData[$i]['label'] = sprintf("Date(%d, %d, %d, %d, %d, %d)", $traffic['date']['year'], $traffic['date']['month'] - 1, 10, 0, 0, 0);
|
|
|
202 |
$trafficData[$i]['rx'] = kibibytesToBytes($traffic['rx'], $this->vnstatJsonVersion);
|
|
|
203 |
$trafficData[$i]['tx'] = kibibytesToBytes($traffic['tx'], $this->vnstatJsonVersion);
|
|
|
204 |
$trafficData[$i]['total'] = kibibytesToBytes(($traffic['rx'] + $traffic['tx']), $this->vnstatJsonVersion);
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
if ($type == 'graph') {
|
|
|
211 |
// Get the largest value and then prefix (B, KB, MB, GB, etc)
|
|
|
212 |
$trafficLargestValue = getLargestValue($trafficData);
|
|
|
213 |
$trafficLargestPrefix = getLargestPrefix($trafficLargestValue);
|
|
|
214 |
|
|
|
215 |
foreach($trafficData as $key => $value) {
|
|
|
216 |
$trafficData[$key]['rx'] = formatBytesTo($value['rx'], $trafficLargestPrefix);
|
|
|
217 |
$trafficData[$key]['tx'] = formatBytesTo($value['tx'], $trafficLargestPrefix);
|
|
|
218 |
$trafficData[$key]['total'] = formatBytesTo($value['total'], $trafficLargestPrefix);
|
|
|
219 |
$trafficData[$key]['delimiter'] = $trafficLargestPrefix;
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
return $trafficData;
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
?>
|