forked from alexandermarston/vnstat-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
263 lines (226 loc) · 10.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
<?php
/*
* Copyright (C) 2016 Alexander Marston (alexander.marston@gmail.com)
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
require('vnstat.php'); // The vnstat information parser
require('config.php'); // Include all the configuration information
function printOptions()
{
global $interface_list;
$i = 0;
foreach ($interface_list as $interface) {
$i++;
if ($i == count($interface_list)) {
echo "<a href=\"?i=" . rawurlencode($interface) . "\">" . rawurlencode($interface) . "</a>";
} else {
echo "<a href=\"?i=" . rawurlencode($interface) . "\">" . rawurlencode($interface) . ", </a>";
}
}
}
function printTableStats($path, $type, $interface, $label)
{
echo '<table class="table table-bordered">
<thead>
<tr>
<th>' . $label . '</th>
<th>Received</th>
<th>Sent</th>
<th>Total</th>
</tr>
</thead>
<tbody>';
$data = getVnstatData($path, $type, $interface);
for ($i = 0; $i < count($data); $i++) {
$label = $data[$i]['label'];
$totalReceived = $data[$i]['rx'];
$totalSent = $data[$i]['tx'];
$totalTraffic = $data[$i]['total'];
echo '<tr>';
echo '<td>' . $label . '</td>';
echo '<td>' . $totalReceived . '</td>';
echo '<td>' . $totalSent . '</td>';
echo '<td>' . $totalTraffic . '</td>';
echo '</tr>';
}
echo '</tbody></table>';
}
$thisInterface = "";
if (isset($_GET['i'])) {
$interfaceChosen = rawurldecode($_GET['i']);
if (in_array($interfaceChosen, $interface_list, true)) {
$thisInterface = $interfaceChosen;
} else {
$thisInterface = reset($interface_list);
}
} else {
// Assume they mean the first interface
$thisInterface = reset($interface_list);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Network Traffic</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages': ['bar']});
google.charts.setOnLoadCallback(drawHourlyChart);
google.charts.setOnLoadCallback(drawDailyChart);
google.charts.setOnLoadCallback(drawMonthlyChart);
function drawHourlyChart()
{
let data = google.visualization.arrayToDataTable([
['Hour', 'Traffic In', 'Traffic Out', 'Total Traffic'],
<?php
$hourlyGraph = getVnstatData($vnstat_bin_dir, "hourlyGraph", $thisInterface);
$hourlyLargestValue = getLargestValue($hourlyGraph);
$hourlyLargestPrefix = getLargestPrefix($hourlyLargestValue);
for ($i = 0; $i < count($hourlyGraph); $i++) {
$hour = $hourlyGraph[$i]['label'];
$inTraffic = bytesToString($hourlyGraph[$i]['rx'], true, $hourlyLargestPrefix);
$outTraffic = bytesToString($hourlyGraph[$i]['tx'], true, $hourlyLargestPrefix);
$totalTraffic = bytesToString($hourlyGraph[$i]['total'], true, $hourlyLargestPrefix);
if (($hourlyGraph[$i]['label'] == "12am") && ($hourlyGraph[$i]['time'] == "0")) {
continue;
}
echo("['" . $hour . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "],\n");
}
?>
]);
let options = {
title: 'Hourly Network Traffic',
subtitle: 'over last 24 hours',
vAxis: {format: '##.## <?php echo $hourlyLargestPrefix; ?>'}
};
let chart = new google.charts.Bar(document.getElementById('hourlyNetworkTrafficGraph'));
chart.draw(data, google.charts.Bar.convertOptions(options));
}
function drawDailyChart()
{
let data = google.visualization.arrayToDataTable([
['Day', 'Traffic In', 'Traffic Out', 'Total Traffic'],
<?php
$dailyGraph = getVnstatData($vnstat_bin_dir, "dailyGraph", $thisInterface);
$dailyLargestValue = getLargestValue($dailyGraph);
$dailyLargestPrefix = getLargestPrefix($dailyLargestValue);
for ($i = 0; $i < count($dailyGraph); $i++) {
$day = $dailyGraph[$i]['label'];
$inTraffic = bytesToString($dailyGraph[$i]['rx'], true, $dailyLargestPrefix);
$outTraffic = bytesToString($dailyGraph[$i]['tx'], true, $dailyLargestPrefix);
$totalTraffic = bytesToString($dailyGraph[$i]['total'], true, $dailyLargestPrefix);
if ($dailyGraph[$i]['time'] == "0") {
continue;
}
echo("['" . $day . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "],\n");
}
?>
]);
let options = {
title: 'Daily Network Traffic',
subtitle: 'over last 29 days (most recent first)',
vAxis: {format: '##.## <?php echo $dailyLargestPrefix; ?>'}
};
let chart = new google.charts.Bar(document.getElementById('dailyNetworkTrafficGraph'));
chart.draw(data, google.charts.Bar.convertOptions(options));
}
function drawMonthlyChart()
{
let data = google.visualization.arrayToDataTable([
['Month', 'Traffic In', 'Traffic Out', 'Total Traffic'],
<?php
$monthlyGraph = getVnstatData($vnstat_bin_dir, "monthlyGraph", $thisInterface);
$monthlyLargestValue = getLargestValue($monthlyGraph);
$monthlyLargestPrefix = getLargestPrefix($monthlyLargestValue);
for ($i = 0; $i < count($monthlyGraph); $i++) {
$hour = $monthlyGraph[$i]['label'];
$inTraffic = bytesToString($monthlyGraph[$i]['rx'], true, $monthlyLargestPrefix);
$outTraffic = bytesToString($monthlyGraph[$i]['tx'], true, $monthlyLargestPrefix);
$totalTraffic = bytesToString($monthlyGraph[$i]['total'], true, $monthlyLargestPrefix);
echo("['" . $hour . "', " . $inTraffic . " , " . $outTraffic . ", " . $totalTraffic . "],\n");
}
?>
]);
let options = {
title: 'Monthly Network Traffic',
subtitle: 'over last 12 months',
vAxis: {format: '##.## <?php echo $monthlyLargestPrefix; ?>'}
};
let chart = new google.charts.Bar(document.getElementById('monthlyNetworkTrafficGraph'));
chart.draw(data, google.charts.Bar.convertOptions(options));
}
</script>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Network Traffic (<?php echo $interface_name[$thisInterface]; ?>)</h1> <?php printOptions(); ?>
</div>
</div>
<div id="graphTabNav" class="container">
<ul class="nav nav-tabs">
<li class="active"><a href="#hourlyGraph" data-toggle="tab">Hourly Graph</a></li>
<li><a href="#dailyGraph" data-toggle="tab">Daily Graph</a></li>
<li><a href="#monthlyGraph" data-toggle="tab">Monthly Graph</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="hourlyGraph">
<div id="hourlyNetworkTrafficGraph" style="height: 300px;"></div>
</div>
<div class="tab-pane" id="dailyGraph">
<div id="dailyNetworkTrafficGraph" style="height: 300px;"></div>
</div>
<div class="tab-pane" id="monthlyGraph">
<div id="monthlyNetworkTrafficGraph" style="height: 300px;"></div>
</div>
</div>
</div>
<div id="tabNav" class="container">
<ul class="nav nav-tabs">
<li class="active"><a href="#hourly" data-toggle="tab">Hourly</a></li>
<li><a href="#daily" data-toggle="tab">Daily</a></li>
<li><a href="#monthly" data-toggle="tab">Monthly</a></li>
<li><a href="#top10" data-toggle="tab">Top 10</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="hourly">
<?php printTableStats($vnstat_bin_dir, "hourly", $thisInterface, 'Hour') ?>
</div>
<div class="tab-pane" id="daily">
<?php printTableStats($vnstat_bin_dir, "daily", $thisInterface, 'Day') ?>
</div>
<div class="tab-pane" id="monthly">
<?php printTableStats($vnstat_bin_dir, "monthly", $thisInterface, 'Month') ?>
</div>
<div class="tab-pane" id="top10">
<?php printTableStats($vnstat_bin_dir, "top10", $thisInterface, 'Day') ?>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<span class="text-muted">Copyright (C) <?php echo date("Y"); ?> Alexander Marston -
<a href="https://github.com/alexandermarston/vnstat-dashboard">vnstat-dashboard</a></span>
</div>
</footer>
</body>
</html>