Project

General

Profile

Download (8.84 KB) Statistics
| Branch: | Tag: | Revision:
1 9573d170 Scott Ullrich
<?php
2 5b237745 Scott Ullrich
/*
3 c5d81585 Renato Botelho
 * status_graph.php
4 191cb31d Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * All rights reserved.
8 191cb31d Stephen Beaver
 *
9 c5d81585 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12 191cb31d Stephen Beaver
 *
13 0f3f6cc9 Jared Dillard
 * 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 191cb31d Stephen Beaver
 *
17 0f3f6cc9 Jared Dillard
 * http://www.apache.org/licenses/LICENSE-2.0
18 191cb31d Stephen Beaver
 *
19 0f3f6cc9 Jared Dillard
 * 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 191cb31d Stephen Beaver
 */
25 5b237745 Scott Ullrich
26 6b07c15a Matthew Grooms
##|+PRIV
27
##|*IDENT=page-status-trafficgraph
28 5230f468 jim-p
##|*NAME=Status: Traffic Graph
29 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Status: Traffic Graph' page.
30
##|*MATCH=status_graph.php*
31 8e95a671 jim-p
##|*MATCH=bandwidth_by_ip.php*
32 8a2f80b2 jim-p
##|*MATCH=graph.php*
33
##|*MATCH=ifstats.php*
34 6b07c15a Matthew Grooms
##|-PRIV
35
36 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
37 179ab6b3 Luiz Otavio O Souza
require_once("ipsec.inc");
38 5b237745 Scott Ullrich
39 94556105 Scott Ullrich
// Get configured interface list
40 61ab4cd3 Scott Ullrich
$ifdescrs = get_configured_interface_with_descr();
41 abe98adb Phil Davis
if (ipsec_enabled()) {
42 98128ad6 Phil Davis
	$ifdescrs['enc0'] = gettext("IPsec");
43 abe98adb Phil Davis
}
44
45 9f5d14ce jim-p
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 98128ad6 Phil Davis
				$ifdescrs['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " " . $mode . ": ".htmlspecialchars($setting['description']);
50 9f5d14ce jim-p
			}
51
		}
52
	}
53
}
54 43a0ac8a Scott Ullrich
55 709309eb Luca Zorzi
// 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 e31aa678 sbeaver
if ($_POST['if']) {
71
	$curif = $_POST['if'];
72 50b2f6ab Scott Ullrich
	$found = false;
73 6c07db48 Phil Davis
	foreach ($ifdescrs as $descr => $ifdescr) {
74 b406c78a Ermal
		if ($descr == $curif) {
75
			$found = true;
76
			break;
77
		}
78
	}
79
	if ($found === false) {
80 6f3d2063 Renato Botelho
		header("Location: status_graph.php");
81 50b2f6ab Scott Ullrich
		exit;
82
	}
83
} else {
84 76165eac Phil Davis
	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 42b0c921 Phil Davis
	} else {
89 76165eac Phil Davis
		$curif = "wan";
90
	}
