Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
1 b5b351be Scott Ullrich
#!/usr/local/bin/php
2 aa280d10 Bill Marquette
<?php
3 b46bfcf5 Bill Marquette
/* $Id$ */
4 aa280d10 Bill Marquette
/*
5
	status_queues.php
6 ce77a9c4 Phil Davis
	Part of the pfSense project
7 dd447bde Jim Thompson
	Copyright (C) 2004, 2005 Scott Ullrich
8 29aef6c4 Jim Thompson
	Copyright (C) 2009 Ermal Luçi
9 ce77a9c4 Phil Davis
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
10 aa280d10 Bill Marquette
	All rights reserved.
11
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14
15
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17
18
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33 2c5fda82 Jose Luis Duran
/*
34 1d333258 Scott Ullrich
	pfSense_BUILDER_BINARIES:	/sbin/pfctl
35
	pfSense_MODULE:	shaper
36
*/
37 aa280d10 Bill Marquette
38 6b07c15a Matthew Grooms
##|+PRIV
39
##|*IDENT=page-status-trafficshaper-queues
40
##|*NAME=Status: Traffic shaper: Queues page
41
##|*DESCR=Allow access to the 'Status: Traffic shaper: Queues' page.
42
##|*MATCH=status_queues.php*
43
##|-PRIV
44
45 42b0c921 Phil Davis
header("Last-Modified: " . gmdate("D, j M Y H:i:s") . " GMT");
46
header("Expires: " . gmdate("D, j M Y H:i:s", time()) . " GMT");
47
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP/1.1
48 5f471c95 Ermal
header("Pragma: no-cache"); // HTTP/1.0
49
50 aa280d10 Bill Marquette
require("guiconfig.inc");
51 21b94a54 Michele Di Maria
class QueueStats {
52
	public $queuename;
53 2c5fda82 Jose Luis Duran
	public $queuelength;
54 6ba3121b Michele Di Maria
	public $pps;
55
	public $bandwidth;
56 21b94a54 Michele Di Maria
	public $borrows;
57
	public $suspends;
58
	public $drops;
59
}
60 3a1e12cf jim-p
if (!file_exists("{$g['varrun_path']}/qstats.pid") || !isvalidpid("{$g['varrun_path']}/qstats.pid")) {
61 2c5fda82 Jose Luis Duran
	/* Start in the background so we don't hang up the GUI */
62 a3eab908 Michele Di Maria
	mwexec_bg("/usr/local/sbin/qstats -p {$g['varrun_path']}/qstats.pid");
63 3a1e12cf jim-p
	/* Give it a moment to start up */
64
	sleep(1);
65
}
66 5f471c95 Ermal
$fd = @fsockopen("unix://{$g['varrun_path']}/qstats");
67 42b0c921 Phil Davis
if (!$fd) {
68
	$error = "Something wrong happened during communication with stat gathering";
69 3a1e12cf jim-p
} else {
70
	$stats = "";
71 42b0c921 Phil Davis
	while(!feof($fd)) {
72 3a1e12cf jim-p
		$stats .= fread($fd, 4096);
73 42b0c921 Phil Davis
	}
74 3a1e12cf jim-p
	fclose($fd);
75
	@file_put_contents("{$g['tmp_path']}/qstats", $stats);
76
	$altqstats = @parse_xml_config("{$g['tmp_path']}/qstats", array("altqstats"));
77 42b0c921 Phil Davis
	if ($altqstats == -1) {
78 3a1e12cf jim-p
		$error = "No queue statistics could be read.";
79 42b0c921 Phil Davis
	}
80 5f471c95 Ermal
}
81 2c5fda82 Jose Luis Duran
if ($_REQUEST['getactivity']) {
82 21b94a54 Michele Di Maria
	$statistics = array();
83 87428ee8 Michele Di Maria
	$bigger_stat = 0;
84
	$stat_type = $_REQUEST['stats'];
85 21b94a54 Michele Di Maria
	/* build the queue stats. */
86 42b0c921 Phil Davis
	foreach ($altqstats['queue'] as $q) {
87 21b94a54 Michele Di Maria
		statsQueues($q);
88
	}
89 87428ee8 Michele Di Maria
	/* calculate the bigger amount of packets or bandwidth being moved through all queues. */
90
	if ($stat_type == "0")
91
	{
92 42b0c921 Phil Davis
		foreach ($statistics as $q) {
93
			if ($bigger_stat < $q->pps) {
94 87428ee8 Michele Di Maria
				$bigger_stat = $q->pps;
95 42b0c921 Phil Davis
			}
96 87428ee8 Michele Di Maria
		}
97 21b94a54 Michele Di Maria
	}
98 87428ee8 Michele Di Maria
	else
99
	{
100 42b0c921 Phil Davis
		foreach ($statistics as $q) {
101
			if ($bigger_stat < $q->bandwidth) {
102 87428ee8 Michele Di Maria
				$bigger_stat = $q->bandwidth;
103 42b0c921 Phil Davis
			}
104 87428ee8 Michele Di Maria
		}
105 2c5fda82 Jose Luis Duran
	}
106 f2b8daad Ermal Lu?i
	$finscript = "";
107 42b0c921 Phil Davis
	foreach ($statistics as $q) {
108
		if ($stat_type == "0") {
109 87428ee8 Michele Di Maria
			$packet_s = round(150 * (1 - $q->pps / $bigger_stat), 0);
110 42b0c921 Phil Davis
		} else {
111 87428ee8 Michele Di Maria
			$packet_s = round(150 * (1 - $q->bandwidth / $bigger_stat), 0);
112 42b0c921 Phil Davis
		}
113
		if ($packet_s < 0) {
114
			$packet_s = 0;
115
		}
116 21b94a54 Michele Di Maria
		$finscript .= "jQuery('#queue{$q->queuename}widthb').width('{$packet_s}');";
117 8e006931 Michele Di Maria
		$finscript .= "jQuery('#queue{$q->queuename}widtha').width('" . (150 - $packet_s) . "');";
118 ff3003df Michele Di Maria
		$finscript .= "jQuery('#queue{$q->queuename}pps').val('" . number_format($q->pps,1) . "');";
119 b06abfe3 Michele Di Maria
		$finscript .= "jQuery('#queue{$q->queuename}bps').val('" . format_bits($q->bandwidth) . "');";
120 21b94a54 Michele Di Maria
		$finscript .= "jQuery('#queue{$q->queuename}borrows').val('{$q->borrows}');";
121
		$finscript .= "jQuery('#queue{$q->queuename}suspends').val('{$q->suspends}');";
122
		$finscript .= "jQuery('#queue{$q->queuename}drops').val('{$q->drops}');";
123 8e006931 Michele Di Maria
		$finscript .= "jQuery('#queue{$q->queuename}length').val('{$q->queuelength}');";
124 21b94a54 Michele Di Maria
	}
125 b06abfe3 Michele Di Maria
	unset($statistics, $altqstats);
126 f2b8daad Ermal Lu?i
	header("Content-type: text/javascript");
127
	echo $finscript;
128 c5d63426 Bill Marquette
	exit;
129
}
130 6deb0c03 Carlos Eduardo Ramos
$pgtitle = array(gettext("Status"),gettext("Traffic shaper"),gettext("Queues"));
131 d71fc5d3 jim-p
$shortcut_section = "trafficshaper";
132 4df96eff Scott Ullrich
include("head.inc");
133 aa280d10 Bill Marquette
?>
134
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
135
<?php include("fbegin.inc"); ?>
136 d1dd0241 Scott Ullrich
<?php
137 42b0c921 Phil Davis
if (!is_array($config['shaper']['queue']) || count($config['shaper']['queue']) < 1) {
138 6deb0c03 Carlos Eduardo Ramos
	echo gettext("Traffic shaping is not configured.");
139 d1dd0241 Scott Ullrich
	include("fend.inc");
140 580a6561 Colin Fleming
	echo "</body></html>";
141 42b0c921 Phil Davis
	exit;
142
}
143 d1dd0241 Scott Ullrich
?>
144 3a1e12cf jim-p
<?php if (!$error): ?>
145 aa280d10 Bill Marquette
<form action="status_queues.php" method="post">
146 f2b8daad Ermal Lu?i
<script type="text/javascript">
147 d456b043 Colin Fleming
//<![CDATA[
148 21b94a54 Michele Di Maria
	function getqueueactivity() {
149
		var url = "/status_queues.php";
150 87428ee8 Michele Di Maria
		var pars = "getactivity=yes&stats=" + jQuery("#selStatistic").val();
151 21b94a54 Michele Di Maria
		jQuery.ajax(
152
			url,
153
			{
154
				type: 'post',
155
				data: pars,
156
				complete: activitycallback
157
			});
158
	}
