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