Project

General

Profile

Download (8.44 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-2019 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

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

    
125
function iflist() {
126
	global $ifdescrs;
127

    
128
	$iflist = array();
129

    
130
	foreach ($ifdescrs as $ifn => $ifd) {
131
		$iflist[$ifn] = $ifd;
132
	}
133

    
134
	return($iflist);
135
}
136

    
137
$pgtitle = array(gettext("Status"), gettext("Traffic Graph"));
138

    
139
include("head.inc");
140

    
141
$form = new Form();
142
$form->addClass('auto-submit');
143

    
144
$section = new Form_Section('Graph Settings');
145

    
146
$group = new Form_Group('Traffic Graph');
147

    
148
$group->add(new Form_Select(
149
	'if',
150
	null,
151
	$curif,
152
	iflist()
153
))->setHelp('Interface');
154

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

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

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

    
188
$section->add($group);
189

    
190
$group2 = new Form_Group('Controls');
191

    
192
$group2->add(new Form_Select(
193
	'backgroundupdate',
194
	null,
195
	$curbackgroundupdate,
196
	array (
197
		'false'	=> gettext('Clear graphs when not visible.'),
198
		'true'	=> gettext('Keep graphs updated on inactive tab. (increases cpu usage)'),
199
	)
200
))->setHelp('Background updates');
201

    
202
$group2->add(new Form_Select(
203
	'invert',
204
	null,
205
	$curinvert,
206
	array (
207
		'true'	=> gettext('On'),
208
		'false'	=> gettext('Off'),
209
	)
210
))->setHelp('Invert in/out');
211

    
212
$group2->add(new Form_Input(
213
	'smoothfactor',
214
	null,
215
	'range',
216
	$cursmoothing,
217
	array (
218
		'min' => 0,
219
		'max' => 5,
220
		'step' => 1
221
		)
222

    
223
))->setHelp('Graph Smoothing');
224

    
225
$section->add($group2);
226

    
227
$form->add($section);
228
print $form;
229

    
230
$realif = get_real_interface($curif);
231
?>
232

    
233
<script src="/vendor/d3/d3.min.js?v=<?=filemtime('/usr/local/www/vendor/d3/d3.min.js')?>"></script>
234
<script src="/vendor/nvd3/nv.d3.js?v=<?=filemtime('/usr/local/www/vendor/nvd3/nv.d3.js')?>"></script>
235
<script src="/vendor/visibility/visibility-1.2.3.min.js?v=<?=filemtime('/usr/local/www/vendor/visibility/visibility-1.2.3.min.js')?>"></script>
236

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

    
239
<script type="text/javascript">
240

    
241

    
242
//<![CDATA[
243
events.push(function() {
244

    
245
	var InterfaceString = "<?=$curif?>";
246
	var RealInterfaceString = "<?=$realif?>";
247
    window.graph_backgroundupdate = $('#backgroundupdate').val() === "true";
248
	window.smoothing = $('#smoothfactor').val();
249
	window.interval = 1;
250
	window.invert = $('#invert').val() === "true";
251
	window.size = 8;
252
	window.interfaces = InterfaceString.split("|").filter(function(entry) { return entry.trim() != ''; });
253
	window.realinterfaces = RealInterfaceString.split("|").filter(function(entry) { return entry.trim() != ''; });
254

    
255
	graph_init();
256
	graph_visibilitycheck();
257

    
258
});
259
//]]>
260
</script>
261

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

    
264
<script type="text/javascript">
265
//<![CDATA[
266

    
267
var graph_interfacenames = <?php
268
	foreach ($ifdescrs as $ifname => $ifdescr) {
269
		$iflist[$ifname] = $ifdescr;
270
	}
271
	echo json_encode($iflist);
272
?>;
273
function updateBandwidth() {
274
	$.ajax(
275
		'/bandwidth_by_ip.php',
276
		{
277
			type: 'get',
278
			data: $(document.forms[0]).serialize(),
279
			success: function (data) {
280
				var hosts_split = data.split("|");
281

    
282
				$('#top10-hosts').empty();
283

    
284
				//parse top ten bandwidth abuser hosts
285
				for (var y=0; y<10; y++) {
286
					if ((y < hosts_split.length) && (hosts_split[y] != "") && (hosts_split[y] != "no info")) {
287
						hostinfo = hosts_split[y].split(";");
288

    
289
						$('#top10-hosts').append('<tr>'+
290
							'<td>'+ hostinfo[0] +'</td>'+
291
							'<td>'+ hostinfo[1] +' <?=gettext("Bits/sec");?></td>'+
292
							'<td>'+ hostinfo[2] +' <?=gettext("Bits/sec");?></td>'+
293
						'</tr>');
294
					}
295
				}
296
			},
297
	});
298
}
299

    
300
events.push(function() {
301
	$('form.auto-submit').on('change', function() {
302
		$(this).submit();
303
	});
304

    
305
	setInterval('updateBandwidth()', 3000);
306

    
307
	updateBandwidth();
308
});
309
//]]>
310
</script>
311
<?php
312

    
313
/* link the ipsec interface magically */
314
if (ipsec_enabled()) {
315
	$ifdescrs['enc0'] = gettext("IPsec");
316
}
317

    
318
?>
319
<div class="panel panel-default">
320
	<div class="panel-heading">
321
		<h2 class="panel-title"><?=gettext("Traffic Graph");?></h2>
322
	</div>
323
	<div class="panel-body">
324
		<div class="col-sm-6">
325
			<div id="traffic-chart-<?=$curif?>" class="d3-chart traffic-widget-chart">
326
				<svg></svg>
327
			</div>
328
		</div>
329
		<div class="col-sm-6">
330
			<table class="table table-striped table-condensed">
331
				<thead>
332
					<tr>
333
						<th><?=(($curhostipformat == "") ? gettext("Host IP") : gettext("Host Name or IP")); ?></th>
334
						<th><?=gettext("Bandwidth In"); ?></th>
335
						<th><?=gettext("Bandwidth Out"); ?></th>
336
					</tr>
337
				</thead>
338
				<tbody id="top10-hosts">
339
					<!-- to be added by javascript -->
340
				</tbody>
341
			</table>
342
		</div>
343
	</div>
344
</div>
345
<?php include("foot.inc");
(162-162/227)