Project

General

Profile

Download (8.35 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-2019 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
 * 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
 *
17
 * http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * 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
 */
25

    
26
##|+PRIV
27
##|*IDENT=page-status-trafficgraph
28
##|*NAME=Status: Traffic Graph
29
##|*DESCR=Allow access to the 'Status: Traffic Graph' page.
30
##|*MATCH=status_graph.php*
31
##|*MATCH=bandwidth_by_ip.php*
32
##|*MATCH=graph.php*
33
##|*MATCH=ifstats.php*
34
##|-PRIV
35

    
36
require_once("guiconfig.inc");
37
require_once("ipsec.inc");
38

    
39
if (is_array($config["traffic_graphs"])){
40
	$pconfig = $config["traffic_graphs"];
41
}
42
// Get configured interface list
43
$ifdescrs = get_configured_interface_with_descr();
44
if (ipsec_enabled()) {
45
	$ifdescrs['enc0'] = gettext("IPsec");
46
}
47

    
48
foreach (array('server', 'client') as $mode) {
49
	if (is_array($config['openvpn']["openvpn-{$mode}"])) {
50
		foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
51
			if (!isset($setting['disable'])) {
52
				$ifdescrs['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " " . $mode . ": ".htmlspecialchars($setting['description']);
53
			}
54
		}
55
	}
56
}
57

    
58
$ifdescrs = array_merge($ifdescrs, interface_ipsec_vti_list_all());
59

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

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

    
123
function iflist() {
124
	global $ifdescrs;
125

    
126
	$iflist = array();
127

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

    
132
	return($iflist);
133
}
134

    
135
$pgtitle = array(gettext("Status"), gettext("Traffic Graph"));
136

    
137
include("head.inc");
138

    
139
$form = new Form();
140
$form->addClass('auto-submit');
141

    
142
$section = new Form_Section('Graph Settings');
143

    
144
$group = new Form_Group('Traffic Graph');
145

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

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

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

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

    
186
$section->add($group);
187

    
188
$group2 = new Form_Group('Controls');
189

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

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

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

    
221
))->setHelp('Graph Smoothing');
222

    
223
$section->add($group2);
224

    
225
$form->add($section);
226
print $form;
227

    
228
$realif = get_real_interface($curif);
229
?>
230

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

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

    
237
<script type="text/javascript">
238

    
239

    
240
//<![CDATA[
241
events.push(function() {
242

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

    
253
	graph_init();
254
	graph_visibilitycheck();
255

    
256
});
257
//]]>
258
</script>
259

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

    
262
<script type="text/javascript">
263
//<![CDATA[
264

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

    
280
				$('#top10-hosts').empty();
281

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

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

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

    
303
	setInterval('updateBandwidth()', 3000);
304

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

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

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