Project

General

Profile

Download (8.7 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-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('config.inc');
39
require_once('config.lib.inc');
40
require_once('guiconfig.inc');
41
require_once('ipsec.inc');
42

    
43

    
44
$pconfig = config_get_path('traffic_graphs');
45

    
46
// Get configured interface list
47
$ifdescrs = get_configured_interface_with_descr();
48
if (ipsec_enabled()) {
49
	$ifdescrs['enc0'] = gettext("IPsec");
50
}
51

    
52
foreach (array('server', 'client') as $mode) {
53
	foreach (config_get_path("openvpn/openvpn-{$mode}", []) as $id => $setting) {
54
		if (isset($setting['disable'])) {
55
			continue;
56
		}
57
		$ifdescrs['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] =
58
		    gettext("OpenVPN") . " " . $mode . ": " .
59
		    htmlspecialchars($setting['description']);
60
	}
61
}
62

    
63
$ifdescrs = array_merge($ifdescrs, interface_ipsec_vti_list_all());
64

    
65
if (!empty($_POST)) {
66
	// update view if settings are changed or saved
67
	$curif = $_POST['if'];
68
	$found = false;
69
	foreach ($ifdescrs as $descr => $ifdescr) {
70
		if ($descr == $curif) {
71
			$found = true;
72
			break;
73
		}
74
	}
75
	if ($found === false) {
76
		header("Location: status_graph.php");
77
		exit;
78
	}
79
	$cursort = $_POST['sort'];
80
	$curfilter = $_POST['filter'];
81
	$curhostipformat = $_POST['hostipformat'];
82
	$curbackgroundupdate = $_POST['backgroundupdate'];
83
	$curinvert = $_POST['invert'];
84
	$cursmoothing = $_POST['smoothfactor'];
85
	$curmode = $_POST['mode'];
86

    
87
	// Save data to config
88
	if (isset($_POST['save'])) {
89
		$pconfig = array();
90
		$pconfig["if"] = $curif;
91
		$pconfig["sort"] = $cursort;
92
		$pconfig["filter"] = $curfilter;
93
		$pconfig["hostipformat"] = $curhostipformat;
94
		$pconfig["backgroundupdate"] = $curbackgroundupdate;
95
		$pconfig["smoothfactor"] = $cursmoothing;
96
		$pconfig["invert"] = $curinvert;
97
		$pconfig["mode"] = $curmode;
98
		config_set_path("traffic_graphs", $pconfig);
99
		write_config("Traffic Graphs settings updated");
100
	}
101
} else {
102
	// default settings from config
103
	if (is_array($pconfig)) {
104
		$curif = $pconfig['if'];
105
		$cursort = $pconfig['sort'];
106
		$curfilter = $pconfig['filter'];
107
		$curhostipformat = $pconfig['hostipformat'];
108
		$curbackgroundupdate = $pconfig['backgroundupdate'];
109
		$cursmoothing = $pconfig['smoothfactor'];
110
		$curinvert = $pconfig['invert'];
111
		$curmode = $pconfig['mode'];;
112
	} else {
113
		// initialize when no config details are present
114
		if (empty($ifdescrs["wan"])) {
115
			/*
116
			 * Handle the case when WAN has been disabled. Use the
117
			 * first key in ifdescrs.
118
			 */
119
			reset($ifdescrs);
120
			$curif = key($ifdescrs);
121
		} else {
122
			$curif = "wan";
123
		}
124
		$cursort = "";
125
		$curfilter = "";
126
		$curhostipformat = "";
127
		$curbackgroundupdate = "";
128
		$cursmoothing = 0;
129
		$curinvert = "";
130
		$curmode = "";
131
	}
132
}
133

    
134
function iflist() {
135
	global $ifdescrs;
136

    
137
	$iflist = array();
138

    
139
	foreach ($ifdescrs as $ifn => $ifd) {
140
		$iflist[$ifn] = $ifd;
141
	}
142

    
143
	return($iflist);
144
}
145

    
146
$pgtitle = array(gettext("Status"), gettext("Traffic Graph"));
147

    
148
include("head.inc");
149

    
150
$form = new Form();
151
$form->addClass('auto-submit');
152

    
153
$section = new Form_Section('Graph Settings');
154

    
155
$group = new Form_Group('Traffic Graph');
156

    
157
$group->add(new Form_Select(
158
	'if',
159
	null,
160
	$curif,
161
	iflist()
162
))->setHelp('Interface');
163

    
164
$group->add(new Form_Select(
165
	'sort',
166
	null,
167
	$cursort,
168
	array (
169
		'in'	=> gettext('Bandwidth In'),
170
		'out'	=> gettext('Bandwidth Out')
171
	)
172
))->setHelp('Sort by');
173

    
174
$group->add(new Form_Select(
175
	'filter',
176
	null,
177
	$curfilter,
178
	array (
179
		'local'	=> gettext('Local'),
180
		'remote'=> gettext('Remote'),
181
		'all'	=> gettext('All')
182
	)
183
))->setHelp('Filter');
184

    
185
$group->add(new Form_Select(
186
	'hostipformat',
187
	null,
188
	$curhostipformat,
189
	array (
190
		''		=> gettext('IP Address'),
191
		'hostname'	=> gettext('Host Name'),
192
		'descr'		=> gettext('Description'),
193
		'fqdn'		=> gettext('FQDN')
194
	)
195
))->setHelp('Display');
196

    
197
$group->add(new Form_Select(
198
	'mode',
199
	null,
200
	$curmode,
201
	array (
202
		'rate'	=> gettext('rate (standard)'),
203
		'iftop'	=> gettext('iftop (experimental)')
204
	)
205
))->setHelp('Mode');
206

    
207
$section->add($group);
208

    
209
$group2 = new Form_Group('Controls');
210

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

    
222
$group2->add(new Form_Select(
223
	'invert',
224
	null,
225
	$curinvert,
226
	array (
227
		'true'	=> gettext('On'),
228
		'false'	=> gettext('Off'),
229
	)
230
))->setHelp('Invert in/out');
231

    
232
$group2->add(new Form_Input(
233
	'smoothfactor',
234
	null,
235
	'range',
236
	$cursmoothing,
237
	array (
238
		'min' => 0,
239
		'max' => 5,
240
		'step' => 1
241
		)
242

    
243
))->setHelp('Graph Smoothing');
244

    
245
$section->add($group2);
246

    
247
$form->add($section);
248
print $form;
249

    
250
$realif = get_real_interface($curif);
251
?>
252

    
253
<script src="/vendor/d3/d3.min.js?v=<?=filemtime('/usr/local/www/vendor/d3/d3.min.js')?>"></script>
254
<script src="/vendor/nvd3/nv.d3.js?v=<?=filemtime('/usr/local/www/vendor/nvd3/nv.d3.js')?>"></script>
255
<script src="/vendor/visibility/visibility-1.2.3.min.js?v=<?=filemtime('/usr/local/www/vendor/visibility/visibility-1.2.3.min.js')?>"></script>
256

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

    
259
<script type="text/javascript">
260

    
261

    
262
//<![CDATA[
263
events.push(function() {
264

    
265
	var InterfaceString = "<?=$curif?>";
266
	var RealInterfaceString = "<?=$realif?>";
267
	window.graph_backgroundupdate = $('#backgroundupdate').val() === "true";
268
	window.smoothing = $('#smoothfactor').val();
269
	window.interval = 1;
270
	window.invert = $('#invert').val() === "true";
271
	window.size = 8;
272
	window.interfaces = InterfaceString.split("|").filter(function(entry) { return entry.trim() != ''; });
273
	window.realinterfaces = RealInterfaceString.split("|").filter(function(entry) { return entry.trim() != ''; });
274

    
275
	graph_init();
276
	graph_visibilitycheck();
277

    
278
});
279
//]]>
280
</script>
281

    
282
<script src="/js/traffic-graphs.js?v=<?=filemtime('/usr/local/www/js/traffic-graphs.js')?>"></script>
283

    
284
<script type="text/javascript">
285
//<![CDATA[
286

    
287
var graph_interfacenames = <?php
288
	foreach ($ifdescrs as $ifname => $ifdescr) {
289
		$iflist[$ifname] = $ifdescr;
290
	}
291
	echo json_encode($iflist);
292
?>;
293
function updateBandwidth() {
294
	$.ajax(
295
		'/bandwidth_by_ip.php',
296
		{
297
			type: 'get',
298
			data: $(document.forms[0]).serialize(),
299
			success: function (data) {
300
				var hosts_split = data.split("|");
301

    
302
				$('#top10-hosts').empty();
303

    
304
				//parse top ten bandwidth abuser hosts
305
				for (var y=0; y<10; y++) {
306
					if ((y < hosts_split.length) && (hosts_split[y] != "") && (hosts_split[y] != "no info")) {
307
						hostinfo = hosts_split[y].split(";");
308

    
309
						$('#top10-hosts').append('<tr>'+
310
							'<td>'+ hostinfo[0] +'</td>'+
311
							'<td>'+ hostinfo[1] +' <?=gettext("Bits/sec");?></td>'+
312
							'<td>'+ hostinfo[2] +' <?=gettext("Bits/sec");?></td>'+
313
						'</tr>');
314
					}
315
				}
316
			},
317
	});
318
}
319

    
320
events.push(function() {
321
	$('form.auto-submit').on('change', function() {
322
		$(this).submit();
323
	});
324

    
325
	setInterval('updateBandwidth()', 3000);
326

    
327
	updateBandwidth();
328
});
329
//]]>
330
</script>
331
<?php
332

    
333
/* link the ipsec interface magically */
334
if (ipsec_enabled()) {
335
	$ifdescrs['enc0'] = gettext("IPsec");
336
}
337

    
338
?>
339
<div class="panel panel-default">
340
	<div class="panel-heading">
341
		<h2 class="panel-title"><?=gettext("Traffic Graph");?></h2>
342
	</div>
343
	<div class="panel-body">
344
		<div class="col-sm-6">
345
			<div id="traffic-chart-<?=$curif?>" class="d3-chart traffic-widget-chart">
346
				<svg></svg>
347
			</div>
348
		</div>
349
		<div class="col-sm-6">
350
			<table class="table table-striped table-condensed">
351
				<thead>
352
					<tr>
353
						<th><?=(($curhostipformat == "") ? gettext("Host IP") : gettext("Host Name or IP")); ?></th>
354
						<th><?=gettext("Bandwidth In"); ?></th>
355
						<th><?=gettext("Bandwidth Out"); ?></th>
356
					</tr>
357
				</thead>
358
				<tbody id="top10-hosts">
359
					<!-- to be added by javascript -->
360
				</tbody>
361
			</table>
362
		</div>
363
	</div>
364
</div>
365
<?php include("foot.inc");
(162-162/228)