Project

General

Profile

Download (8.75 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-2020 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
		foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
53
			if (!isset($setting['disable'])) {
54
				$ifdescrs['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " " . $mode . ": ".htmlspecialchars($setting['description']);
55
			}
56
		}
57
	}
58
}
59

    
60
$ifdescrs = array_merge($ifdescrs, interface_ipsec_vti_list_all());
61

    
62
if (!empty($_POST)) {
63
	// update view if settings are changed or saved
64
	$curif = $_POST['if'];
65
	$found = false;
66
	foreach ($ifdescrs as $descr => $ifdescr) {
67
		if ($descr == $curif) {
68
			$found = true;
69
			break;
70
		}
71
	}
72
	if ($found === false) {
73
		header("Location: status_graph.php");
74
		exit;
75
	}
76
	$cursort = $_POST['sort'];
77
	$curfilter = $_POST['filter'];
78
	$curhostipformat = $_POST['hostipformat'];
79
	$curbackgroundupdate = $_POST['backgroundupdate'];
80
	$curinvert = $_POST['invert'];
81
	$cursmoothing = $_POST['smoothfactor'];
82
	$curmode = $_POST['mode'];
83
	
84
	// Save data to config
85
	if (isset($_POST['save'])) {
86
		$pconfig = array();
87
		$pconfig["if"] = $curif;
88
		$pconfig["sort"] = $cursort;
89
		$pconfig["filter"] = $curfilter;
90
		$pconfig["hostipformat"] = $curhostipformat;
91
		$pconfig["backgroundupdate"] = $curbackgroundupdate;
92
		$pconfig["smoothfactor"] = $cursmoothing;
93
		$pconfig["invert"] = $curinvert;
94
		$pconfig["mode"] = $curmode;
95
		$config["traffic_graphs"] = array();
96
		$config["traffic_graphs"] = $pconfig;
97
		write_config("Traffic Graphs settings updated");
98
	}
99
} else {
100
	// default settings from config
101
	if (is_array($pconfig)) {
102
		$curif = $pconfig['if'];
103
		$cursort = $pconfig['sort'];
104
		$curfilter = $pconfig['filter'];
105
		$curhostipformat = $pconfig['hostipformat'];
106
		$curbackgroundupdate = $pconfig['backgroundupdate'];
107
		$cursmoothing = $pconfig['smoothfactor'];
108
		$curinvert = $pconfig['invert'];
109
		$curmode = $pconfig['mode'];;
110
	} else {
111
		// initialize when no config details are present
112
		if (empty($ifdescrs["wan"])) {
113
			/* Handle the case when WAN has been disabled. Use the first key in ifdescrs. */
114
			reset($ifdescrs);
115
			$curif = key($ifdescrs);
116
		} else {
117
			$curif = "wan";
118
		}
119
		$cursort = "";
120
		$curfilter = "";
121
		$curhostipformat = "";
122
		$curbackgroundupdate = "";
123
		$cursmoothing = 0;
124
		$curinvert = "";
125
		$curmode = "";
126
	}
127
}
128

    
129
function iflist() {
130
	global $ifdescrs;
131

    
132
	$iflist = array();
133

    
134
	foreach ($ifdescrs as $ifn => $ifd) {
135
		$iflist[$ifn] = $ifd;
136
	}
137

    
138
	return($iflist);
139
}
140

    
141
$pgtitle = array(gettext("Status"), gettext("Traffic Graph"));
142

    
143
include("head.inc");
144

    
145
$form = new Form();
146
$form->addClass('auto-submit');
147

    
148
$section = new Form_Section('Graph Settings');
149

    
150
$group = new Form_Group('Traffic Graph');
151

    
152
$group->add(new Form_Select(
153
	'if',
154
	null,
155
	$curif,
156
	iflist()
157
))->setHelp('Interface');
158

    
159
$group->add(new Form_Select(
160
	'sort',
161
	null,
162
	$cursort,
163
	array (
164
		'in'	=> gettext('Bandwidth In'),
165
		'out'	=> gettext('Bandwidth Out')
166
	)
167
))->setHelp('Sort by');
168

    
169
$group->add(new Form_Select(
170
	'filter',
171
	null,
172
	$curfilter,
173
	array (
174
		'local'	=> gettext('Local'),
175
		'remote'=> gettext('Remote'),
176
		'all'	=> gettext('All')
177
	)
178
))->setHelp('Filter');
179

    
180
$group->add(new Form_Select(
181
	'hostipformat',
182
	null,
183
	$curhostipformat,
184
	array (
185
		''			=> gettext('IP Address'),
186
		'hostname'	=> gettext('Host Name'),
187
		'descr'		=> gettext('Description'),
188
		'fqdn'		=> gettext('FQDN')
189
	)
190
))->setHelp('Display');
191

    
192
$group->add(new Form_Select(
193
    'mode',
194
    null,
195
    $curmode,
196
    array (
197
        'rate'		=> gettext('rate (standard)'),
198
        'iftop'	    => gettext('iftop (experimental)')
199
    )
200
))->setHelp('Mode');
201
    
