Project

General

Profile

Download (9.8 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
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
##|+PRIV
23
##|*IDENT=page-status-trafficshaper-queues
24
##|*NAME=Status: Traffic Shaper: Queues
25
##|*DESCR=Allow access to the 'Status: Traffic Shaper: Queues' page.
26
##|*MATCH=status_queues.php*
27
##|-PRIV
28
/*
29
header("Last-Modified: " . gmdate("D, j M Y H:i:s") . " GMT");
30
header("Expires: " . gmdate("D, j M Y H:i:s", time()) . " GMT");
31
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP/1.1
32
header("Pragma: no-cache"); // HTTP/1.0
33
*/
34

    
35
require_once("guiconfig.inc");
36
class QueueStats {
37
	public $queuename;
38
	public $queuelength;
39
	public $pps;
40
	public $bandwidth;
41
	public $borrows;
42
	public $suspends;
43
	public $drops;
44
}
45
if (!file_exists("{$g['varrun_path']}/qstats.pid") || !isvalidpid("{$g['varrun_path']}/qstats.pid")) {
46
	/* Start in the background so we don't hang up the GUI */
47
	mwexec_bg("/usr/local/sbin/qstats -p {$g['varrun_path']}/qstats.pid");
48
	/* Give it a moment to start up */
49
	sleep(1);
50
}
51
$fd = @fsockopen("unix://{$g['varrun_path']}/qstats");
52
if (!$fd) {
53
	$error = gettext("Something wrong happened during communication with stat gathering.");
54
} else {
55
	$stats = "";
56
	while (!feof($fd)) {
57
		$stats .= fread($fd, 4096);
58
	}
59
	fclose($fd);
60
	@file_put_contents("{$g['tmp_path']}/qstats", $stats);
61
	$altqstats = @parse_xml_config("{$g['tmp_path']}/qstats", array("altqstats"));
62
	if ($altqstats == -1) {
63
		$error = gettext("No queue statistics could be read.");
64
	}
65
}
66
if ($_REQUEST['getactivity']) {
67
	$statistics = array();
68
	$bigger_stat = 0;
69
	$stat_type = $_REQUEST['stats'];
70
	/* build the queue stats. */
71
	foreach ($altqstats['queue'] as $q) {
72
		statsQueues($q);
73
	}
74
	/* calculate the bigger amount of packets or bandwidth being moved through all queues. */
75
	if ($stat_type == "0") {
76
		foreach ($statistics as $q) {
77
			if ($bigger_stat < $q->pps) {
78
				$bigger_stat = $q->pps;
79
			}
80
		}
81
	} else {
82
		foreach ($statistics as $q) {
83
			if ($bigger_stat < $q->bandwidth) {
84
				$bigger_stat = $q->bandwidth;
85
			}
86
		}
87
	}
88
	$finscript = "";
89
	foreach ($statistics as $q) {
90
		if ($stat_type == "0" && $bigger_stat != "0") {
91
			$packet_s = round(100 * ($q->pps / $bigger_stat), 0);
92
		} else if ($bigger_stat != "0") {
93
			$packet_s = round(100 * ($q->bandwidth / $bigger_stat), 0);
94
		} else {
95
			$packet_s = 0;
96
		}
97
		$finscript .= "$('#queue{$q->queuename}width').css('width','{$packet_s}%');";
98
		$finscript .= "$('#queue{$q->queuename}pps').val('" . number_format($q->pps, 1) . "');";
99
		$finscript .= "$('#queue{$q->queuename}bps').val('" . format_bits($q->bandwidth) . "');";
100
		$finscript .= "$('#queue{$q->queuename}borrows').val('{$q->borrows}');";
101
		$finscript .= "$('#queue{$q->queuename}suspends').val('{$q->suspends}');";
102
		$finscript .= "$('#queue{$q->queuename}drops').val('{$q->drops}');";
103
		$finscript .= "$('#queue{$q->queuename}length').val('{$q->queuelength}');";
104
	}
105
	unset($statistics, $altqstats);
106
	header("Content-type: text/javascript");
107
	echo $finscript;
108
	exit;
109
}
110
$pgtitle = array(gettext("Status"), gettext("Queues"));
111
$shortcut_section = "trafficshaper";
112
include("head.inc");
113

    
114
if (!is_array($config['shaper']['queue']) || count($config['shaper']['queue']) < 1) {
115
	print_info_box(gettext("Traffic shaping is not configured."));
116
	include("foot.inc");
117
	exit;
118
}
119

    
120
if (!$error): ?>
121
<form action="status_queues.php" method="post">
122
<script type="text/javascript">
123
//<![CDATA[
124
events.push(function() {
125

    
126
	function getqueueactivity() {
127
		var url = "/status_queues.php";
128
		var pars = "getactivity=yes&stats=" + $("#selStatistic").val();
129
		$.ajax(
130
			url,
131
			{
132
				type: 'post',
133
				data: pars,
134
				complete: activitycallback
135
			});
136
	}
137

    
138
	function activitycallback(transport) {
139
		setTimeout(getqueueactivity, 5100);
140
	}
141

    
142
	$(document).ready(function() {
143
		setTimeout(getqueueactivity, 150);
144
	});
145
});
146
//]]>
147
</script>
148
<?php endif;
149

    
150
if ($error):
151
	print_info_box($error);
152
else: ?>
153
	<div class="panel panel-default">
154
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("Status Queues"); ?></h2></div>
155
		<div class="panel-body table-responsive">
156
			<table class="table table-striped table-hover">
157
				<thead>
158
					<tr>
159
						<th><?=gettext("Queue"); ?></th>
160
						<th><?=gettext("Statistics"); ?>
161
							<select id="selStatistic" class="form-control">
162
								<option value="0"><?=gettext("PPS");?></option>
163
								<option value="1"><?=gettext("Bandwidth");?></option>
164
							</select>
165
						</th>
166
						<th><?=gettext("PPS"); ?></th>
167
						<th><?=gettext("Bandwidth"); ?></th>
168
						<th><?=gettext("Borrows"); ?></th>
169
						<th><?=gettext("Suspends"); ?></th>
170
						<th><?=gettext("Drops"); ?></th>
171
						<th><?=gettext("Length"); ?></th>
172
					</tr>
173
				</thead>
174
				<tbody>
175
<?php
176
	$if_queue_list = get_configured_interface_list_by_realif(false, true);
177
	processQueues($altqstats, 0, "");
178
?>
179
<?php endif; ?>
180
				</tbody>
181
			</table>
182
			<br />
183
			<div class="infoblock blockopen">
184
<?php
185
	print_info_box(gettext("Queue graphs take 5 seconds to sample data."), 'info', false);
186
?>
187
			</div>
188
		</div>
189
	</div>
190
<br/>
191

    
192
<script type="text/javascript">
193
//<![CDATA[
194
	function StatsShowHide(classname) {
195
		var firstrow = $("." + classname).first();
196
		if (firstrow.is(':visible')) {
197
			$("." + classname).hide();
198
		} else {
199
			$("." + classname).show();
200
		}
201
	}
202
//]]>
203
</script>
204
</form>
205
<?php
206

    
207
include("foot.inc");
208

    
209
function processQueues($altqstats, $level, $parent_name) {
210
	global $g;
211
	global $if_queue_list;
212

    
213
	$parent_name = $parent_name . " queuerow" . $altqstats['name'] . $altqstats['interface'];
214
	$prev_if = $altqstats['interface'];
215
	foreach ($altqstats['queue'] as $q) {
216
		$if_name = "";
217
		foreach ($if_queue_list as $oif => $real_name) {
218
			if ($oif == $q['interface']) {
219
				$if_name = $real_name;
220
				break;
221
			}
222
		}
223
		if ($prev_if != $q['interface']) {
224
			echo "<tr><td colspan=\"8\"><b>Interface " . htmlspecialchars(convert_real_interface_to_friendly_descr($q['interface'])) . "</b></td></tr>\n";
225
			$prev_if = $q['interface'];
226
		}
227
?>
228
		<tr class="<?=$parent_name;?>">
229
			<td class="<?=$row_class?>" style="padding-left:<?=$level * 20?>px;">
230
				<?php
231
				if (is_array($q['queue'])) {
232
					echo "<a href=\"#\" onclick=\"StatsShowHide('queuerow{$q['name']}{$q['interface']}');return false\">+/-</a>";
233
				}
234
				if (strstr($q['name'], "root_")) {
235
					echo "<a href=\"firewall_shaper.php?interface={$if_name}&amp;queue={$if_name}&amp;action=show\">Root queue</a>";
236
				} else {
237
					echo "<a href=\"firewall_shaper.php?interface={$if_name}&amp;queue={$q['name']}&amp;action=show\">" . htmlspecialchars($q['name']) . "</a>";
238
				}
239
				?>
240
			</td>
241
<?php
242
		$cpuUsage = 0;
243
		print('<td>');
244
		print('<div class="progress" style="height: 7px;width: 170px;">');
245
		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>');
246
		print('	  </div>');
247
		print('</td>');
248
		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>');
249
		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>');
250
		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>');
251
		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>');
252
		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>');
253
		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>');
254
?>
255
		</tr>
256
<?php
257
		if (is_array($q['queue'])) {
258
			processQueues($q, $level + 1, $parent_name);
259
		}
260
	};
261
}
262

    
263
function statsQueues($xml) {
264
	global $statistics;
265

    
266
	$current = new QueueStats();
267
	$child = new QueueStats();
268
	$current->queuename = $xml['name'] . $xml['interface'];
269
	$current->queuelength = $xml['qlength'];
270
	$current->pps = $xml['measured'];
271
	$current->bandwidth = $xml['measuredspeedint'];
272
	$current->borrows = intval($xml['borrows']);
273
	$current->suspends = intval($xml['suspends']);
274
	$current->drops = intval($xml['droppedpkts']);
275
	if (is_array($xml['queue'])) {
276
		foreach ($xml['queue'] as $q) {
277
			$child = statsQueues($q);
278
			$current->pps += $child->pps;
279
			$current->bandwidth += $child->bandwidth;
280
			$current->borrows += $child->borrows;
281
			$current->suspends += $child->suspends;
282
			$current->drops += $child->drops;
283
		}
284
	}
285
	unset($child);
286
	$statistics[] = $current;
287
	return $current;
288
}
289
function format_bits($bits) {
290
	if ($bits >= 1000000000) {
291
		return sprintf("%.2f Gbps", $bits/1000000000);
292
	} else if ($bits >= 1000000) {
293
		return sprintf("%.2f Mbps", $bits/1000000);
294
	} else if ($bits >= 1000) {
295
		return sprintf("%.2f Kbps", $bits/1000);
296
	} else {
297
		return sprintf("%d bps", $bits);
298
	}
299
}
300
?>
(180-180/225)