Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/* $Id$ */
4
/*
5
	status_queues.php
6
        Part of the pfSense project
7
	Copyright (C) 2004, 2005 Scott Ullrich
8
	Copyright (C) 2009 Ermal Luçi
9
	All rights reserved.
10

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

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

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

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

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

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

    
50
require("guiconfig.inc");
51
class QueueStats {
52
	public $queuename;
53
	public $pps;
54
	public $bandwidth;
55
	public $borrows;
56
	public $suspends;
57
	public $drops;
58
}
59
if (!file_exists("{$g['varrun_path']}/qstats.pid") || !isvalidpid("{$g['varrun_path']}/qstats.pid")) {
60
	/* Start in the background so we don't hang up the GUI */	
61
	mwexec_bg("/usr/local/sbin/qstats -p {$g['varrun_path']}/qstats.pid");
62
	/* Give it a moment to start up */
63
	sleep(1);
64
}
65
$fd = @fsockopen("unix://{$g['varrun_path']}/qstats");
66
 if (!$fd) {
67
	$error = "Something wrong happened during comunication with stat gathering";
68
} else {
69
	$stats = "";
70
	while(!feof($fd))
71
		$stats .= fread($fd, 4096);
72
	fclose($fd);
73
	@file_put_contents("{$g['tmp_path']}/qstats", $stats);
74
	$altqstats = @parse_xml_config("{$g['tmp_path']}/qstats", array("altqstats"));
75
	if ($altqstats == -1)
76
		$error = "No queue statistics could be read.";
77
}
78
if ($_REQUEST['getactivity']) {	
79
	$statistics = array();
80
	$bigger_packets = 0;
81
	/* build the queue stats. */
82
	foreach($altqstats['queue'] as $q) {
83
		statsQueues($q);
84
	}
85
	/* calculate the bigger amount of packets being moved through all queues. */
86
	foreach($statistics as $q) {
87
		if ($bigger_packets < $q->pps)
88
			$bigger_packets = $q->pps;
89
	}
90
	$finscript = "";
91
	foreach($statistics as $q) {
92
		$packet_s = round(200 * (1 - $q->pps / $bigger_packets), 0);
93
		if ($packet_s < 0) {$packet_s = 0;}
94
		$finscript .= "jQuery('#queue{$q->queuename}widthb').width('{$packet_s}');";
95
		$finscript .= "jQuery('#queue{$q->queuename}widtha').width('" . (200 - $packet_s) . "');";
96
		$finscript .= "jQuery('#queue{$q->queuename}pps').val('" . number_format($q->pps,1) . "');";
97
		$finscript .= "jQuery('#queue{$q->queuename}bps').val('" . format_bits($q->bandwidth) . "');";
98
		$finscript .= "jQuery('#queue{$q->queuename}borrows').val('{$q->borrows}');";
99
		$finscript .= "jQuery('#queue{$q->queuename}suspends').val('{$q->suspends}');";
100
		$finscript .= "jQuery('#queue{$q->queuename}drops').val('{$q->drops}');";
101
	}
102
	unset($statistics, $altqstats);
103
	header("Content-type: text/javascript");
104
	echo $finscript;
105
	exit;
106
}
107
$pgtitle = array(gettext("Status"),gettext("Traffic shaper"),gettext("Queues"));
108
$shortcut_section = "trafficshaper";
109
include("head.inc");
110
?>
111
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
112
<?php include("fbegin.inc"); ?>
113
<?php
114
if(!is_array($config['shaper']['queue']) || count($config['shaper']['queue']) < 1) {
115
	echo gettext("Traffic shaping is not configured.");
116
	include("fend.inc");
117
	exit;}
118
?>
119
<?php if (!$error): ?>
120
<form action="status_queues.php" method="post">
121
<script type="text/javascript">
122
	function getqueueactivity() {
123
		var url = "/status_queues.php";
124
		var pars = 'getactivity=yes';
125
		jQuery.ajax(
126
			url,
127
			{
128
				type: 'post',
129
				data: pars,
130
				complete: activitycallback
131
			});
132
	}
133
	function activitycallback(transport) {
134
		setTimeout('getqueueactivity()', 5100);
135
	}
136
	jQuery(document).ready(function(){
137
		setTimeout('getqueueactivity()', 150);
138
	});
139
</script>
140
<?php endif; ?>
141
<table width="100%" border="1" cellpadding="0" cellspacing="0">
142
<?php if ($error): ?>
143
	<tr><td><?php echo $error; ?></td></tr>
144
<?php else: ?>
145
	<tr>
146
		<td class="listhdr"><?=gettext("Queue"); ?></td>
147
		<td class="listhdr"><?=gettext("Statistics"); ?></td>
148
		<td class="listhdr" width="1%"><?=gettext("PPS"); ?></td>
149
		<td class="listhdr" width="1%"><?=gettext("Bandwidth"); ?></td>
150
		<td class="listhdr" width="1%"><?=gettext("Borrows"); ?></td>