159
	function activitycallback(transport) {
160
		setTimeout('getqueueactivity()', 5100);
161
	}
162 42b0c921 Phil Davis
	jQuery(document).ready(function() {
163 21b94a54 Michele Di Maria
		setTimeout('getqueueactivity()', 150);
164
	});
165 d456b043 Colin Fleming
//]]>
166 f2b8daad Ermal Lu?i
</script>
167 3a1e12cf jim-p
<?php endif; ?>
168 d456b043 Colin Fleming
<table width="100%" border="1" cellpadding="0" cellspacing="0" summary="status queues">
169 3a1e12cf jim-p
<?php if ($error): ?>
170 21b94a54 Michele Di Maria
	<tr><td><?php echo $error; ?></td></tr>
171 3a1e12cf jim-p
<?php else: ?>
172 21b94a54 Michele Di Maria
	<tr>
173
		<td class="listhdr"><?=gettext("Queue"); ?></td>
174 87428ee8 Michele Di Maria
		<td class="listhdr">
175
			<?=gettext("Statistics"); ?>
176
			<select id="selStatistic">
177
				<option value="0">PPS</option>
178
				<option value="1">Bandwidth</option>
179
			</select>
180
		</td>
181 21b94a54 Michele Di Maria
		<td class="listhdr" width="1%"><?=gettext("PPS"); ?></td>
