Project

General

Profile

Download (6.19 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_pftop.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-2023 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
##|+PRIV
25
##|*IDENT=page-diagnostics-system-pftop
26
##|*NAME=Diagnostics: pfTop
27
##|*DESCR=Allows access to the 'Diagnostics: pfTop' page
28
##|*MATCH=diag_pftop.php*
29
##|-PRIV
30

    
31
require_once("guiconfig.inc");
32

    
33
$pgtitle = array(gettext("Diagnostics"), gettext("pfTop"));
34
$pftop = "/usr/local/sbin/pftop";
35

    
36
$sorttypes = array('age', 'bytes', 'dest', 'dport', 'exp', 'none', 'pkt', 'sport', 'src');
37
$viewtypes = array('default', 'label', 'long', 'queue', 'rules', 'size', 'speed', 'state', 'time');
38
$viewall = array('queue', 'label', 'rules');
39
$numstates = array('50', '100', '200', '500', '1000', 'all');
40

    
41
if ($_REQUEST['getactivity']) {
42
	if ($_REQUEST['sorttype'] && in_array($_REQUEST['sorttype'], $sorttypes) &&
43
	    $_REQUEST['viewtype'] && in_array($_REQUEST['viewtype'], $viewtypes) &&
44
	    $_REQUEST['states'] && in_array($_REQUEST['states'], $numstates)) {
45
		$viewtype = escapeshellarg($_REQUEST['viewtype']);
46
		if (in_array($_REQUEST['viewtype'], $viewall)) {
47
			$sorttype = "";
48
			$numstate = "-a";
49
		} else {
50
			$sorttype = "-o " . escapeshellarg($_REQUEST['sorttype']);
51
			$numstate = ($_REQUEST['states'] == "all" ? "-a" : escapeshellarg($_REQUEST['states']));
52
		}
53
	} else {
54
		$sorttype = "bytes";
55
		$viewtype = "default";
56
		$numstate = "100";
57
	}
58
	if ($_REQUEST['filter'] != "") {
59
		$filter = "-f " . escapeshellarg($_REQUEST['filter']);
60
	} else {
61
		$filter = "";
62
	}
63
	$text = shell_exec("$pftop {$filter} -b {$sorttype} -w 135 -v {$viewtype} {$numstate}");
64
	if (empty($text)) {
65
		echo "Invalid filter, check syntax";
66
	} else {
67
		echo trim(htmlentities($text));
68
	}
69
	exit;
70
}
71

    
72
include("head.inc");
73

    
74
if ($_REQUEST['sorttype'] && in_array($_REQUEST['sorttype'], $sorttypes) &&
75
    $_REQUEST['viewtype'] && in_array($_REQUEST['viewtype'], $viewtypes) &&
76
    $_REQUEST['states'] && in_array($_REQUEST['states'], $numstates)) {
77
	$viewtype = escapeshellarg($_REQUEST['viewtype']);
78
	if (in_array($_REQUEST['viewtype'], $viewall)) {
79
		$sorttype = "";
80
		$numstate = "-a";
81
	} else {
82
		$sorttype = "-o " . escapeshellarg($_REQUEST['sorttype']);
83
		$numstate = ($_REQUEST['states'] == "all" ? "-a" : escapeshellarg($_REQUEST['states']));
84
	}
85
} else {
86
	$sorttype = "bytes";
87
	$viewtype = "default";
88
	$numstate = "100";
89
}
90
if ($_REQUEST['filter'] != "") {
91
	$filter = "-f " . escapeshellarg($_REQUEST['filter']);
92
} else {
93
	$filter = "";
94
}
95

    
96
if ($input_errors) {
97
	print_input_errors($input_errors);
98
}
99

    
100
$form = new Form(false);
101
$form->addGlobal(new Form_Input(
102
	'getactivity',
103
	null,
104
	'hidden',
105
	'yes'
106
));
107
$section = new Form_Section('pfTop Configuration');
108

    
109
$validViews = array(
110
	'default' => gettext('default'),
111
	'label' => gettext('label'),
112
	'long' => gettext('long'),
113
	'queue' => gettext('queue'),
114
	'rules' => gettext('rules'),
115
	'size' => gettext('size'),
116
	'speed' => gettext('speed'),
117
	'state' => gettext('state'),
118
	'time' => gettext('time'),
119
);
120
$section->addInput(new Form_Select(
121
	'viewtype',
122
	'View',
123
	$viewtype,
124
	$validViews
125
));
126

    
127
$section->addInput(new Form_Input(
128
	'filter',
129
	'Filter expression',
130
	'text',
131
	$_REQUEST['filter'],
132
	['placeholder' => 'e.g. tcp, ip6 or dst net 208.123.73.0/24']
133
))->setHelp('<em>click for filter help</em>%1$s' .
134
	'<code>[proto &lt;ip|ip6|ah|carp|esp|icmp|ipv6-icmp|pfsync|tcp|udp&gt;]</code><br />' .
135
	'<code>[src|dst|gw] [host|net|port] &lt;host/network/port&gt;</code><br />' .
136
	'<code>[in|out]</code><br /><br />' .
137
	'These are the most common selectors. Some expressions can be combined using "and" / "or". ' .
138
	'See %2$s for more detailed expression syntax.%3$s',
139
	'<span class="infoblock"><br />',
140
	'<a target="_blank" href="https://www.freebsd.org/cgi/man.cgi?query=pftop#STATE_FILTERING">pftop(8)</a>',
141
	'</span></p>'
142
);
143

    
144
$section->addInput(new Form_Select(
145
	'sorttype',
146
	'Sort by',
147
	$sorttype,
148
	array(
149
		'none' => gettext('None'),
150
		'age' => gettext('Age'),
151
		'bytes' => gettext('Bytes'),
152
		'dest' => gettext('Destination Address'),
153
		'dport' => gettext('Destination Port'),
154
		'exp' => gettext('Expiry'),
155
		'pkt' => gettext('Packet'),
156
		'sport' => gettext('Source Port'),
157
		'src' => gettext('Source Address'),
158
	)
159
));
160

    
161
$validStates = array(50, 100, 200, 500, 1000, 'all');
162
$section->addInput(new Form_Select(
163
	'states',
164
	'Maximum # of States',
165
	$numstate,
166
	array_combine($validStates, $validStates)
167
));
168

    
169
$form->add($section);
170
print $form;
171
?>
172

    
173
<script type="text/javascript">
174
//<![CDATA[
175
	function getpftopactivity() {
176
		$.ajax(
177
			'/diag_pftop.php',
178
			{
179
				method: 'post',
180
				data: $(document.forms[0]).serialize(),
181
				dataType: "html",
182
				success: function (data) {
183
					$('#xhrOutput').html(data);
184
				},
185
			}
186
		);
187
	}
188

    
189
	events.push(function() {
190
		setInterval('getpftopactivity()', 2500);
191
		getpftopactivity();
192
	});
193
//]]>
194
</script>
195

    
196
<div class="panel panel-default">
197
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Output')?></h2></div>
198
	<div class="panel panel-body">
199
		<pre id="xhrOutput"><?=gettext("Gathering pfTOP activity, please wait...")?></pre>
200
	</div>
201
</div>
202

    
203
<script type="text/javascript">
204
//<![CDATA[
205
events.push(function() {
206
	$('#viewtype').on('change', function() {
207
		if (['queue', 'label', 'rules'].indexOf($(this).val()) > -1) {
208
			$("#filter, #sorttype, #sorttypediv, #statesdiv, #states").parents('.form-group').hide();
209
		} else {
210
			$("#filter, #sorttype, #sorttypediv, #statesdiv, #states").parents('.form-group').show();
211
		}
212
	});
213
	$('#filter').on('keypress keyup', function(event) {
214
		var keyPressed = event.keyCode || event.which;
215
		if (keyPressed === 13) {
216
			event.preventDefault();
217
			return false;
218
		}
219
	});
220
});
221
//]]>
222
</script>
223
<?php include("foot.inc");
(23-23/228)