-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.php
346 lines (309 loc) · 14.1 KB
/
index.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
require 'vendor/autoload.php';
if (file_exists(__DIR__ . '/config.php')) {
$config = require __DIR__ . '/config.php';
} else {
$config = [];
}
if (array_key_exists('interfaces', $config) && !empty($config['interfaces'])) {
if (array_key_exists('interface', $_GET) && in_array($_GET['interface'], $config['interfaces'])) {
$interface = $_GET['interface'];
} else {
$interface = $config['interfaces'][0];
}
} else {
$interface = null;
}
$database = new Vnstat\Database($interface);
$timezone = new DateTimeZone(date_default_timezone_get());
function formatBytes($bytes)
{
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes) . ' ' . $units[$pow];
}
function formatBitrate($bytes, $seconds)
{
$units = ['bit', 'kbit', 'mbit', 'gbit', 'tbit'];
$bits = ($bytes * 8) / $seconds;
$pow = floor(($bits ? log($bits) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bits /= (1 << (10 * $pow));
return round($bits, 2) . ' ' . $units[$pow] . '/s';
}
function formatRatio($bytesReceived, $bytesSent)
{
$total = $bytesReceived + $bytesSent;
$percentageReceived = ($bytesReceived / $total * 100);
return sprintf(
'<div class="ratio"><div style="width: %f%%;"></div></div>',
$percentageReceived
);
}
$dayFormatter = new IntlDateFormatter(
'en-GB',
IntlDateFormatter::FULL,
IntlDateFormatter::NONE,
date_default_timezone_get()
);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Network Traffic</title>
<link href="bootstrap-3.2.0-dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="bootstrap-3.2.0-dist/css/bootstrap-theme.min.css" rel="stylesheet" />
<link href="xcharts/xcharts.min.css" rel="stylesheet" />
<script type="text/javascript" src="xcharts/d3.min.js"></script>
<script type="text/javascript" src="xcharts/xcharts.min.js"></script>
<style type="text/css">
div.ratio {
display: inline-block;
width: 100px;
height: 10px;
border: 1px solid #ddd;
background-color: #222;
overflow: hidden;
}
div.ratio > div {
height: 10px;
background-color: #5cb85c;
}
g.received > rect {
fill: #5cb85c !important;
}
g.sent > rect {
fill: #222 !important;
}
th.position,
td.position {
width: 60px;
}
th.received,
td.received,
th.sent,
td.sent,
th.total,
td.total,
th.average-rate,
td.average-rate {
width: 120px;
text-align: right;
}
th.ratio,
td.ratio {
width: 120px;
}
</style>
</head>
<body>
<div class="container">
<div class="page-header">
<?php if (array_key_exists('interfaces', $config) && count($config['interfaces']) > 1): ?>
<div class="pull-right">
<div class="input-group">
<span class="input-group-addon">Interface</span>
<select class="form-control" onchange="window.location.href = '?interface=' + this.value;">
<?php foreach ($config['interfaces'] as $option): ?>
<option value="<?php echo htmlspecialchars($option); ?>"<?php if ($option === $interface): ?> selected="selected"<?php endif; ?>>
<?php echo htmlspecialchars($option); ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<?php endif; ?>
<h1>Network Traffic</h1>
</div>
<h2>Hourly</h2>
<figure style="width: 100%; height: 300px;" id="hourly-chart"></figure>
<script type="text/javascript">
<?php
$endHour = (int) date('H');
$startHour = ($endHour - 23);
if ($startHour < 0) {
$startHour = 24 + $startHour;
}
$hours = $database->getHours();
$receivedData = [
'className' => '.received',
'data' => [],
];
$sentData = [
'className' => '.sent',
'data' => [],
];
$maxBytes = 0;
for ($i = $startHour; $i < 24; $i++) {
$hour = $hours[$i];
$receivedData['data'][] = ['x' => $i, 'y' => $hour->getBytesReceived()];
$sentData['data'][] = ['x' => $i, 'y' => $hour->getBytesSent()];
$maxBytes = max($maxBytes, $hour->getBytesReceived(), $hour->getBytesSent());
}
if ($endHour !== 23) {
for ($i = 0; $i <= $endHour; $i++) {
$hour = $hours[$i];
$receivedData['data'][] = ['x' => $i, 'y' => $hour->getBytesReceived()];
$sentData['data'][] = ['x' => $i, 'y' => $hour->getBytesSent()];
$maxBytes = max($maxBytes, $hour->getBytesReceived(), $hour->getBytesSent());
}
}
?>
var chart = new xChart(
'bar',
{
"xScale": "ordinal",
"yScale": "linear",
"type": "bar",
"main": [
<?php echo json_encode($receivedData); ?>,
<?php echo json_encode($sentData); ?>
]
},
'#hourly-chart',
{
"tickHintX": -25,
"tickFormatY": function (y) {
var units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
var pow = Math.floor((y ? Math.log(y) : 0) / Math.log(1024));
pow = Math.min(pow, units.length - 1);
return (Math.round(y / (1 << (10 * pow)) * 10) / 10) + ' ' + units[pow];
},
"sortX": function (a, b) {
// This actually only works because we hacked the
// source of xcharts.min.js
return 0;
}
}
);
</script>
<h2>Daily</h2>
<table class="table table-bordered">
<thead>
<tr>
<th class="day">Day</th>
<th class="received">Received</th>
<th class="sent">Sent</th>
<th class="total">Total</th>
<th class="average-rate">Average Rate</th>
<th class="ratio">Ratio</th>
</tr>
</thead>
<tbody>
<?php foreach ($database->getDays() as $id => $entry): ?>
<?php
if (!$entry->isFilled()) {
continue;
}
// This calculation has to be done because a day may
// have more or less than 24 hours (DST or leapseconds).
$diffDate = clone $entry->getDateTime();
$diffDate->setTimezone($timezone);
$diffDate->setTime(0, 0, 0);
$startTimestamp = $diffDate->getTimestamp();
$diffDate->setTime(23, 59, 59);
$endTimestamp = $diffDate->getTimestamp();
$range = $endTimestamp - $startTimestamp;
?>
<tr>
<td class="day"><?php echo $dayFormatter->format($entry->getDateTime()); ?></td>
<td class="received"><?php echo formatBytes($entry->getBytesReceived()); ?></td>
<td class="sent"><?php echo formatBytes($entry->getBytesSent()); ?></td>
<td class="total"><?php echo formatBytes($entry->getBytesReceived() + $entry->getBytesSent()); ?></td>
<td class="average-rate"><?php echo formatBitrate($entry->getBytesReceived() + $entry->getBytesSent(), $range); ?></td>
<td class="ratio"><?php echo formatRatio($entry->getBytesReceived(), $entry->getBytesSent()); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h2>Monthly</h2>
<table class="table table-bordered">
<thead>
<tr>
<th class="month">Month</th>
<th class="received">Received</th>
<th class="sent">Sent</th>
<th class="total">Total</th>
<th class="average-rate">Average Rate</th>
<th class="ratio">Ratio</th>
</tr>
</thead>
<tbody>
<?php foreach ($database->getMonths() as $id => $entry): ?>
<?php
if (!$entry->isFilled()) {
continue;
}
$entry->getDateTime()->setTimeZone($timezone);
// And again we can't just multiply the number of days
// by the number of normal seconds to be accurate.
$diffDate = clone $entry->getDateTime();
$diffDate->setTimezone($timezone);
$diffDate->setTime(0, 0, 0);
$diffDate->modify('first day of');
$startTimestamp = $diffDate->getTimestamp();
$diffDate->modify('last day of');
$diffDate->setTime(23, 59, 59);
$endTimestamp = $diffDate->getTimestamp();
$range = $endTimestamp - $startTimestamp;
?>
<tr>
<td class="month"><?php echo $entry->getDateTime()->format('F Y'); ?></td>
<td class="received"><?php echo formatBytes($entry->getBytesReceived()); ?></td>
<td class="sent"><?php echo formatBytes($entry->getBytesSent()); ?></td>
<td class="total"><?php echo formatBytes($entry->getBytesReceived() + $entry->getBytesSent()); ?></td>
<td class="average-rate"><?php echo formatBitrate($entry->getBytesReceived() + $entry->getBytesSent(), $range); ?></td>
<td class="ratio"><?php echo formatRatio($entry->getBytesReceived(), $entry->getBytesSent()); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h2>Top 10</h2>
<table class="table table-bordered">
<thead>
<tr>
<th class="position">#</th>
<th class="day">Day</th>
<th class="received">Received</th>
<th class="sent">Sent</th>
<th class="total">Total</th>
<th class="average-rate">Average Rate</th>
<th class="ratio">Ratio</th>
</tr>
</thead>
<tbody>
<?php foreach ($database->getTop10() as $id => $entry): ?>
<?php
if (!$entry->isFilled()) {
continue;
}
// This calculation has to be done because a day may
// have more or less than 24 hours (DST or leapseconds).
$diffDate = clone $entry->getDateTime();
$diffDate->setTimezone($timezone);
$diffDate->setTime(0, 0, 0);
$startTimestamp = $diffDate->getTimestamp();
$diffDate->setTime(23, 59, 59);
$endTimestamp = $diffDate->getTimestamp();
$range = $endTimestamp - $startTimestamp;
?>
<tr>
<td class="position"><?php echo ($id + 1); ?></td>
<td class="day"><?php echo $dayFormatter->format($entry->getDateTime()); ?></td>
<td class="received"><?php echo formatBytes($entry->getBytesReceived()); ?></td>
<td class="sent"><?php echo formatBytes($entry->getBytesSent()); ?></td>
<td class="total"><?php echo formatBytes($entry->getBytesReceived() + $entry->getBytesSent()); ?></td>
<td class="average-rate"><?php echo formatBitrate($entry->getBytesReceived() + $entry->getBytesSent(), $range); ?></td>
<td class="ratio"><?php echo formatRatio($entry->getBytesReceived(), $entry->getBytesSent()); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</body>
</html>