182
		<td class="listhdr" width="1%"><?=gettext("Bandwidth"); ?></td>
183
		<td class="listhdr" width="1%"><?=gettext("Borrows"); ?></td>
184
		<td class="listhdr" width="1%"><?=gettext("Suspends"); ?></td>
185
		<td class="listhdr" width="1%"><?=gettext("Drops"); ?></td>
186 2c5fda82 Jose Luis Duran
		<td class="listhdr" width="1%"><?=gettext("Length"); ?></td>
187 21b94a54 Michele Di Maria
	</tr>
188 42b0c921 Phil Davis
<?php
189 6ba3121b Michele Di Maria
	$if_queue_list = get_configured_interface_list_by_realif(false, true);
190 42b0c921 Phil Davis
	processQueues($altqstats, 0, "")
191
?>
192 3a1e12cf jim-p
<?php endif; ?>
193 21b94a54 Michele Di Maria
</table>
194
<p>
195 8cd558b6 ayvis
	<strong><span class="red"><?=gettext("Note"); ?>:</span></strong><br />
196
	<?=gettext("Queue graphs take 5 seconds to sample data"); ?>.<br />
197 21b94a54 Michele Di Maria
	<?=gettext("You can configure the Traffic Shaper"); ?> <a href="/firewall_shaper_wizards.php"><?=gettext("here"); ?></a>.
198
</p>
199 ff3003df Michele Di Maria
<script type="text/javascript">
200 d456b043 Colin Fleming
//<![CDATA[
201 ff3003df Michele Di Maria
	function StatsShowHide(classname) {
202 42b0c921 Phil Davis
		var firstrow = jQuery("." + classname).first();
203
		if (firstrow.is(':visible')) {
204
			jQuery("." + classname).hide();
205
		} else {
206
			jQuery("." + classname).show();
207
		}
208 ff3003df Michele Di Maria
	}
