Project

General

Profile

Download (12.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	status_queues.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *	Redistribution and use in source and binary forms, with or without modification,
9
 *	are permitted provided that the following conditions are met:
10
 *
11
 *	1. Redistributions of source code must retain the above copyright notice,
12
 *		this list of conditions and the following disclaimer.
13
 *
14
 *	2. Redistributions in binary form must reproduce the above copyright
15
 *		notice, this list of conditions and the following disclaimer in
16
 *		the documentation and/or other materials provided with the
17
 *		distribution.
18
 *
19
 *	3. All advertising materials mentioning features or use of this software
20
 *		must display the following acknowledgment:
21
 *		"This product includes software developed by the pfSense Project
22
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
23
 *
24
 *	4. The names "pfSense" and "pfSense Project" must not be used to
25
 *		 endorse or promote products derived from this software without
26
 *		 prior written permission. For written permission, please contact
27
 *		 coreteam@pfsense.org.
28
 *
29
 *	5. Products derived from this software may not be called "pfSense"
30
 *		nor may "pfSense" appear in their names without prior written
31
 *		permission of the Electric Sheep Fencing, LLC.
32
 *
33
 *	6. Redistributions of any form whatsoever must retain the following
34
 *		acknowledgment:
35
 *
36
 *	"This product includes software developed by the pfSense Project
37
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
38
 *
39
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
40
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
43
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
51
 *
52
 *	====================================================================
53
 *
54
 */
55
/*
56
	pfSense_BUILDER_BINARIES:	/sbin/pfctl
57
	pfSense_MODULE: shaper
58
*/
59

    
60
##|+PRIV
61
##|*IDENT=page-status-trafficshaper-queues
62
##|*NAME=Status: Traffic shaper: Queues
63
##|*DESCR=Allow access to the 'Status: Traffic shaper: Queues' page.
64
##|*MATCH=status_queues.php*
65
##|-PRIV
66
/*
67
header("Last-Modified: " . gmdate("D, j M Y H:i:s") . " GMT");
68
header("Expires: " . gmdate("D, j M Y H:i:s", time()) . " GMT");
69
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP/1.1
70
header("Pragma: no-cache"); // HTTP/1.0
71
*/
72

    
73
require("guiconfig.inc");
74
class QueueStats {
75
	public $queuename;
76
	public $queuelength;
77
	public $pps;
78
	public $bandwidth;
79
	public $borrows;
80
	public $suspends;
81
	public $drops;
82
}
83
if (!file_exists("{$g['varrun_path']}/qstats.pid") || !isvalidpid("{$g['varrun_path']}/qstats.pid")) {
84
	/* Start in the background so we don't hang up the GUI */
85
	mwexec_bg("/usr/local/sbin/qstats -p {$g['varrun_path']}/qstats.pid");
86
	/* Give it a moment to start up */
87
	sleep(1);
88
}
89
$fd = @fsockopen("unix://{$g['varrun_path']}/qstats");
90
if (!$fd) {
91
	$error = "Something wrong happened during communication with stat gathering";
92
} else {
93
	$stats = "";
94
	while (!feof($fd)) {
95
		$stats .= fread($fd, 4096);
96
	}
97
	fclose($fd);
98
	@file_put_contents("{$g['tmp_path']}/qstats", $stats);
99
	$altqstats = @parse_xml_config("{$g['tmp_path']}/qstats", array("altqstats"));
100
	if ($altqstats == -1) {
101
		$error = "No queue statistics could be read.";
102
	}
103
}
104
if ($_REQUEST['getactivity']) {
105
	$statistics = array();
106
	$bigger_stat = 0;
107
	$stat_type = $_REQUEST['stats'];
108
	/* build the queue stats. */
109
	foreach ($altqstats['queue'] as $q) {
110
		statsQueues($q);
111
	}
112
	/* calculate the bigger amount of packets or bandwidth being moved through all queues. */
113
	if ($stat_type == "0") {
114
		foreach ($statistics as $q) {
115
			if ($bigger_stat < $q->pps) {
116
				$bigger_stat = $q->pps;
117
			}
118
		}
119
	} else {
120
		foreach ($statistics as $q) {
121
			if ($bigger_stat < $q->bandwidth) {
122
				$bigger_stat = $q->bandwidth;
123
			}
124
		}
125
	}
126
	$finscript = "";
127
	foreach ($statistics as $q) {
128
		if ($stat_type == "0") {
129
			$packet_s = round(100 * ($q->pps / $bigger_stat), 0);
130
		} else {
131
			$packet_s = round(100 * ($q->bandwidth / $bigger_stat), 0);
132
		}
133
		if ($packet_s < 0) {
134
			$packet_s = 0;
135
		}
136
		$finscript .= "jQuery('#queue{$q->queuename}width').css('width','{$packet_s}%');";
137
		$finscript .= "jQuery('#queue{$q->queuename}pps').val('" . number_format($q->pps, 1) . "');";
138
		$finscript .= "jQuery('#queue{$q->queuename}bps').val('" . format_bits($q->bandwidth) . "');";
139
		$finscript .= "jQuery('#queue{$q->queuename}borrows').val('{$q->borrows}');";
140
		$finscript .= "jQuery('#queue{$q->queuename}suspends').val('{$q->suspends}');";
141
		$finscript .= "jQuery('#queue{$q->queuename}drops').val('{$q->drops}');";
142
		$finscript .= "jQuery('#queue{$q->queuename}length').val('{$q->queuelength}');";
143
	}
144
	unset($statistics, $altqstats);
145
	header("Content-type: text/javascript");
146
	echo $finscript;
147
	exit;
148
}
149
$pgtitle = array(gettext("Status"), gettext("Traffic shaper"), gettext("Queues"));
150
$shortcut_section = "trafficshaper";
151
include("head.inc");
152
?>
153
<body>
154
<script src="/jquery/jquery-1.11.2.min.js"></script>
155
<?php
156
if (!is_array($config['shaper']['queue']) || count($config['shaper']['queue']) < 1) {
157
	print_info_box(gettext("Traffic shaping is not configured."));
158
	include("foot.inc");
159
	exit;
160
}
161
?>
162
<?php if (!$error): ?>
163
<form action="status_queues.php" method="post">
164
<script type="text/javascript">
165
//<![CDATA[
166
	function getqueueactivity() {
167
		var url = "/status_queues.php";
168
		var pars = "getactivity=yes&stats=" + jQuery("#selStatistic").val();
169
		jQuery.ajax(
170
			url,
171
			{
172
				type: 'post',
173
				data: pars,
174
				complete: activitycallback
175
			});
176
	}
177
	function activitycallback(transport) {
178
		setTimeout('getqueueactivity()', 5100);
179
	}
180
	jQuery(document).ready(function() {
181
		setTimeout('getqueueactivity()', 150);
182
	});
183
//]]>
184
</script>
185
<?php endif;
186

    
187
if ($error):
188
	print_info_box($error);
189
else: ?>
190
	<div class="panel panel-default">
191
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Status Queues"); ?></h2></div>
192
		<div class="panel-body table-responsive">
193
			<table class="table table-striped table-hover">
194
				<thead>
195
					<tr>
196
						<th><?=gettext("Queue"); ?></th>
197
						<th><?=gettext("Statistics"); ?>
198
							<select id="selStatistic">
199
								<option value="0">PPS</option>
200
								<option value="1">Bandwidth</option>
201
							</select>
202
						</th>
203
						<th><?=gettext("PPS"); ?></th>
204
						<th><?=gettext("Bandwidth"); ?></th>
205
						<th><?=gettext("Borrows"); ?></th>
206
						<th><?=gettext("Suspends"); ?></th>
207
						<th><?=gettext("Drops"); ?></th>
208
						<th><?=gettext("Length"); ?></th>
209
					</tr>
210
				</thead>
211
				<tbody>
212
<?php
213
	$if_queue_list = get_configured_interface_list_by_realif(false, true);
214
	processQueues($altqstats, 0, "");
215
?>
216
<?php endif; ?>
217
				</tbody>
218
			</table>
219
		<br />
220
<?php
221
		print_info_box(gettext("Queue graphs take 5 seconds to sample data"));
222
?>
223
		</div>
224
	</div>
225
</br>
226

    
227
<?php
228

    
229

    
230

    
231
?>
232

    
233
<script type="text/javascript">
234
//<![CDATA[
235
	function StatsShowHide(classname) {
236
		var firstrow = jQuery("." + classname).first();
237
		if (firstrow.is(':visible')) {
238
			jQuery("." + classname).hide();
239
		} else {
240
			jQuery("." + classname).show();
241
		}
242
	}
243
//]]>
244
</script>
245
</form>
246
<?php
247

    
248
include("foot.inc");
249

    
250
function processQueues($altqstats, $level, $parent_name) {
251
	global $g;
252
	global $if_queue_list;
253
	$gray_value = 190 + $level * 10;
254
	if ($gray_value > 250) {
255
		$gray_value = 255;
256
	}
257
	$row_background = str_repeat(dechex($gray_value), 3);
258
	$parent_name = $parent_name . " queuerow" . $altqstats['name'] . $altqstats['interface'];
259
	$prev_if = $altqstats['interface'];
260
	foreach ($altqstats['queue'] as $q) {
261
		$if_name = "";
262
		foreach ($if_queue_list as $oif => $real_name) {
263
			if ($oif == $q['interface']) {
264
				$if_name = $real_name;
265
				break;
266
			}
267
		}
268
		if ($prev_if != $q['interface']) {
269
			echo "<tr><td><b>Interface ". htmlspecialchars(convert_real_interface_to_friendly_descr($q['interface'])) . "</b></td></tr>";
270
			$prev_if = $q['interface'];
271
		}
272
?>
273
		<tr class="<?php echo $parent_name?>">
274
			<td bgcolor="#<?php echo $row_background?>" style="padding-left: <?php echo $level * 20?>px;">
275
				<font color="#000000">
276
					<?
277
					if (is_array($q['queue'])) {
278
						echo "<a href=\"#\" onclick=\"StatsShowHide('queuerow{$q['name']}{$q['interface']}');return false\">+/-</a> ";
279
					}
280
					if (strstr($q['name'], "root_")) {
281
						echo "<a href=\"firewall_shaper.php?interface={$if_name}&amp;queue={$if_name}&amp;action=show\">Root queue</a>";
282
					} else {
283
						echo "<a href=\"firewall_shaper.php?interface={$if_name}&amp;queue={$q['name']}&amp;action=show\">" . htmlspecialchars($q['name']) . "</a>";
284
					}
285
					?>
286
				</font>
287
			</td>
288
<?php
289
		$cpuUsage = 0;
290
		echo "<td bgcolor=\"#{$row_background}\">";
291
		echo "<div class='progress' style='height: 7px;width: 170px;'>
292
				<div class='progress-bar' role='progressbar' name='queue{$q['name']}{$q['interface']}width' id='queue{$q['name']}{$q['interface']}width' aria-valuenow='70' aria-valuemin='0' aria-valuemax='100' style='width: ".	($cpuUsage*100) ."%;'></div>
293
			  </div>";
294
		echo " </td>";
295
		echo "<td bgcolor=\"#{$row_background}\"><input style='border: 0px solid white; background-color:#{$row_background}; color:#000000;width:70px;text-align:right;' size='10' name='queue{$q['name']}{$q['interface']}pps' id='queue{$q['name']}{$q['interface']}pps' value='(" . gettext("Loading") . ")' align='left' /></td>";
296
		echo "<td bgcolor=\"#{$row_background}\"><input style='border: 0px solid white; background-color:#{$row_background}; color:#000000;width:80px;text-align:right;' size='10' name='queue{$q['name']}{$q['interface']}bps' id='queue{$q['name']}{$q['interface']}bps' value='' align='right' /></td>";
297
		echo "<td bgcolor=\"#{$row_background}\"><input style='border: 0px solid white; background-color:#{$row_background}; color:#000000;width:70px;text-align:right;' size='10' name='queue{$q['name']}{$q['interface']}borrows' id='queue{$q['name']}{$q['interface']}borrows' value='' align='right' /></td>";
298
		echo "<td bgcolor=\"#{$row_background}\"><input style='border: 0px solid white; background-color:#{$row_background}; color:#000000;width:70px;text-align:right;' size='10' name='queue{$q['name']}{$q['interface']}suspends' id='queue{$q['name']}{$q['interface']}suspends' value='' align='right' /></td>";
299
		echo "<td bgcolor=\"#{$row_background}\"><input style='border: 0px solid white; background-color:#{$row_background}; color:#000000;width:70px;text-align:right;' size='10' name='queue{$q['name']}{$q['interface']}drops' id='queue{$q['name']}{$q['interface']}drops' value='' align='right' /></td>";
300
		echo "<td bgcolor=\"#{$row_background}\"><input style='border: 0px solid white; background-color:#{$row_background}; color:#000000;width:70px;text-align:right;' size='10' name='queue{$q['name']}{$q['interface']}length' id='queue{$q['name']}{$q['interface']}length' value='' align='right' /></td>";
301
?>
302
		</tr>
303
<?php
304
		if (is_array($q['queue'])) {
305
			processQueues($q, $level + 1, $parent_name);
306
		}
307
	};
308
}
309

    
310
function statsQueues($xml) {
311
	global $statistics;
312

    
313
	$current = new QueueStats();
314
	$child = new QueueStats();
315
	$current->queuename = $xml['name'] . $xml['interface'];
316
	$current->queuelength = $xml['qlength'];
317
	$current->pps = $xml['measured'];
318
	$current->bandwidth = $xml['measuredspeedint'];
319
	$current->borrows = intval($xml['borrows']);
320
	$current->suspends = intval($xml['suspends']);
321
	$current->drops = intval($xml['droppedpkts']);
322
	if (is_array($xml['queue'])) {
323
		foreach ($xml['queue'] as $q) {
324
			$child = statsQueues($q);
325
			$current->pps += $child->pps;
326
			$current->bandwidth += $child->bandwidth;
327
			$current->borrows += $child->borrows;
328
			$current->suspends += $child->suspends;
329
			$current->drops += $child->drops;
330
		}
331
	}
332
	unset($child);
333
	$statistics[] = $current;
334
	return $current;
335
}
336
function format_bits($bits) {
337
	if ($bits >= 1000000000) {
338
		return sprintf("%.2f Gbps", $bits/1000000000);
339
	} else if ($bits >= 1000000) {
340
		return sprintf("%.2f Mbps", $bits/1000000);
341
	} else if ($bits >= 1000) {
342
		return sprintf("%.2f Kbps", $bits/1000);
343
	} else {
344
		return sprintf("%d bps", $bits);
345
	}
346
}
347
?>
(177-177/228)