Project

General

Profile

Download (15.9 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"); ?> (<?=gettext($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
var scale_type_text = '<?=gettext($scale_type); ?>';
214

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

    
220
	fetch_data();
221
}
222

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

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

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

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

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

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

    
256
	if (!isNumber(ifin) || !isNumber(ifout)) {
257
		return handle_error();
258
	}
259

    
260
	var diff_ugmt  = ugmt - last_ugmt;
261
	var diff_ifin  = ifin - last_ifin;
262
	var diff_ifout = ifout - last_ifout;
263

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

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

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

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

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

    
322
	var rmax;  // max, rounded up
323

    
324
	if (unit == 'bits') {
325
		/* round up max, such that
326
		   100 kbps -> 200 kbps -> 400 kbps -> 800 kbps -> 1 Mbps -> 2 Mbps -> ... */
327
		rmax = 12500;
328
		i = 0;
329
		while (max > rmax) {
330
			i++;
331
			if (i && (i % 4 == 0)) {
332
				rmax *= 1.25;
333
			} else {
334
				rmax *= 2;
335
			}
336
		}
337
	} else {
338
		/* round up max, such that
339
		   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 ... */
340
		rmax = 10240;
341
		i = 0;
342
		while (max > rmax) {
343
			i++;
344
			if (i && (i % 4 == 0)) {
345
				rmax *= 1.25;
346
			} else {
347
				rmax *= 2;
348
			}
349

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

    
356
	scale = <?=$height?> / rmax;
357

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

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

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

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

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

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

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

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

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

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

    
426
    ]]>
427
	</script>
428
</svg>
(59-59/225)