209 d456b043 Colin Fleming
//]]>
210 ff3003df Michele Di Maria
</script>
211 84903059 Michele Di Maria
</form>
212 aa280d10 Bill Marquette
<?php include("fend.inc"); ?>
213
</body>
214
</html>
215 2c5fda82 Jose Luis Duran
<?php
216 42b0c921 Phil Davis
function processQueues($altqstats, $level, $parent_name) {
217 21b94a54 Michele Di Maria
	global $g;
218 6ba3121b Michele Di Maria
	global $if_queue_list;
219 ff3003df Michele Di Maria
	$gray_value = 190 + $level * 10;
220 42b0c921 Phil Davis
	if ($gray_value > 250) {
221
		$gray_value = 255;
222
	}
223 ff3003df Michele Di Maria
	$row_background = str_repeat(dechex($gray_value), 3);
224
	$parent_name = $parent_name . " queuerow" . $altqstats['name'] . $altqstats['interface'];
225 1679b68c Michele Di Maria
	$prev_if = $altqstats['interface'];
226 6ba3121b Michele Di Maria
	foreach ($altqstats['queue'] as $q) {
227
		$if_name = "";
228 1679b68c Michele Di Maria
		foreach ($if_queue_list as $oif => $real_name) {
229
			if ($oif == $q['interface']) {
230 6ba3121b Michele Di Maria
				$if_name = $real_name;
231
				break;
232
			}
233
		}
234 1679b68c Michele Di Maria
		if ($prev_if != $q['interface']) {
235
			echo "<tr><td colspan=\"8\" style=\"padding: 2px;\"><b>Interface ". htmlspecialchars(convert_real_interface_to_friendly_descr($q['interface'])) . "</b></td></tr>";
236
			$prev_if = $q['interface'];
237
		}
238 42b0c921 Phil Davis
?>
239 ff3003df Michele Di Maria
		<tr class="<?php echo $parent_name?>">
240
			<td bgcolor="#<?php echo $row_background?>" style="padding-left: <?php echo $level * 20?>px;">
241 21b94a54 Michele Di Maria
				<font color="#000000">
242
					<?
243 42b0c921 Phil Davis
					if (strstr($q['name'], "root_")) {
244 84903059 Michele Di Maria
						echo "<a href=\"firewall_shaper.php?interface={$if_name}&amp;queue={$if_name}&amp;action=show\">Root queue</a>";
245 42b0c921 Phil Davis
					} else {
246 84903059 Michele Di Maria
						echo "<a href=\"firewall_shaper.php?interface={$if_name}&amp;queue={$q['name']}&amp;action=show\">" . htmlspecialchars($q['name']) . "</a>";
247 42b0c921 Phil Davis
					}
248 21b94a54 Michele Di Maria
					?>
249
				</font>
250 ff3003df Michele Di Maria
			</td>
251 42b0c921 Phil Davis
<?php
252
		$cpuUsage = 0;
253
		echo "<td class=\"nowrap\" width=\"1%\" bgcolor=\"#{$row_background}\">";
254
		echo "<img src='./themes/".$g['theme']."/images/misc/bar_left.gif' height='10' width='4' border='0' align='middle' alt='' />";
255
		echo "<img src='./themes/".$g['theme']."/images/misc/bar_blue.gif' height='10' name='queue{$q['name']}{$q['interface']}widtha' id='queue{$q['name']}{$q['interface']}widtha' width='" . $cpuUsage . "' border='0' align='middle' alt='" . htmlspecialchars($q['name']) . "' />";
256
		echo "<img src='./themes/".$g['theme']."/images/misc/bar_gray.gif' height='10' name='queue{$q['name']}{$q['interface']}widthb' id='queue{$q['name']}{$q['interface']}widthb' width='" . (150 - $cpuUsage) . "' border='0' align='middle' alt='" . htmlspecialchars($q['name']) . "' />";
257
		echo "<img src='./themes/".$g['theme']."/images/misc/bar_right.gif' height='10' width='5' border='0' align='middle' alt='' /> ";
258
		if (is_array($q['queue'])) {
259
			echo "<a href=\"#\" onclick=\"StatsShowHide('queuerow{$q['name']}{$q['interface']}');return false\">+/-</a> ";
260
		}
261
		echo " </td>";
262
		echo "<td width=\"1%\" 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>";
263
		echo "<td width=\"1%\" 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>";
264
		echo "<td width=\"1%\" 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>";
265
		echo "<td width=\"1%\" 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>";
266
		echo "<td width=\"1%\" 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>";
267
		echo "<td width=\"1%\" 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>";
268
?>
269 21b94a54 Michele Di Maria
		</tr>
270 42b0c921 Phil Davis
<?php
271
		if (is_array($q['queue'])) {
272 ff3003df Michele Di Maria
			processQueues($q, $level + 1, $parent_name);
273 42b0c921 Phil Davis
		}
274 21b94a54 Michele Di Maria
	};
275
}
276 42b0c921 Phil Davis
function statsQueues($xml) {
277 21b94a54 Michele Di Maria
	global $statistics;
278 b06abfe3 Michele Di Maria
279 21b94a54 Michele Di Maria
	$current = new QueueStats();
280
	$child = new QueueStats();
281
	$current->queuename = $xml['name'] . $xml['interface'];
282 2c5fda82 Jose Luis Duran
	$current->queuelength = $xml['qlength'];
283 ff3003df Michele Di Maria
	$current->pps = $xml['measured'];
284
	$current->bandwidth = $xml['measuredspeedint'];
285 21b94a54 Michele Di Maria
	$current->borrows = intval($xml['borrows']);
286
	$current->suspends = intval($xml['suspends']);
287 8e006931 Michele Di Maria
	$current->drops = intval($xml['droppedpkts']);
288 1679b68c Michele Di Maria
	if (is_array($xml['queue'])) {
289 42b0c921 Phil Davis
		foreach ($xml['queue'] as $q) {
290 21b94a54 Michele Di Maria
			$child = statsQueues($q);
291
			$current->pps += $child->pps;
292
			$current->bandwidth += $child->bandwidth;
293
			$current->borrows += $child->borrows;
294
			$current->suspends += $child->suspends;
295
			$current->drops += $child->drops;
296
		}
297
	}
298 a3eab908 Michele Di Maria
	unset($child);
299 21b94a54 Michele Di Maria
	$statistics[] = $current;
300
	return $current;
301
}
302 b06abfe3 Michele Di Maria
function format_bits($bits) {
303
	if ($bits >= 1000000000) {
304
		return sprintf("%.2f Gbps", $bits/1000000000);
305
	} else if ($bits >= 1000000) {
306
		return sprintf("%.2f Mbps", $bits/1000000);
307
	} else if ($bits >= 1000) {
308 31e41cf4 Michele Di Maria
		return sprintf("%.2f Kbps", $bits/1000);
309 b06abfe3 Michele Di Maria
	} else {
310 31e41cf4 Michele Di Maria
		return sprintf("%d bps", $bits);
311 b06abfe3 Michele Di Maria
	}
312
}
313 73eeee93 Michele Di Maria
?>