Project

General

Profile

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

    
60
##|+PRIV
61
##|*IDENT=page-diagnostics-interfacetraffic
62
##|*NAME=Diagnostics: Interface Traffic
63
##|*DESCR=Allow access to the 'Diagnostics: Interface Traffic' page.
64
##|*MATCH=graph.php*
65
##|-PRIV
66

    
67
require_once("globals.inc");
68
require_once("guiconfig.inc");
69

    
70
header("Last-Modified: " . gmdate("D, j M Y H:i:s") . " GMT");
71
header("Expires: " . gmdate("D, j M Y H:i:s", time()) . " GMT");
72
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP/1.1
73
header("Pragma: no-cache"); // HTTP/1.0
74
header("Content-type: image/svg+xml");
75

    
76
/********** HTTP GET Based Conf ***********/
77
$ifnum = @$_GET["ifnum"];  // BSD / SNMP interface name / number
78
$ifnum = get_real_interface($ifnum);
79
$ifname = @$_GET["ifname"]?$_GET["ifname"]:"Interface $ifnum";  //Interface name that will be showed on top right of graph
80

    
81
/********* Other conf *******/
82
if (isset($config["widgets"]["trafficgraphs"]["scale_type"])) {
83
	$scale_type = $config["widgets"]["trafficgraphs"]["scale_type"];
84
} else {
85
	$scale_type = "up";
86
}
87

    
88
$nb_plot=120;                   //NB plot in graph
89
if ($_GET["timeint"]) {
90
	$time_interval = $_GET["timeint"];		//Refresh time Interval
91
} else {
92
	$time_interval = 3;
93
}
94

    
95
if ($_GET["initdelay"]) {
96
	$init_delay = $_GET["initdelay"];		//Initial Delay
97
} else {
98
	$init_delay = 3;
99
}
100

    
101
//SVG attributes
102
$attribs['axis']='fill="black" stroke="black"';
103
$attribs['in']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="7"';
104
$attribs['out']='fill="#000000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="7"';
105
$attribs['graph_in']='fill="none" stroke="#FF0000" stroke-opacity="0.8"';
106
$attribs['graph_out']='fill="none" stroke="#000000" stroke-opacity="0.8"';
107
$attribs['legend']='fill="black" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
108
$attribs['graphname']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="8"';
109
$attribs['grid_txt']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="6"';
110
$attribs['grid']='stroke="gray" stroke-opacity="0.5"';
111
$attribs['switch_unit']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4" text-decoration="underline"';
112
$attribs['switch_scale']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4" text-decoration="underline"';
113
$attribs['error']='fill="blue" font-family="Arial" font-size="4"';
114
$attribs['collect_initial']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
115

    
116
//Error text if we cannot fetch data : depends on which method is used
117
$error_text = sprintf(gettext("Cannot get data about interface %s"), htmlspecialchars($ifnum));
118

    
119
$height=100;            //SVG internal height : do not modify
120
$width=200;             //SVG internal width : do not modify
121

    
122
$fetch_link = "ifstats.php?if=" . htmlspecialchars($ifnum);
123

    
124
/********* Graph DATA **************/
125
print('<?xml version="1.0" encoding="UTF-8"?>' . "\n");?>
126
<svg width="100%" height="100%" viewBox="0 0 <?=$width?> <?=$height?>" preserveAspectRatio="none" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" onload="init(evt)">
127
	<g id="graph">
128
		<rect id="bg" x1="0" y1="0" width="100%" height="100%" fill="white"/>
129
		<line id="axis_x" x1="0" y1="0" x2="0" y2="100%" <?=$attribs['axis']?>/>
130
		<line id="axis_y" x1="0" y1="100%" x2="100%" y2="100%" <?=$attribs['axis']?>/>
131
		<path id="graph_out" d="M0 <?=$height?> L 0 <?=$height?>" <?=$attribs['graph_out']?>/>
132
		<path id="graph_in" d="M0 <?=$height?> L 0 <?=$height?>" <?=$attribs['graph_in']?>/>
133
		<path id="grid" d="M0 <?=$height/4*1?> L <?=$width?> <?=$height/4*1?> M0 <?=$height/4*2?> L <?=$width?> <?=$height/4*2?> M0 <?=$height/4*3?> L <?=$width?> <?=$height/4*3?>" <?=$attribs['grid']?>/>
134
		<text id="grid_txt1" x="<?=$width?>" y="<?=$height/4*1?>" <?=$attribs['grid_txt']?> text-anchor="end"> </text>
