summaryrefslogtreecommitdiffstats
path: root/usr/local/www/status_queues.php
blob: c147a57613442843a76542d0712b86aaab11e688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/local/bin/php
<?php
/* $Id$ */
/*
	status_queues.php
        Part of the pfSense project
	Copyright (C) 2004, 2005 Scott Ullrich
	Copyright (C) 2009 Ermal Luçi
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/
/*	
	pfSense_BUILDER_BINARIES:	/sbin/pfctl
	pfSense_MODULE:	shaper
*/

##|+PRIV
##|*IDENT=page-status-trafficshaper-queues
##|*NAME=Status: Traffic shaper: Queues page
##|*DESCR=Allow access to the 'Status: Traffic shaper: Queues' page.
##|*MATCH=status_queues.php*
##|-PRIV

header("Last-Modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT" );
header("Expires: " . gmdate( "D, j M Y H:i:s", time() ) . " GMT" );
header("Cache-Control: no-store, no-cache, must-revalidate" ); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", FALSE );
header("Pragma: no-cache"); // HTTP/1.0

require("guiconfig.inc");
class QueueStats {
	public $queuename;
	public $pps;
	public $bandwidth;
	public $borrows;
	public $suspends;
	public $drops;
}
if (!file_exists("{$g['varrun_path']}/qstats.pid") || !isvalidpid("{$g['varrun_path']}/qstats.pid")) {
	/* Start in the background so we don't hang up the GUI */	
	mwexec_bg("/usr/local/sbin/qstats -p {$g['varrun_path']}/qstats.pid");
	/* Give it a moment to start up */
	sleep(1);
}
$fd = @fsockopen("unix://{$g['varrun_path']}/qstats");
 if (!$fd) {
	$error = "Something wrong happened during comunication with stat gathering";
} else {
	$stats = "";
	while(!feof($fd))
		$stats .= fread($fd, 4096);
	fclose($fd);
	@file_put_contents("{$g['tmp_path']}/qstats", $stats);
	$altqstats = @parse_xml_config("{$g['tmp_path']}/qstats", array("altqstats"));
	if ($altqstats == -1)
		$error = "No queue statistics could be read.";
}
if ($_REQUEST['getactivity']) {	
	$statistics = array();
	$bigger_packets = 0;
	/* build the queue stats. */
	foreach($altqstats['queue'] as $q) {
		statsQueues($q);
	}
	/* calculate the bigger amount of packets being moved through all queues. */
	foreach($statistics as $q) {
		if ($bigger_packets < $q->pps)
			$bigger_packets = $q->pps;
	}
	$finscript = "";
	foreach($statistics as $q) {
		$packet_s = round(200 * (1 - $q->pps / $bigger_packets), 0);
		if ($packet_s < 0) {$packet_s = 0;}
		$finscript .= "jQuery('#queue{$q->queuename}widthb').width('{$packet_s}');";
		$finscript .= "jQuery('#queue{$q->queuename}widtha').width('" . (200 - $packet_s) . "');";
		$finscript .= "jQuery('#queue{$q->queuename}pps').val('" . number_format($q->pps,1) . "');";
		$finscript .= "jQuery('#queue{$q->queuename}bps').val('" . format_bits($q->bandwidth) . "');";
		$finscript .= "jQuery('#queue{$q->queuename}borrows').val('{$q->borrows}');";
		$finscript .= "jQuery('#queue{$q->queuename}suspends').val('{$q->suspends}');";
		$finscript .= "jQuery('#queue{$q->queuename}drops').val('{$q->drops}');";
	}
	unset($statistics, $altqstats);
	header("Content-type: text/javascript");
	echo $finscript;
	exit;
}
$pgtitle = array(gettext("Status"),gettext("Traffic shaper"),gettext("Queues"));
$shortcut_section = "trafficshaper";
include("head.inc");
?>
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<?php
if(!is_array($config['shaper']['queue']) || count($config['shaper']['queue']) < 1) {
	echo gettext("Traffic shaping is not configured.");
	include("fend.inc");
	exit;}
?>
<?php if (!$error): ?>
<form action="status_queues.php" method="post">
<script type="text/javascript">
	function getqueueactivity() {
		var url = "/status_queues.php";
		var pars = 'getactivity=yes';
		jQuery.ajax(
			url,
			{
				type: 'post',
				data: pars,
				complete: activitycallback
			});
	}
	function activitycallback(transport) {
		setTimeout('getqueueactivity()', 5100);
	}
	jQuery(document).ready(function(){
		setTimeout('getqueueactivity()', 150);
	});
</script>
<?php endif; ?>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<?php if ($error): ?>
	<tr><td><?php echo $error; ?></td></tr>