91 50b2f6ab Scott Ullrich
}
92 e31aa678 sbeaver
if ($_POST['sort']) {
93
	$cursort = $_POST['sort'];
94 893fb622 Michele Di Maria
} else {
95
	$cursort = "";
96
}
97 e31aa678 sbeaver
if ($_POST['filter']) {
98
	$curfilter = $_POST['filter'];
99 da11e022 Phil Davis
} else {
100
	$curfilter = "";
101
}
102 e31aa678 sbeaver
if ($_POST['hostipformat']) {
103
	$curhostipformat = $_POST['hostipformat'];
104 4006a437 Phil Davis
} else {
105
	$curhostipformat = "";
106
}
107 fe5c31bb PiBa-NL
if ($_POST['backgroundupdate']) {
108
	$curbackgroundupdate = $_POST['backgroundupdate'];
109
} else {
110
	$curbackgroundupdate = "";
111
}
112 769cdf3b Bill Marquette
113 e31aa678 sbeaver
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 abe98adb Phil Davis
$pgtitle = array(gettext("Status"), gettext("Traffic Graph"));
126 f0a3b883 Scott Ullrich
127 4df96eff Scott Ullrich
include("head.inc");
128
129 e31aa678 sbeaver
$form = new Form(false);
130 a4af095c Renato Botelho
$form->addClass('auto-submit');
131 e31aa678 sbeaver
132 5f88f964 k-paulius
$section = new Form_Section('Graph Settings');
133 5b237745 Scott Ullrich
134 a4af095c Renato Botelho
$group = new Form_Group('');
135
136
$group->add(new Form_Select(
137 e31aa678 sbeaver
	'if',
138 a4af095c Renato Botelho
	null,
139 e31aa678 sbeaver
	$curif,
140
	iflist()
141 a4af095c Renato Botelho
))->setHelp('Interface');
142 e31aa678 sbeaver
143 a4af095c Renato Botelho
$group->add(new Form_Select(
144 e31aa678 sbeaver
	'sort',
145 a4af095c Renato Botelho
	null,
146 e31aa678 sbeaver
	$cursort,
147
	array (
148 b50d30c3 Stephen Beaver
		'in'	=> gettext('Bandwidth In'),
149
		'out'	=> gettext('Bandwidth Out')
150 e31aa678 sbeaver
	)
151 a4af095c Renato Botelho
))->setHelp('Sort by');
152 e31aa678 sbeaver
153 a4af095c Renato Botelho
$group->add(new Form_Select(
154 e31aa678 sbeaver
	'filter',
155 a4af095c Renato Botelho
	null,
156 e31aa678 sbeaver
	$curfilter,
157
	array (
158 b50d30c3 Stephen Beaver
		'local'	=> gettext('Local'),
159
		'remote'=> gettext('Remote'),
160
		'all'	=> gettext('All')
161 e31aa678 sbeaver
	)
162 a4af095c Renato Botelho
))->setHelp('Filter');
163 e31aa678 sbeaver
164 a4af095c Renato Botelho
$group->add(new Form_Select(
165 e31aa678 sbeaver
	'hostipformat',
166 a4af095c Renato Botelho
	null,
167 e31aa678 sbeaver
	$curhostipformat,
168
	array (
169 b50d30c3 Stephen Beaver
		''			=> gettext('IP Address'),
170
		'hostname'	=> gettext('Host Name'),
171
		'descr'		=> gettext('Description'),
172
		'fqdn'		=> gettext('FQDN')
173 e31aa678 sbeaver
	)
174 a4af095c Renato Botelho
))->setHelp('Display');
175
176 fe5c31bb PiBa-NL
$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 a4af095c Renato Botelho
$section->add($group);
187 e31aa678 sbeaver
188
$form->add($section);
189
print $form;
190
191
?>
192 849d3a37 Jared Dillard
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 fe5c31bb PiBa-NL
	window.interfaces = InterfaceString.split("|");
212 849d3a37 Jared Dillard
	window.charts = {};
213
    window.myData = {};
214
    window.updateIds = 0;
215 fe5c31bb PiBa-NL
    window.updateTimerIds = 0;