135
		<text id="grid_txt2" x="<?=$width?>" y="<?=$height/4*2?>" <?=$attribs['grid_txt']?> text-anchor="end"> </text>
136
		<text id="grid_txt3" x="<?=$width?>" y="<?=$height/4*3?>" <?=$attribs['grid_txt']?> text-anchor="end"> </text>
137
		<text id="graph_in_lbl" x="5" y="8" <?=$attribs['in']?>><?=gettext("In"); ?></text>
138
		<text id="graph_out_lbl" x="5" y="16" <?=$attribs['out']?>><?=gettext("Out"); ?></text>
139
		<text id="graph_in_txt" x="20" y="8" <?=$attribs['in']?>> </text>
140
		<text id="graph_out_txt" x="20" y="16" <?=$attribs['out']?>> </text>
141
		<text id="ifname" x="<?=$width?>" y="8" <?=$attribs['graphname']?> text-anchor="end"><?=htmlspecialchars($ifname)?></text>
142
		<text id="switch_unit" x="<?=$width*0.55?>" y="5" <?=$attribs['switch_unit']?>><?=gettext("Switch to bytes/s"); ?></text>
143
		<text id="switch_scale" x="<?=$width*0.55?>" y="11" <?=$attribs['switch_scale']?>><?=gettext("AutoScale"); ?> (<?=$scale_type?>)</text>
144
		<text id="date" x="<?=$width*0.33?>" y="5" <?=$attribs['legend']?>> </text>
145
		<text id="time" x="<?=$width*0.33?>" y="11" <?=$attribs['legend']?>> </text>
146
		<text id="graphlast" x="<?=$width*0.55?>" y="17" <?=$attribs['legend']?>><?=sprintf(gettext("Graph shows last %s seconds"), $time_interval*$nb_plot)?></text>
147
		<polygon id="axis_arrow_x" <?=$attribs['axis']?> points="<?=($width) . "," . ($height)?> <?=($width-2) . "," . ($height-2)?> <?=($width-2) . "," . $height?>"/>
148
		<text id="error" x="<?=$width*0.5?>" y="<?=$height*0.5?>" visibility="hidden" <?=$attribs['error']?> text-anchor="middle"><?=$error_text?></text>
149
		<text id="collect_initial" x="<?=$width*0.5?>" y="<?=$height*0.5?>" visibility="hidden" <?=$attribs['collect_initial']?> text-anchor="middle"><?=gettext("Collecting initial data, please wait"); ?>...</text>
150
	</g>
151
	<script type="text/ecmascript">