151
		<td class="listhdr" width="1%"><?=gettext("Suspends"); ?></td>
152
		<td class="listhdr" width="1%"><?=gettext("Drops"); ?></td>
153
	</tr>
154
	<?php 
155
	$if_queue_list = get_configured_interface_list_by_realif(false, true);
156
	processQueues($altqstats, 0, "")?>
157
<?php endif; ?>
158
</table>
159
<p>
160
	<strong><span class="red"><?=gettext("Note"); ?>:</span></strong><strong><br></strong>
161
	<?=gettext("Queue graphs take 5 seconds to sample data"); ?>.<br>
162
	<?=gettext("You can configure the Traffic Shaper"); ?> <a href="/firewall_shaper_wizards.php"><?=gettext("here"); ?></a>.
163
</p>
164
<script type="text/javascript">
165
	function StatsShowHide(classname) {
166
    var firstrow = jQuery("." + classname).first();
167
    if (firstrow.is(':visible')) {
168
        jQuery("." + classname).hide();}
169
    else {
170
        jQuery("." + classname).show();}
171
	}
172
</script>
173
<?php include("fend.inc"); ?>
174
</body>
175
</html>
176
<?php 
177
function processQueues($altqstats, $level, $parent_name){
178
	global $g;
179
	global $if_queue_list;
180
	$gray_value = 190 + $level * 10;
181
	if ($gray_value > 250) $gray_value = 255;
182
	$row_background = str_repeat(dechex($gray_value), 3);
183
	$parent_name = $parent_name . " queuerow" . $altqstats['name'] . $altqstats['interface'];
184
	foreach ($altqstats['queue'] as $q) {
185
		$if_name = "";
186
		foreach ($if_queue_list as $oif => $real_name)
187
		{
188
			if ($oif == $q['interface'])
189
			{
190
				$if_name = $real_name;
191
				break;
192
			}
193
		}
194
		?>
195
		<tr class="<?php echo $parent_name?>">
196
			<td bgcolor="#<?php echo $row_background?>" style="padding-left: <?php echo $level * 20?>px;">
197
				<font color="#000000">
198
					<?
199
					if (strstr($q['name'], "root_"))
200
						echo "<a href=\"firewall_shaper.php?interface={$if_name}&queue={$if_name}&action=show\">" . htmlspecialchars(convert_real_interface_to_friendly_descr($q['interface'])) . "</a>";
201
					else
202
						echo "<a href=\"firewall_shaper.php?interface={$if_name}&queue={$q['name']}&action=show\">" . htmlspecialchars($q['name']) . "</a>";
203
					?>
204
				</font>
205
			</td>
206
			<?php
207
			$cpuUsage = 0;
208
			echo "<td bgcolor=\"#{$row_background}\"><nobr>";
209
			echo "<img src='./themes/".$g['theme']."/images/misc/bar_left.gif' height='10' width='4' border='0' align='absmiddle'>";
210
			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'>";
211
			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'>";
212
			echo "<nobr><img src='./themes/".$g['theme']."/images/misc/bar_right.gif' height='10' width='5' border='0' align='absmiddle'> ";
213
			if (is_array($q['queue'])) {
214
				echo "<a href=\"#\" onclick=\"StatsShowHide('queuerow{$q['name']}{$q['interface']}');return false\">+/-</a>";
215
			}
216
			echo "</nobr></td>";
217
			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>";
218
			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>";
219
			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>";
220
			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>";
221
			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>";
222
			?>
223
		</tr>
224
		<?php
225
		if (is_array($q['queue']))
226
			processQueues($q, $level + 1, $parent_name);
227
	};
228
}
229
function statsQueues($xml){
230
	global $statistics;
231

    
232
	$current = new QueueStats();
233
	$child = new QueueStats();
234
	$current->queuename = $xml['name'] . $xml['interface'];
235
	$current->pps = $xml['measured'];
236
	$current->bandwidth = $xml['measuredspeedint'];
237
	$current->borrows = intval($xml['borrows']);
238
	$current->suspends = intval($xml['suspends']);
239
	$current->drops = intval($xml['drops']);
240
	if (is_array($xml['queue']))
241
		{
242
		foreach($xml['queue'] as $q) {
243
			$child = statsQueues($q);
244
			$current->pps += $child->pps;
245
			$current->bandwidth += $child->bandwidth;
246
			$current->borrows += $child->borrows;
247
			$current->suspends += $child->suspends;
248
			$current->drops += $child->drops;
249
		}
250
	}
251
	unset($child);
252
	$statistics[] = $current;
253
	return $current;
254
}
255
function format_bits($bits) {
256
	if ($bits >= 1000000000) {
257
		return sprintf("%.2f Gbps", $bits/1000000000);
258
	} else if ($bits >= 1000000) {
259
		return sprintf("%.2f Mbps", $bits/1000000);
260
	} else if ($bits >= 1000) {
261
		return sprintf("%.2f Kb", $bits/1000);
262
	} else {
263
		return sprintf("%d b", $bits);
264
	}
265
}
266
?>
(188-188/246)