202
$section->add($group);
203

    
204
$group2 = new Form_Group('Controls');
205

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

    
216
$group2->add(new Form_Select(
217
	'invert',
218
	null,
219
	$curinvert,
220
	array (
221
		'true'	=> gettext('On'),
222
		'false'	=> gettext('Off'),
223
	)
224
))->setHelp('Invert in/out');
225

    
226
$group2->add(new Form_Input(
227
	'smoothfactor',
228
	null,
229
	'range',
230
	$cursmoothing,
231
	array (
232
		'min' => 0,
233
		'max' => 5,
234
		'step' => 1
235
		)
236

    
237
))->setHelp('Graph Smoothing');
238

    
239
$section->add($group2);
240

    
241
$form->add($section);
242
print $form;
243

    
244
$realif = get_real_interface($curif);
245
?>
246

    
247
<script src="/vendor/d3/d3.min.js?v=<?=filemtime('/usr/local/www/vendor/d3/d3.min.js')?>"></script>
248
<script src="/vendor/nvd3/nv.d3.js?v=<?=filemtime('/usr/local/www/vendor/nvd3/nv.d3.js')?>"></script>
249
<script src="/vendor/visibility/visibility-1.2.3.min.js?v=<?=filemtime('/usr/local/www/vendor/visibility/visibility-1.2.3.min.js')?>"></script>
250

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

    
253
<script type="text/javascript">
254

    
255

    
256
//<![CDATA[
257
events.push(function() {
258

    
259
	var InterfaceString = "<?=$curif?>";
260
	var RealInterfaceString = "<?=$realif?>";
261
    window.graph_backgroundupdate = $('#backgroundupdate').val() === "true";
262
	window.smoothing = $('#smoothfactor').val();
263
	window.interval = 1;
264
	window.invert = $('#invert').val() === "true";
265
	window.size = 8;
266
	window.interfaces = InterfaceString.split("|").filter(function(entry) { return entry.trim() != ''; });
267
	window.realinterfaces = RealInterfaceString.split("|").filter(function(entry) { return entry.trim() != ''; });
268

    
269
	graph_init();
270
	graph_visibilitycheck();
271

    
272
});
273
//]]>
274
</script>
275

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

    
278
<script type="text/javascript">
279
//<![CDATA[
280

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

    
296
				$('#top10-hosts').empty();
297

    
298
				//parse top ten bandwidth abuser hosts
299
				for (var y=0; y<10; y++) {
300
					if ((y < hosts_split.length) && (hosts_split[y] != "") && (hosts_split[y] != "no info")) {
301
						hostinfo = hosts_split[y].split(";");
302

    
303
						$('#top10-hosts').append('<tr>'+
304
							'<td>'+ hostinfo[0] +'</td>'+
305
							'<td>'+ hostinfo[1] +' <?=gettext("Bits/sec");?></td>'+
306
							'<td>'+ hostinfo[2] +' <?=gettext("Bits/sec");?></td>'+
307
						'</tr>');
308
					}
309
				}
310
			},
311
	});
312
}
313

    
314
events.push(function() {
315
	$('form.auto-submit').on('change', function() {
316
		$(this).submit();
317
	});
318

    
319
	setInterval('updateBandwidth()', 3000);
320

    
321
	updateBandwidth();
322
});
323
//]]>
324
</script>
325
<?php
326

    
327
/* link the ipsec interface magically */
328
if (ipsec_enabled()) {
329
	$ifdescrs['enc0'] = gettext("IPsec");
330
}
331

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