1
|
<?php
|
2
|
/*
|
3
|
* status_graph.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
|
7
|
* All rights reserved.
|
8
|
*
|
9
|
* originally based on m0n0wall (http://m0n0.ch/wall)
|
10
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
11
|
* All rights reserved.
|
12
|
*
|
13
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
14
|
* you may not use this file except in compliance with the License.
|
15
|
* You may obtain a copy of the License at
|
16
|
*
|
17
|
* http://www.apache.org/licenses/LICENSE-2.0
|
18
|
*
|
19
|
* Unless required by applicable law or agreed to in writing, software
|
20
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
21
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
22
|
* See the License for the specific language governing permissions and
|
23
|
* limitations under the License.
|
24
|
*/
|
25
|
|
26
|
##|+PRIV
|
27
|
##|*IDENT=page-status-trafficgraph
|
28
|
##|*NAME=Status: Traffic Graph
|
29
|
##|*DESCR=Allow access to the 'Status: Traffic Graph' page.
|
30
|
##|*MATCH=status_graph.php*
|
31
|
##|*MATCH=bandwidth_by_ip.php*
|
32
|
##|*MATCH=graph.php*
|
33
|
##|*MATCH=ifstats.php*
|
34
|
##|-PRIV
|
35
|
|
36
|
require_once("guiconfig.inc");
|
37
|
require_once("ipsec.inc");
|
38
|
|
39
|
// Get configured interface list
|
40
|
$ifdescrs = get_configured_interface_with_descr();
|
41
|
if (ipsec_enabled()) {
|
42
|
$ifdescrs['enc0'] = gettext("IPsec");
|
43
|
}
|
44
|
|
45
|
foreach (array('server', 'client') as $mode) {
|
46
|
if (is_array($config['openvpn']["openvpn-{$mode}"])) {
|
47
|
foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
|
48
|
if (!isset($setting['disable'])) {
|
49
|
$ifdescrs['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " " . $mode . ": ".htmlspecialchars($setting['description']);
|
50
|
}
|
51
|
}
|
52
|
}
|
53
|
}
|
54
|
|
55
|
// Compatiblity to restore GET parameters used pre-2.3
|
56
|
// Useful to save a URL for a given graph configuration
|
57
|
if (isset($_GET['if']) && !isset($_POST['if'])) {
|
58
|
$_POST['if'] = $_GET['if'];
|
59
|
}
|
60
|
if (isset($_GET['sort']) && !isset($_POST['sort'])) {
|
61
|
$_POST['sort'] = $_GET['sort'];
|
62
|
}
|
63
|
if (isset($_GET['filter']) && !isset($_POST['filter'])) {
|
64
|
$_POST['filter'] = $_GET['filter'];
|
65
|
}
|
66
|
if (isset($_GET['hostipformat']) && !isset($_POST['hostipformat'])) {
|
67
|
$_POST['hostipformat'] = $_GET['hostipformat'];
|
68
|
}
|
69
|
|
70
|
if ($_POST['if']) {
|
71
|
$curif = $_POST['if'];
|
72
|
$found = false;
|
73
|
foreach ($ifdescrs as $descr => $ifdescr) {
|
74
|
if ($descr == $curif) {
|
75
|
$found = true;
|
76
|
break;
|
77
|
}
|
78
|
}
|
79
|
if ($found === false) {
|
80
|
header("Location: status_graph.php");
|
81
|
exit;
|
82
|
}
|
83
|
} else {
|
84
|
if (empty($ifdescrs["wan"])) {
|
85
|
/* Handle the case when WAN has been disabled. Use the first key in ifdescrs. */
|
86
|
reset($ifdescrs);
|
87
|
$curif = key($ifdescrs);
|
88
|
} else {
|
89
|
$curif = "wan";
|
90
|
}
|
91
|
}
|
92
|
if ($_POST['sort']) {
|
93
|
$cursort = $_POST['sort'];
|
94
|
} else {
|
95
|
$cursort = "";
|
96
|
}
|
97
|
if ($_POST['filter']) {
|
98
|
$curfilter = $_POST['filter'];
|
99
|
} else {
|
100
|
$curfilter = "";
|
101
|
}
|
102
|
if ($_POST['hostipformat']) {
|
103
|
$curhostipformat = $_POST['hostipformat'];
|
104
|
} else {
|
105
|
$curhostipformat = "";
|
106
|
}
|
107
|
if ($_POST['backgroundupdate']) {
|
108
|
$curbackgroundupdate = $_POST['backgroundupdate'];
|
109
|
} else {
|
110
|
$curbackgroundupdate = "";
|
111
|
}
|
112
|
|
113
|
function iflist() {
|
114
|
global $ifdescrs;
|
115
|
|
116
|
$iflist = array();
|
117
|
|
118
|
foreach ($ifdescrs as $ifn => $ifd) {
|
119
|
$iflist[$ifn] = $ifd;
|
120
|
}
|
121
|
|
122
|
return($iflist);
|
123
|
}
|
124
|
|
125
|
$pgtitle = array(gettext("Status"), gettext("Traffic Graph"));
|
126
|
|
127
|
include("head.inc");
|
128
|
|
129
|
$form = new Form(false);
|
130
|
$form->addClass('auto-submit');
|
131
|
|
132
|
$section = new Form_Section('Graph Settings');
|
133
|
|
134
|
$group = new Form_Group('');
|
135
|
|
136
|
$group->add(new Form_Select(
|
137
|
'if',
|
138
|
null,
|
139
|
$curif,
|
140
|
iflist()
|
141
|
))->setHelp('Interface');
|
142
|
|
143
|
$group->add(new Form_Select(
|
144
|
'sort',
|
145
|
null,
|
146
|
$cursort,
|
147
|
array (
|
148
|
'in' => gettext('Bandwidth In'),
|
149
|
'out' => gettext('Bandwidth Out')
|
150
|
)
|
151
|
))->setHelp('Sort by');
|
152
|
|
153
|
$group->add(new Form_Select(
|
154
|
'filter',
|
155
|
null,
|
156
|
$curfilter,
|
157
|
array (
|
158
|
'local' => gettext('Local'),
|
159
|
'remote'=> gettext('Remote'),
|
160
|
'all' => gettext('All')
|
161
|
)
|
162
|
))->setHelp('Filter');
|
163
|
|
164
|
$group->add(new Form_Select(
|
165
|
'hostipformat',
|
166
|
null,
|
167
|
$curhostipformat,
|
168
|
array (
|
169
|
'' => gettext('IP Address'),
|
170
|
'hostname' => gettext('Host Name'),
|
171
|
'descr' => gettext('Description'),
|
172
|
'fqdn' => gettext('FQDN')
|
173
|
)
|
174
|
))->setHelp('Display');
|
175
|
|
176
|
$group->add(new Form_Select(
|
177
|
'backgroundupdate',
|
178
|
null,
|
179
|
$curbackgroundupdate,
|
180
|
array (
|
181
|
'false' => gettext('Clear graphs when not visible.'),
|
182
|
'true' => gettext('Keep graphs updated on inactive tab. (increases cpu usage)'),
|
183
|
)
|
184
|
))->setHelp('Background updates');
|
185
|
|
186
|
$section->add($group);
|
187
|
|
188
|
$form->add($section);
|
189
|
print $form;
|
190
|
|
191
|
?>
|
192
|
|
193
|
<script src="/vendor/d3/d3.min.js"></script>
|
194
|
<script src="/vendor/nvd3/nv.d3.js"></script>
|
195
|
<script src="/vendor/visibility/visibility-1.2.3.min.js"></script>
|
196
|
|
197
|
<link href="/vendor/nvd3/nv.d3.css" media="screen, projection" rel="stylesheet" type="text/css">
|
198
|
|
199
|
<script type="text/javascript">
|
200
|
|
201
|
//<![CDATA[
|
202
|
events.push(function() {
|
203
|
|
204
|
var InterfaceString = "<?=$curif?>";
|
205
|
|
206
|
//store saved settings in a fresh localstorage
|
207
|
localStorage.clear();
|
208
|
localStorage.setItem('interval', 1);
|
209
|
localStorage.setItem('invert', "true");
|
210
|
localStorage.setItem('size', 1);
|
211
|
window.interfaces = InterfaceString.split("|");
|
212
|
window.charts = {};
|
213
|
window.myData = {};
|
214
|
window.updateIds = 0;
|
215
|
window.updateTimerIds = 0;
|
216
|
window.latest = [];
|
217
|
var refreshInterval = localStorage.getItem('interval');
|
218
|
|
219
|
//TODO make it fall on a second value so it increments better
|
220
|
var now = then = new Date(Date.now());
|
221
|
|
222
|
var nowTime = now.getTime();
|
223
|
|
224
|
$.each( window.interfaces, function( key, value ) {
|
225
|
|
226
|
myData[value] = [];
|
227
|
updateIds = 0;
|
228
|
updateTimerIds = 0;
|
229
|
|
230
|
var itemIn = new Object();
|
231
|
var itemOut = new Object();
|
232
|
|
233
|
itemIn.key = value + " (in)";
|
234
|
if(localStorage.getItem('invert') === "true") { itemIn.area = true; }
|
235
|
itemIn.first = true;
|
236
|
itemIn.values = [{x: nowTime, y: 0}];
|
237
|
myData[value].push(itemIn);
|
238
|
|
239
|
itemOut.key = value + " (out)";
|
240
|
if(localStorage.getItem('invert') === "true") { itemOut.area = true; }
|
241
|
itemOut.first = true;
|
242
|
itemOut.values = [{x: nowTime, y: 0}];
|
243
|
myData[value].push(itemOut);
|
244
|
|
245
|
});
|
246
|
|
247
|
var backgroundupdate = $('#backgroundupdate').val() === "true";
|
248
|
draw_graph(refreshInterval, then, backgroundupdate);
|
249
|
|
250
|
//re-draw graph when the page goes from inactive (in it's window) to active
|
251
|
Visibility.change(function (e, state) {
|
252
|
if($('#backgroundupdate').val() === "true"){
|
253
|
return;
|
254
|
}
|
255
|
if(state === "visible") {
|
256
|
|
257
|
now = then = new Date(Date.now());
|
258
|
|
259
|
var nowTime = now.getTime();
|
260
|
|
261
|
$.each( window.interfaces, function( key, value ) {
|
262
|
|
263
|
Visibility.stop(updateIds);
|
264
|
clearInterval(updateTimerIds);
|
265
|
|
266
|
myData[value] = [];
|
267
|
|
268
|
var itemIn = new Object();
|
269
|
var itemOut = new Object();
|
270
|
|
271
|
itemIn.key = value + " (in)";
|
272
|
if(localStorage.getItem('invert') === "true") { itemIn.area = true; }
|
273
|
itemIn.first = true;
|
274
|
itemIn.values = [{x: nowTime, y: 0}];
|
275
|
myData[value].push(itemIn);
|
276
|
|
277
|
itemOut.key = value + " (out)";
|
278
|
if(localStorage.getItem('invert') === "true") { itemOut.area = true; }
|
279
|
itemOut.first = true;
|
280
|
itemOut.values = [{x: nowTime, y: 0}];
|
281
|
myData[value].push(itemOut);
|
282
|
|
283
|
});
|
284
|
|
285
|
draw_graph(refreshInterval, then, false);
|
286
|
|
287
|
}
|
288
|
});
|
289
|
|
290
|
});
|
291
|
//]]>
|
292
|
</script>
|
293
|
|
294
|
<script src="/js/traffic-graphs.js"></script>
|
295
|
|
296
|
<script type="text/javascript">
|
297
|
//<![CDATA[
|
298
|
|
299
|
var graph_interfacenames = <?php
|
300
|
foreach ($ifdescrs as $ifname => $ifdescr) {
|
301
|
$iflist[$ifname] = $ifdescr;
|
302
|
}
|
303
|
echo json_encode($iflist);
|
304
|
?>;
|
305
|
function updateBandwidth() {
|
306
|
$.ajax(
|
307
|
'/bandwidth_by_ip.php',
|
308
|
{
|
309
|
type: 'get',
|
310
|
data: $(document.forms[0]).serialize(),
|
311
|
success: function (data) {
|
312
|
var hosts_split = data.split("|");
|
313
|
|
314
|
$('#top10-hosts').empty();
|
315
|
|
316
|
//parse top ten bandwidth abuser hosts
|
317
|
for (var y=0; y<10; y++) {
|
318
|
if ((y < hosts_split.length) && (hosts_split[y] != "") && (hosts_split[y] != "no info")) {
|
319
|
hostinfo = hosts_split[y].split(";");
|
320
|
|
321
|
$('#top10-hosts').append('<tr>'+
|
322
|
'<td>'+ hostinfo[0] +'</td>'+
|
323
|
'<td>'+ hostinfo[1] +' <?=gettext("Bits/sec");?></td>'+
|
324
|
'<td>'+ hostinfo[2] +' <?=gettext("Bits/sec");?></td>'+
|
325
|
'</tr>');
|
326
|
}
|
327
|
}
|
328
|
},
|
329
|
});
|
330
|
}
|
331
|
|
332
|
events.push(function() {
|
333
|
$('form.auto-submit').on('change', function() {
|
334
|
$(this).submit();
|
335
|
});
|
336
|
|
337
|
setInterval('updateBandwidth()', 3000);
|
338
|
|
339
|
updateBandwidth();
|
340
|
});
|
341
|
//]]>
|
342
|
</script>
|
343
|
<?php
|
344
|
|
345
|
/* link the ipsec interface magically */
|
346
|
if (ipsec_enabled()) {
|
347
|
$ifdescrs['enc0'] = gettext("IPsec");
|
348
|
}
|
349
|
|
350
|
?>
|
351
|
<div class="panel panel-default">
|
352
|
<div class="panel-heading">
|
353
|
<h2 class="panel-title"><?=gettext("Traffic Graph");?></h2>
|
354
|
</div>
|
355
|
<div class="panel-body">
|
356
|
<div class="col-sm-6">
|
357
|
<div id="traffic-chart-<?=$curif?>" class="d3-chart traffic-widget-chart">
|
358
|
<svg></svg>
|
359
|
</div>
|
360
|
</div>
|
361
|
<div class="col-sm-6">
|
362
|
<table class="table table-striped table-condensed">
|
363
|
<thead>
|
364
|
<tr>
|
365
|
<th><?=(($curhostipformat == "") ? gettext("Host IP") : gettext("Host Name or IP")); ?></th>
|
366
|
<th><?=gettext("Bandwidth In"); ?></th>
|
367
|
<th><?=gettext("Bandwidth Out"); ?></th>
|
368
|
</tr>
|
369
|
</thead>
|
370
|
<tbody id="top10-hosts">
|
371
|
<!-- to be added by javascript -->
|
372
|
</tbody>
|
373
|
</table>
|
374
|
</div>
|
375
|
</div>
|
376
|
</div>
|
377
|
<?php include("foot.inc");
|