216 849d3a37 Jared Dillard
    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 fe5c31bb PiBa-NL
	$.each( window.interfaces, function( key, value ) {
225 849d3a37 Jared Dillard
226
		myData[value] = [];
227
		updateIds = 0;
228 fe5c31bb PiBa-NL
		updateTimerIds = 0;
229 849d3a37 Jared Dillard
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 fe5c31bb PiBa-NL
    var backgroundupdate = $('#backgroundupdate').val() === "true";
248
	draw_graph(refreshInterval, then, backgroundupdate);
249 849d3a37 Jared Dillard
250
	//re-draw graph when the page goes from inactive (in it's window) to active
251
	Visibility.change(function (e, state) {
252 fe5c31bb PiBa-NL
		if($('#backgroundupdate').val() === "true"){
253
			return;
254
		}
255 849d3a37 Jared Dillard
		if(state === "visible") {
256
257
			now = then = new Date(Date.now());
258
259
			var nowTime = now.getTime();
260
261 fe5c31bb PiBa-NL
			$.each( window.interfaces, function( key, value ) {
262 849d3a37 Jared Dillard
263
				Visibility.stop(updateIds);
264 fe5c31bb PiBa-NL
				clearInterval(updateTimerIds);
265 849d3a37 Jared Dillard
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 fe5c31bb PiBa-NL
			draw_graph(refreshInterval, then, false);
286 849d3a37 Jared Dillard
287
		}
288
	});
289
290
});
291
//]]>
292
</script>
293
294
<script src="/js/traffic-graphs.js"></script>
295
296 8fd9052f Colin Fleming
<script type="text/javascript">
297
//<![CDATA[
298 f0a3b883 Scott Ullrich
299 fe5c31bb PiBa-NL
var graph_interfacenames = <?php
300
	foreach ($ifdescrs as $ifname => $ifdescr) {
301
		$iflist[$ifname] = $ifdescr;
302
	}
303
	echo json_encode($iflist);
304
?>;
305 abe98adb Phil Davis
function updateBandwidth() {
306 607b1c39 Sjon Hortensius
	$.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 abe98adb Phil Davis
				for (var y=0; y<10; y++) {
318 607b1c39 Sjon Hortensius
					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 3bd74348 bruno
							'<td>'+ hostinfo[1] +' <?=gettext("Bits/sec");?></td>'+
324
							'<td>'+ hostinfo[2] +' <?=gettext("Bits/sec");?></td>'+
325 607b1c39 Sjon Hortensius
						'</tr>');
326
					}
327
				}
328
			},
329
	});
330 f0a3b883 Scott Ullrich
}
331
332 abe98adb Phil Davis
events.push(function() {
333
	$('form.auto-submit').on('change', function() {
334 607b1c39 Sjon Hortensius
		$(this).submit();
335
	});
336 f0a3b883 Scott Ullrich
337 889f9ee0 heper
	setInterval('updateBandwidth()', 3000);
338 e31aa678 sbeaver
339 607b1c39 Sjon Hortensius
	updateBandwidth();
340
});
341 8fd9052f Colin Fleming
//]]>
342 f0a3b883 Scott Ullrich
</script>
343 5b237745 Scott Ullrich
<?php
344 9573d170 Scott Ullrich
345 872ce0dd Ermal Luçi
/* link the ipsec interface magically */
346 abe98adb Phil Davis
if (ipsec_enabled()) {
347 98128ad6 Phil Davis
	$ifdescrs['enc0'] = gettext("IPsec");
348 abe98adb Phil Davis
}
349 c1abd446 Seth Mos
350 5b237745 Scott Ullrich
?>
351 607b1c39 Sjon Hortensius
<div class="panel panel-default">
352
	<div class="panel-heading">
353 3d7a8696 k-paulius
		<h2 class="panel-title"><?=gettext("Traffic Graph");?></h2>
354 e31aa678 sbeaver
	</div>
355 607b1c39 Sjon Hortensius
	<div class="panel-body">
356
		<div class="col-sm-6">
357 849d3a37 Jared Dillard
			<div id="traffic-chart-<?=$curif?>" class="d3-chart traffic-widget-chart">
358
				<svg></svg>
359
			</div>
360 607b1c39 Sjon Hortensius
		</div>
361
		<div class="col-sm-6">
362
			<table class="table table-striped table-condensed">
363
				<thead>
364
					<tr>
365 abe98adb Phil Davis
						<th><?=(($curhostipformat == "") ? gettext("Host IP") : gettext("Host Name or IP")); ?></th>
366 607b1c39 Sjon Hortensius
						<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 e57c91a2 Renato Botelho
	</div>
376 8ab3e9ed Erik Kristensen
</div>
377 c10cb196 Stephen Beaver
<?php include("foot.inc");