<?php else: ?>
	<tr>
		<td class="listhdr"><?=gettext("Queue"); ?></td>
		<td class="listhdr"><?=gettext("Statistics"); ?></td>
		<td class="listhdr" width="1%"><?=gettext("PPS"); ?></td>
		<td class="listhdr" width="1%"><?=gettext("Bandwidth"); ?></td>
		<td class="listhdr" width="1%"><?=gettext("Borrows"); ?></td>
		<td class="listhdr" width="1%"><?=gettext("Suspends"); ?></td>
		<td class="listhdr" width="1%"><?=gettext("Drops"); ?></td>
	</tr>
	<?php 
	$if_queue_list = get_configured_interface_list_by_realif(false, true);
	processQueues($altqstats, 0, "")?>
<?php endif; ?>
</table>
<p>
	<strong><span class="red"><?=gettext("Note"); ?>:</span></strong><strong><br></strong>
	<?=gettext("Queue graphs take 5 seconds to sample data"); ?>.<br>
	<?=gettext("You can configure the Traffic Shaper"); ?> <a href="/firewall_shaper_wizards.php"><?=gettext("here"); ?></a>.
</p>
<script type="text/javascript">
	function StatsShowHide(classname) {
    var firstrow = jQuery("." + classname).first();
    if (firstrow.is(':visible')) {
        jQuery("." + classname).hide();}
    else {
        jQuery("." + classname).show();}
	}
</script>
<?php include("fend.inc"); ?>
</body>
</html>
<?php 
function processQueues($altqstats, $level, $parent_name){
	global $g;
	global $if_queue_list;
	$gray_value = 190 + $level * 10;
	if ($gray_value > 250) $gray_value = 255;
	$row_background = str_repeat(dechex($gray_value), 3);
	$parent_name = $parent_name . " queuerow" . $altqstats['name'] . $altqstats['interface'];
	foreach ($altqstats['queue'] as $q) {
		$if_name = "";
		foreach ($if_queue_list as $oif => $real_name)
		{
			if ($oif == $q['interface'])
			{
				$if_name = $real_name;
				break;
			}
		}
		?>
		<tr class="<?php echo $parent_name?>">
			<td bgcolor="#<?php echo $row_background?>" style="padding-left: <?php echo $level * 20?>px;">
				<font color="#000000">
					<?
					if (strstr($q['name'], "root_"))
						echo "<a href=\"firewall_shaper.php?interface={$if_name}&queue={$if_name}&action=show\">" . htmlspecialchars(convert_real_interface_to_friendly_descr($q['interface'])) . "</a>";
					else
						echo "<a href=\"firewall_shaper.php?interface={$if_name}&queue={$q['name']}&action=show\">" . htmlspecialchars($q['name']) . "</a>";
					?>
				</font>
			</td>
			<?php
			$cpuUsage = 0;
			echo "<td bgcolor=\"#{$row_background}\"><nobr>";
			echo "<img src='./themes/".$g['theme']."/images/misc/bar_left.gif' height='10' width='4' border='0' align='absmiddle'>";
			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='absmiddle'>";
			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='" . (200 - $cpuUsage) . "' border='0' align='absmiddle'>";
			echo "<nobr><img src='./themes/".$g['theme']."/images/misc/bar_right.gif' height='10' width='5' border='0' align='absmiddle'> ";
			if (is_array($q['queue'])) {
				echo "<a href=\"#\" onclick=\"StatsShowHide('queuerow{$q['name']}{$q['interface']}');return false\">+/-</a>";
			}
			echo "</nobr></td>";
			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']}pps' id='queue{$q['name']}{$q['interface']}pps' value='(" . gettext("Loading") . ")' align='left'></td>";
			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>";
			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']}borrows' id='queue{$q['name']}{$q['interface']}borrows' value='' align='right'></td>";
			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']}suspends' id='queue{$q['name']}{$q['interface']}suspends' value='' align='right'></td>";
			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']}drops' id='queue{$q['name']}{$q['interface']}drops' value='' align='right'></td>";
			?>
		</tr>
		<?php
		if (is_array($q['queue']))
			processQueues($q, $level + 1, $parent_name);
	};
}
function statsQueues($xml){
	global $statistics;

	$current = new QueueStats();
	$child = new QueueStats();
	$current->queuename = $xml['name'] . $xml['interface'];
	$current->pps = $xml['measured'];
	$current->bandwidth = $xml['measuredspeedint'];
	$current->borrows = intval($xml['borrows']);
	$current->suspends = intval($xml['suspends']);
	$current->drops = intval($xml['drops']);
	if (is_array($xml['queue']))
		{
		foreach($xml['queue'] as $q) {
			$child = statsQueues($q);
			$current->pps += $child->pps;
			$current->bandwidth += $child->bandwidth;
			$current->borrows += $child->borrows;
			$current->suspends += $child->suspends;
			$current->drops += $child->drops;
		}
	}
	unset($child);
	$statistics[] = $current;
	return $current;
}
function format_bits($bits) {
	if ($bits >= 1000000000) {
		return sprintf("%.2f Gbps", $bits/1000000000);
	} else if ($bits >= 1000000) {
		return sprintf("%.2f Mbps", $bits/1000000);
	} else if ($bits >= 1000) {
		return sprintf("%.2f Kbps", $bits/1000);
	} else {
		return sprintf("%d bps", $bits);
	}
}
?>
OpenPOWER on IntegriCloud