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" && $bigger_stat != "0") {
125
			$packet_s = round(100 * ($q->pps / $bigger_stat), 0);
126
		} else if ($bigger_stat != "0") {
127
			$packet_s = round(100 * ($q->bandwidth / $bigger_stat), 0);
128
		} else {
129
			$packet_s = 0;
130
		}
131
		$finscript .= "$('#queue{$q->queuename}width').css('width','{$packet_s}%');";
132
		$finscript .= "$('#queue{$q->queuename}pps').val('" . number_format($q->pps, 1) . "');";
133
		$finscript .= "$('#queue{$q->queuename}bps').val('" . format_bits($q->bandwidth) . "');";
134
		$finscript .= "$('#queue{$q->queuename}borrows').val('{$q->borrows}');";
135
		$finscript .= "$('#queue{$q->queuename}suspends').val('{$q->suspends}');";
136
		$finscript .= "$('#queue{$q->queuename}drops').val('{$q->drops}');";
137
		$finscript .= "$('#queue{$q->queuename}length').val('{$q->queuelength}');";
138
	}
139
	unset($statistics, $altqstats);
140
	header("Content-type: text/javascript");
141
	echo $finscript;
142
	exit;
143
}
144
$pgtitle = array(gettext("Status"), gettext("Queues"));
145
$shortcut_section = "trafficshaper";
146
include("head.inc");
147

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

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

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

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

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

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

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

    
241
include("foot.inc");
242

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

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

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

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