Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * status_queues.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright notice,
13
 *    this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. All advertising materials mentioning features or use of this software
21
 *    must display the following acknowledgment:
22
 *    "This product includes software developed by the pfSense Project
23
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
24
 *
25
 * 4. The names "pfSense" and "pfSense Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    coreteam@pfsense.org.
29
 *
30
 * 5. Products derived from this software may not be called "pfSense"
31
 *    nor may "pfSense" appear in their names without prior written
32
 *    permission of the Electric Sheep Fencing, LLC.
33
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *
37
 * "This product includes software developed by the pfSense Project
38
 * for use in the pfSense software distribution (http://www.pfsense.org/).
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 * OF THE POSSIBILITY OF SUCH DAMAGE.
52
 */
53

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

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

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

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

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

    
170
	function activitycallback(transport) {
171
		setTimeout(getqueueactivity, 5100);
172
	}
173

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

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

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

    
239
include("foot.inc");
240

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

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

    
295
function statsQueues($xml) {
296
	global $statistics;
297

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