152
	<![CDATA[
153

    
154
/**
155
 * getURL is a proprietary Adobe function, but it's simplicity has made it very
156
 * popular. If getURL is undefined we spin our own by wrapping XMLHttpRequest.
157
 */
158
if (typeof getURL == 'undefined') {
159
	getURL = function(url, callback) {
160
		if (!url) {
161
			throw '<?=gettext("No URL for getURL"); ?>';
162
		}
163

    
164
		try {
165
			if (typeof callback.operationComplete == 'function') {
166
				callback = callback.operationComplete;
167
			}
168
		} catch (e) {}
169
		if (typeof callback != 'function') {
170
			throw '<?=gettext("No callback function for getURL"); ?>';
171
		}
172

    
173
		var http_request = null;
174
		if (typeof XMLHttpRequest != 'undefined') {
175
			http_request = new XMLHttpRequest();
176
		} else if (typeof ActiveXObject != 'undefined') {
177
			try {
178
				http_request = new ActiveXObject('Msxml2.XMLHTTP');
179
			} catch (e) {
180
				try {
181
				http_request = new ActiveXObject('Microsoft.XMLHTTP');
182
				} catch (e) {}
183
			}
184
		}
185
		if (!http_request) {
186
			throw '<?=gettext("Both getURL and XMLHttpRequest are undefined"); ?>';
187
		}
188

    
189
		http_request.onreadystatechange = function() {
190
			if (http_request.readyState == 4) {
191
				callback( { success : true,
192
					content : http_request.responseText,
193
					contentType : http_request.getResponseHeader("Content-Type") } );
194
			}
195
		}
196
		http_request.open('GET', url, true);
197
		http_request.send(null);
198
	}
199
}
200

    
201
var SVGDoc = null;
202
var last_ifin = 0;
203
var last_ifout = 0;
204
var last_ugmt = 0;
205
var max = 0;
206
var plot_in = new Array();
207
var plot_out = new Array();
208

    
209
var max_num_points = <?=$nb_plot?>;  // maximum number of plot data points
210
var step = <?=$width?> / max_num_points ;
211
var unit = 'bits';
212
var scale_type = '<?=$scale_type?>';
213

    
214
function init(evt) {
215
	SVGDoc = evt.target.ownerDocument;
216
	SVGDoc.getElementById("switch_unit").addEventListener("mousedown", switch_unit, false);
217
	SVGDoc.getElementById("switch_scale").addEventListener("mousedown", switch_scale, false);
218

    
219
	fetch_data();
220
}
221

    
222
function switch_unit(event) {
223
	SVGDoc.getElementById('switch_unit').firstChild.data = '<?=gettext("Switch to"); ?> ' + unit + '/s';
224
	unit = (unit == 'bits') ? 'bytes' : 'bits';
225
}
226

    
227
function switch_scale(event) {
228
	scale_type = (scale_type == 'up') ? '<?=gettext("follow"); ?>' : '<?=gettext("up"); ?>';
229
	SVGDoc.getElementById('switch_scale').firstChild.data = 'AutoScale (' + scale_type + ')';
230
}
231

    
232
function fetch_data() {
233
	getURL('<?=$fetch_link?>', plot_data);
234
}
235

    
236
function plot_data(obj) {
237
	// Show datetimelegend
238
	var now = new Date();
239
	var time = LZ(now.getHours()) + ":" + LZ(now.getMinutes()) + ":" + LZ(now.getSeconds());
240
	SVGDoc.getElementById('time').firstChild.data = time;
241
	var date = (now.getMonth()+1) + "/" + now.getDate() + "/" + now.getFullYear();
242
	SVGDoc.getElementById('date').firstChild.data = date;
243

    
244
	if (!obj.success) {
245
		return handle_error();  // getURL failed to get data
246
	}
247

    
248
	var t = obj.content.split("|");
249
	var ugmt = parseFloat(t[0]);  // ugmt is an unixtimestamp style
250
	var ifin = parseInt(t[1], 10);    // number of bytes received by the interface
251
	var ifout = parseInt(t[2], 10);   // number of bytes sent by the interface
252
	var scale;
253

    
254
	if (!isNumber(ifin) || !isNumber(ifout)) {
255
		return handle_error();
256
	}
257

    
258
	var diff_ugmt  = ugmt - last_ugmt;
259
	var diff_ifin  = ifin - last_ifin;
260
	var diff_ifout = ifout - last_ifout;
261

    
262
	if (diff_ugmt == 0) {
263
		diff_ugmt = 1;  /* avoid division by zero */
264
	}
265

    
266
	last_ugmt = ugmt;
267
	last_ifin = ifin;
268
	last_ifout = ifout;
269
	var graphTimerId = 0;
270
	switch (plot_in.length) {
271
		case 0:
272
			SVGDoc.getElementById("collect_initial").setAttributeNS(null, 'visibility', 'visible');
273
			plot_in[0] = diff_ifin / diff_ugmt;
274
			plot_out[0] = diff_ifout / diff_ugmt;
275
			setTimeout('fetch_data()', <?=1000*($time_interval + $init_delay)?>);
276
			return;
277
		case 1:
278
			SVGDoc.getElementById("collect_initial").setAttributeNS(null, 'visibility', 'hidden');
279
			break;
280
		case max_num_points:
281
			// shift plot to left if the maximum number of plot points has been reached
282
			var i = 0;
283
			while (i < max_num_points) {
284
				plot_in[i] = plot_in[i+1];
285
				plot_out[i] = plot_out[++i];
286
			}
287
			plot_in.length--;
288
			plot_out.length--;
289
	}
290

    
291
	plot_in[plot_in.length] = diff_ifin / diff_ugmt;
292
	plot_out[plot_out.length]= diff_ifout / diff_ugmt;
293
	var index_plot = plot_in.length - 1;
294

    
295
	SVGDoc.getElementById('graph_in_txt').firstChild.data = formatSpeed(plot_in[index_plot], unit);
296
	SVGDoc.getElementById('graph_out_txt').firstChild.data = formatSpeed(plot_out[index_plot], unit);
297

    
298
	/* determine peak for sensible scaling */
299
	if (scale_type == 'up') {
300
		if (plot_in[index_plot] > max) {
301
			max = plot_in[index_plot];
302
		}
303
		if (plot_out[index_plot] > max) {
304
			max = plot_out[index_plot];
305
		}
306
	} else if (scale_type == 'follow') {
307
		i = 0;
308
		max = 0;
309
		while (i < plot_in.length) {
310
			if (plot_in[i] > max) {
311
				max = plot_in[i];
312
			}
313
			if (plot_out[i] > max) {
314
				max = plot_out[i];
315
			}
316
			i++;
317
		}
318
	}
319

    
320
	var rmax;  // max, rounded up
321

    
322
	if (unit == 'bits') {
323
		/* round up max, such that
324
		   100 kbps -> 200 kbps -> 400 kbps -> 800 kbps -> 1 Mbps -> 2 Mbps -> ... */
325
		rmax = 12500;
326
		i = 0;
327
		while (max > rmax) {
328
			i++;
329
			if (i && (i % 4 == 0)) {
330
				rmax *= 1.25;
331
			} else {
332
				rmax *= 2;
333
			}
334
		}
335
	} else {
336
		/* round up max, such that
337
		   10 KB/s -> 20 KB/s -> 40 KB/s -> 80 KB/s -> 100 KB/s -> 200 KB/s -> 400 KB/s -> 800 KB/s -> 1 MB/s ... */
338
		rmax = 10240;
339
		i = 0;
340
		while (max > rmax) {
341
			i++;
342
			if (i && (i % 4 == 0)) {
343
				rmax *= 1.25;
344
			} else {
345
				rmax *= 2;
346
			}
347

    
348
			if (i == 8) {
349
				rmax *= 1.024;
350
			}
351
		}
352
	}
353

    
354
	scale = <?=$height?> / rmax;
355

    
356
	/* change labels accordingly */
357
	SVGDoc.getElementById('grid_txt1').firstChild.data = formatSpeed(3*rmax/4, unit);
358
	SVGDoc.getElementById('grid_txt2').firstChild.data = formatSpeed(2*rmax/4, unit);
359
	SVGDoc.getElementById('grid_txt3').firstChild.data = formatSpeed(rmax/4, unit);
360

    
361
	var path_in = "M 0 " + (<?=$height?> - (plot_in[0] * scale));
362
	var path_out = "M 0 " + (<?=$height?> - (plot_out[0] * scale));
363
	for (i = 1; i < plot_in.length; i++) {
364
		var x = step * i;
365
		var y_in = <?=$height?> - (plot_in[i] * scale);
366
		var y_out = <?=$height?> - (plot_out[i] * scale);
367
		path_in += " L" + x + " " + y_in;
368
		path_out += " L" + x + " " + y_out;
369
	}
370

    
371
	SVGDoc.getElementById('error').setAttributeNS(null, 'visibility', 'hidden');
372
	SVGDoc.getElementById('graph_in').setAttributeNS(null, 'd', path_in);
373
	SVGDoc.getElementById('graph_out').setAttributeNS(null, 'd', path_out);
374

    
375
	setTimeout('fetch_data()', <?=1000*$time_interval?>);
376
}
377

    
378
function handle_error() {
379
	SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'visible');
380
	setTimeout('fetch_data()', <?=1000*$time_interval?>);
381
}
382

    
383
function isNumber(a) {
384
	return typeof a == 'number' && isFinite(a);
385
}
386

    
387
function formatSpeed(speed, unit) {
388
	if (unit == 'bits') {
389
		return formatSpeedBits(speed);
390
	}
391
	if (unit == 'bytes') {
392
		return formatSpeedBytes(speed);
393
	}
394
}
395

    
396
function formatSpeedBits(speed) {
397
	// format speed in bits/sec, input: bytes/sec
398
	if (speed < 125000) {
399
		return Math.round(speed / 125) + " <?=gettext("Kbps"); ?>";
400
	}
401
	if (speed < 125000000) {
402
		return Math.round(speed / 1250)/100 + " <?=gettext("Mbps"); ?>";
403
	}
404
	// else
405
	return Math.round(speed / 1250000)/100 + " <?=gettext("Gbps"); ?>";  /* wow! */
406
}
407

    
408
function formatSpeedBytes(speed) {
409
	// format speed in bytes/sec, input:  bytes/sec
410
	if (speed < 1048576) {
411
		return Math.round(speed / 10.24)/100 + " <?=gettext("KB/s"); ?>";
412
	}
413
	if (speed < 1073741824) {
414
		return Math.round(speed / 10485.76)/100 + " <?=gettext("MB/s"); ?>";
415
	}
416
	// else
417
	return Math.round(speed / 10737418.24)/100 + " <?=gettext("GB/s"); ?>";  /* wow! */
418
}
419

    
420
function LZ(x) {
421
	return (x < 0 || x > 9 ? "" : "0") + x;
422
}
423

    
424
    ]]>
425
	</script>
426
</svg>
(61-61/227)