Project

General

Profile

Download (10.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * status_graph.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2018 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
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *
16
 * 1. Redistributions of source code must retain the above copyright notice,
17
 *    this list of conditions and the following disclaimer.
18
 *
19
 * 2. Redistributions in binary form must reproduce the above copyright
20
 *    notice, this list of conditions and the following disclaimer in
21
 *    the documentation and/or other materials provided with the
22
 *    distribution.
23
 *
24
 * 3. All advertising materials mentioning features or use of this software
25
 *    must display the following acknowledgment:
26
 *    "This product includes software developed by the pfSense Project
27
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
28
 *
29
 * 4. The names "pfSense" and "pfSense Project" must not be used to
30
 *    endorse or promote products derived from this software without
31
 *    prior written permission. For written permission, please contact
32
 *    coreteam@pfsense.org.
33
 *
34
 * 5. Products derived from this software may not be called "pfSense"
35
 *    nor may "pfSense" appear in their names without prior written
36
 *    permission of the Electric Sheep Fencing, LLC.
37
 *
38
 * 6. Redistributions of any form whatsoever must retain the following
39
 *    acknowledgment:
40
 *
41
 * "This product includes software developed by the pfSense Project
42
 * for use in the pfSense software distribution (http://www.pfsense.org/).
43
 *
44
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
45
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
48
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 * OF THE POSSIBILITY OF SUCH DAMAGE.
56
 */
57

    
58
##|+PRIV
59
##|*IDENT=page-status-trafficgraph
60
##|*NAME=Status: Traffic Graph
61
##|*DESCR=Allow access to the 'Status: Traffic Graph' page.
62
##|*MATCH=status_graph.php*
63
##|*MATCH=bandwidth_by_ip.php*
64
##|*MATCH=graph.php*
65
##|*MATCH=ifstats.php*
66
##|-PRIV
67

    
68
require_once("guiconfig.inc");
69
require_once("ipsec.inc");
70

    
71
// Get configured interface list
72
$ifdescrs = get_configured_interface_with_descr();
73
if (ipsec_enabled()) {
74
	$ifdescrs['enc0'] = gettext("IPsec");
75
}
76

    
77
foreach (array('server', 'client') as $mode) {
78
	if (is_array($config['openvpn']["openvpn-{$mode}"])) {
79
		foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
80
			if (!isset($setting['disable'])) {
81
				$ifdescrs['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " " . $mode . ": ".htmlspecialchars($setting['description']);
82
			}
83
		}
84
	}
85
}
86

    
87
// Compatiblity to restore GET parameters used pre-2.3
88
// Useful to save a URL for a given graph configuration
89
if (isset($_GET['if']) && !isset($_POST['if'])) {
90
	$_POST['if'] = $_GET['if'];
91
}
92
if (isset($_GET['sort']) && !isset($_POST['sort'])) {
93
	$_POST['sort'] = $_GET['sort'];
94
}
95
if (isset($_GET['filter']) && !isset($_POST['filter'])) {
96
	$_POST['filter'] = $_GET['filter'];
97
}
98
if (isset($_GET['hostipformat']) && !isset($_POST['hostipformat'])) {
99
	$_POST['hostipformat'] = $_GET['hostipformat'];
100
}
101

    
102
if ($_POST['if']) {
103
	$curif = $_POST['if'];
104
	$found = false;
105
	foreach ($ifdescrs as $descr => $ifdescr) {
106
		if ($descr == $curif) {
107
			$found = true;
108
			break;
109
		}
110
	}
111
	if ($found === false) {
112
		header("Location: status_graph.php");
113
		exit;
114
	}
115
} else {
116
	if (empty($ifdescrs["wan"])) {
117
		/* Handle the case when WAN has been disabled. Use the first key in ifdescrs. */
118
		reset($ifdescrs);
119
		$curif = key($ifdescrs);
120
	} else {
121
		$curif = "wan";
122
	}
123
}
124
if ($_POST['sort']) {
125
	$cursort = $_POST['sort'];
126
} else {
127
	$cursort = "";
128
}
129
if ($_POST['filter']) {
130
	$curfilter = $_POST['filter'];
131
} else {
132
	$curfilter = "";
133
}
134
if ($_POST['hostipformat']) {
135
	$curhostipformat = $_POST['hostipformat'];
136
} else {
137
	$curhostipformat = "";
138
}
139
if ($_POST['backgroundupdate']) {
140
	$curbackgroundupdate = $_POST['backgroundupdate'];
141
} else {
142
	$curbackgroundupdate = "";
143
}
144

    
145
function iflist() {
146
	global $ifdescrs;
147

    
148
	$iflist = array();
149

    
150
	foreach ($ifdescrs as $ifn => $ifd) {
151
		$iflist[$ifn] = $ifd;
152
	}
153

    
154
	return($iflist);
155
}
156

    
157
$pgtitle = array(gettext("Status"), gettext("Traffic Graph"));
158

    
159
include("head.inc");
160

    
161
$form = new Form(false);
162
$form->addClass('auto-submit');
163

    
164
$section = new Form_Section('Graph Settings');
165

    
166
$group = new Form_Group('');
167

    
168
$group->add(new Form_Select(
169
	'if',
170
	null,
171
	$curif,
172
	iflist()
173
))->setHelp('Interface');
174

    
175
$group->add(new Form_Select(
176
	'sort',
177
	null,
178
	$cursort,
179
	array (
180
		'in'	=> gettext('Bandwidth In'),
181
		'out'	=> gettext('Bandwidth Out')
182
	)
183
))->setHelp('Sort by');
184

    
185
$group->add(new Form_Select(
186
	'filter',
187
	null,
188
	$curfilter,
189
	array (
190
		'local'	=> gettext('Local'),
191
		'remote'=> gettext('Remote'),
192
		'all'	=> gettext('All')
193
	)
194
))->setHelp('Filter');
195

    
196
$group->add(new Form_Select(
197
	'hostipformat',
198
	null,
199
	$curhostipformat,
200
	array (
201
		''			=> gettext('IP Address'),
202
		'hostname'	=> gettext('Host Name'),
203
		'descr'		=> gettext('Description'),
204
		'fqdn'		=> gettext('FQDN')
205
	)
206
))->setHelp('Display');
207

    
208
$group->add(new Form_Select(
209
	'backgroundupdate',
210
	null,
211
	$curbackgroundupdate,
212
	array (
213
		'false'	=> gettext('Clear graphs when not visible.'),
214
		'true'	=> gettext('Keep graphs updated on inactive tab. (increases cpu usage)'),
215
	)
216
))->setHelp('Background updates');
217

    
218
$section->add($group);
219

    
220
$form->add($section);
221
print $form;
222

    
223
?>
224

    
225
<script src="/vendor/d3/d3.min.js"></script>
226
<script src="/vendor/nvd3/nv.d3.js"></script>
227
<script src="/vendor/visibility/visibility-1.2.3.min.js"></script>
228

    
229
<link href="/vendor/nvd3/nv.d3.css" media="screen, projection" rel="stylesheet" type="text/css">
230

    
231
<script type="text/javascript">
232

    
233
//<![CDATA[
234
events.push(function() {
235

    
236
	var InterfaceString = "<?=$curif?>";
237

    
238
	//store saved settings in a fresh localstorage
239
	localStorage.clear();
240
	localStorage.setItem('interval', 1);
241
	localStorage.setItem('invert', "true");
242
	localStorage.setItem('size', 1);
243
	window.interfaces = InterfaceString.split("|");
244
	window.charts = {};
245
    window.myData = {};
246
    window.updateIds = 0;
247
    window.updateTimerIds = 0;
248
    window.latest = [];
249
    var refreshInterval = localStorage.getItem('interval');
250

    
251
    //TODO make it fall on a second value so it increments better
252
    var now = then = new Date(Date.now());
253

    
254
    var nowTime = now.getTime();
255

    
256
	$.each( window.interfaces, function( key, value ) {
257

    
258
		myData[value] = [];
259
		updateIds = 0;
260
		updateTimerIds = 0;
261

    
262
		var itemIn = new Object();
263
		var itemOut = new Object();
264

    
265
		itemIn.key = value + " (in)";
266
		if(localStorage.getItem('invert') === "true") { itemIn.area = true; }
267
		itemIn.first = true;
268
		itemIn.values = [{x: nowTime, y: 0}];
269
		myData[value].push(itemIn);
270

    
271
		itemOut.key = value + " (out)";
272
		if(localStorage.getItem('invert') === "true") { itemOut.area = true; }
273
		itemOut.first = true;
274
		itemOut.values = [{x: nowTime, y: 0}];
275
		myData[value].push(itemOut);
276

    
277
	});
278

    
279
    var backgroundupdate = $('#backgroundupdate').val() === "true";
280
	draw_graph(refreshInterval, then, backgroundupdate);
281

    
282
	//re-draw graph when the page goes from inactive (in it's window) to active
283
	Visibility.change(function (e, state) {
284
		if($('#backgroundupdate').val() === "true"){
285
			return;
286
		}
287
		if(state === "visible") {
288

    
289
			now = then = new Date(Date.now());
290

    
291
			var nowTime = now.getTime();
292

    
293
			$.each( window.interfaces, function( key, value ) {
294

    
295
				Visibility.stop(updateIds);
296
				clearInterval(updateTimerIds);
297

    
298
				myData[value] = [];
299

    
300
				var itemIn = new Object();
301
				var itemOut = new Object();
302

    
303
				itemIn.key = value + " (in)";
304
				if(localStorage.getItem('invert') === "true") { itemIn.area = true; }
305
				itemIn.first = true;
306
				itemIn.values = [{x: nowTime, y: 0}];
307
				myData[value].push(itemIn);
308

    
309
				itemOut.key = value + " (out)";
310
				if(localStorage.getItem('invert') === "true") { itemOut.area = true; }
311
				itemOut.first = true;
312
				itemOut.values = [{x: nowTime, y: 0}];
313
				myData[value].push(itemOut);
314

    
315
			});
316

    
317
			draw_graph(refreshInterval, then, false);
318

    
319
		}
320
	});
321

    
322
});
323
//]]>
324
</script>
325

    
326
<script src="/js/traffic-graphs.js"></script>
327

    
328
<script type="text/javascript">
329
//<![CDATA[
330

    
331
var graph_interfacenames = <?php
332
	foreach ($ifdescrs as $ifname => $ifdescr) {
333
		$iflist[$ifname] = $ifdescr;
334
	}
335
	echo json_encode($iflist);
336
?>;
337
function updateBandwidth() {
338
	$.ajax(
339
		'/bandwidth_by_ip.php',
340
		{
341
			type: 'get',
342
			data: $(document.forms[0]).serialize(),
343
			success: function (data) {
344
				var hosts_split = data.split("|");
345

    
346
				$('#top10-hosts').empty();
347

    
348
				//parse top ten bandwidth abuser hosts
349
				for (var y=0; y<10; y++) {
350
					if ((y < hosts_split.length) && (hosts_split[y] != "") && (hosts_split[y] != "no info")) {
351
						hostinfo = hosts_split[y].split(";");
352

    
353
						$('#top10-hosts').append('<tr>'+
354
							'<td>'+ hostinfo[0] +'</td>'+
355
							'<td>'+ hostinfo[1] +' <?=gettext("Bits/sec");?></td>'+
356
							'<td>'+ hostinfo[2] +' <?=gettext("Bits/sec");?></td>'+
357
						'</tr>');
358
					}
359
				}
360
			},
361
	});
362
}
363

    
364
events.push(function() {
365
	$('form.auto-submit').on('change', function() {
366
		$(this).submit();
367
	});
368

    
369
	setInterval('updateBandwidth()', 3000);
370

    
371
	updateBandwidth();
372
});
373
//]]>
374
</script>
375
<?php
376

    
377
/* link the ipsec interface magically */
378
if (ipsec_enabled()) {
379
	$ifdescrs['enc0'] = gettext("IPsec");
380
}
381

    
382
?>
383
<div class="panel panel-default">
384
	<div class="panel-heading">
385
		<h2 class="panel-title"><?=gettext("Traffic Graph");?></h2>
386
	</div>
387
	<div class="panel-body">
388
		<div class="col-sm-6">
389
			<div id="traffic-chart-<?=$curif?>" class="d3-chart traffic-widget-chart">
390
				<svg></svg>
391
			</div>
392
		</div>
393
		<div class="col-sm-6">
394
			<table class="table table-striped table-condensed">
395
				<thead>
396
					<tr>
397
						<th><?=(($curhostipformat == "") ? gettext("Host IP") : gettext("Host Name or IP")); ?></th>
398
						<th><?=gettext("Bandwidth In"); ?></th>
399
						<th><?=gettext("Bandwidth Out"); ?></th>
400
					</tr>
401
				</thead>
402
				<tbody id="top10-hosts">
403
					<!-- to be added by javascript -->
404
				</tbody>
405
			</table>
406
		</div>
407
	</div>
408
</div>
409
<?php include("foot.inc");
(166-166/230)