Project

General

Profile

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

    
69
require("guiconfig.inc");
70
class QueueStats {
71
	public $queuename;
72
	public $queuelength;
73
	public $pps;
74
	public $bandwidth;
75
	public $borrows;
76
	public $suspends;
77
	public $drops;
78
}
79
if (!file_exists("{$g['varrun_path']}/qstats.pid") || !isvalidpid("{$g['varrun_path']}/qstats.pid")) {
80
	/* Start in the background so we don't hang up the GUI */
81
	mwexec_bg("/usr/local/sbin/qstats -p {$g['varrun_path']}/qstats.pid");
82
	/* Give it a moment to start up */
83
	sleep(1);
84
}
85
$fd = @fsockopen("unix://{$g['varrun_path']}/qstats");
86
if (!$fd) {
87
	$error = gettext("Something wrong happened during communication with stat gathering");
88
} else {
89
	$stats = "";
90
	while (!feof($fd)) {
91
		$stats .= fread($fd, 4096);
92
	}
93
	fclose($fd);
94
	@file_put_contents("{$g['tmp_path']}/qstats", $stats);
95
	$altqstats = @parse_xml_config("{$g['tmp_path']}/qstats", array("altqstats"));
96
	if ($altqstats == -1) {
97
		$error = gettext("No queue statistics could be read.");
98
	}
99
}
100
if ($_REQUEST['getactivity']) {
101
	$statistics = array();
102
	$bigger_stat = 0;
103
	$stat_type = $_REQUEST['stats'];
104
	/* build the queue stats. */
105
	foreach ($altqstats['queue'] as $q) {
106
		statsQueues($q);
107
	}
108
	/* calculate the bigger amount of packets or bandwidth being moved through all queues. */
109
	if ($stat_type == "0") {
110
		foreach ($statistics as $q) {
111
			if ($bigger_stat < $q->pps) {
112
				$bigger_stat = $q->pps;
113
			}
114
		}
115
	} else {
116
		foreach ($statistics as $q) {
117
			if ($bigger_stat < $q->bandwidth) {
118
				$bigger_stat = $q->bandwidth;
119
			}
120
		}
121
	}
122
	$finscript = "";
123
	foreach ($statistics as $q) {
124
		if ($stat_type == "0") {
125
			$packet_s = round(100 * ($q->pps / $bigger_stat), 0);
126
		} else {
127
			$packet_s = round(100 * ($q->bandwidth / $bigger_stat), 0);
128
		}
129
		if ($packet_s < 0) {
130
			$packet_s = 0;
131
		}
132
		$finscript .= "$('#queue{$q->queuename}width').css('width','{$packet_s}%');";
133
		$finscript .= "$('#queue{$q->queuename}pps').val('" . number_format($q->pps, 1) . "');";
134
		$finscript .= "$('#queue{$q->queuename}bps').val('" . format_bits($q->bandwidth) . "');";
135
		$finscript .= "$('#queue{$q->queuename}borrows').val('{$q->borrows}');";
136
		$finscript .= "$('#queue{$q->queuename}suspends').val('{$q->suspends}');";
137
		$finscript .= "$('#queue{$q->queuename}drops').val('{$q->drops}');";
138
		$finscript .= "$('#queue{$q->queuename}length').val('{$q->queuelength}');";
139
	}
140
	unset($statistics, $altqstats);
141
	header("Content-type: text/javascript");
142
	echo $finscript;
143
	exit;
144
}
145
$pgtitle = array(gettext("Status"), gettext("Queues"));
146
$shortcut_section = "trafficshaper";
147
include("head.inc");
148

    
149
if (!is_array($config['shaper']['queue']) || count($config['shaper']['queue']) < 1) {
150
	print_info_box(gettext("Traffic shaping is not configured."));
151
	include("foot.inc");
152
	exit;
153
}
154

    
155
if (!$error): ?>
156
<form action="status_queues.php" method="post">
157
<script type="text/javascript">
158
//<![CDATA[
159
events.push(function() {
160

    
161
	function getqueueactivity() {
162
		var url = "/status_queues.php";
163
		var pars = "getactivity=yes&stats=" + $("#selStatistic").val();
164
		$.ajax(
165
			url,
166
			{
167
				type: 'post',
168
				data: pars,
169
				complete: activitycallback
170
			});
171
	}
172

    
173
	function activitycallback(transport) {
174
		setTimeout(getqueueactivity, 5100);
175
	}
176

    
177
	$(document).ready(function() {
178
		setTimeout(getqueueactivity, 150);
179
	});
180
});
181
//]]>
182
</script>
183
<?php endif;
184

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

    
227
<script type="text/javascript">
228
//<![CDATA[
229
	function StatsShowHide(classname) {
230
		var firstrow = $("." + classname).first();
231
		if (firstrow.is(':visible')) {
232
			$("." + classname).hide();
233
		} else {
234
			$("." + classname).show();
235
		}
236
	}
237
//]]>
238
</script>
239
</form>
240
<?php
241

    
242
include("foot.inc");
243

    
244
function processQueues($altqstats, $level, $parent_name) {
245
	global $g;
246
	global $if_queue_list;
247

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

    
298
function statsQueues($xml) {
299
	global $statistics;
300

    
301
	$current = new QueueStats();
302
	$child = new QueueStats();
303
	$current->queuename = $xml['name'] . $xml['interface'];
304
	$current->queuelength = $xml['qlength'];
305
	$current->pps = $xml['measured'];
306
	$current->bandwidth = $xml['measuredspeedint'];
307
	$current->borrows = intval($xml['borrows']);
308
	$current->suspends = intval($xml['suspends']);
309
	$current->drops = intval($xml['droppedpkts']);
310
	if (is_array($xml['queue'])) {
311
		foreach ($xml['queue'] as $q) {
312
			$child = statsQueues($q);
313
			$current->pps += $child->pps;
314
			$current->bandwidth += $child->bandwidth;
315
			$current->borrows += $child->borrows;
316
			$current->suspends += $child->suspends;
317
			$current->drops += $child->drops;
318
		}
319
	}
320
	unset($child);
321
	$statistics[] = $current;
322
	return $current;
323
}
324
function format_bits($bits) {
325
	if ($bits >= 1000000000) {
326
		return sprintf("%.2f Gbps", $bits/1000000000);
327
	} else if ($bits >= 1000000) {
328
		return sprintf("%.2f Mbps", $bits/1000000);
329
	} else if ($bits >= 1000) {
330
		return sprintf("%.2f Kbps", $bits/1000);
331
	} else {
332
		return sprintf("%d bps", $bits);
333
	}
334
}
335
?>
(180-180/229)