Project

General

Profile

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

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

    
68
require("globals.inc");
69
require("guiconfig.inc");
70

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

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

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

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

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

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

    
117
//Error text if we cannot fetch data : depends on which method is used
118
$error_text = "Cannot get data about interface " . htmlspecialchars($ifnum);
119

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

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

    
125
/* check for custom theme colors */
126
if (file_exists("/usr/local/www/themes/{$g['theme']}/graph.php")) {
127
	$themetxt = file_get_contents("/usr/local/www/themes/{$g['theme']}/graph.php");
128
	eval($themetxt);
129
}
130

    
131
/********* Graph DATA **************/
132
print('<?xml version="1.0" encoding="UTF-8"?>' . "\n");?>
133
<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)">
134
	<g id="graph">
135
		<rect id="bg" x1="0" y1="0" width="100%" height="100%" fill="white"/>
136
		<line id="axis_x" x1="0" y1="0" x2="0" y2="100%" <?=$attribs['axis']?>/>
137
		<line id="axis_y" x1="0" y1="100%" x2="100%" y2="100%" <?=$attribs['axis']?>/>
138
		<path id="graph_out" d="M0 <?=$height?> L 0 <?=$height?>" <?=$attribs['graph_out']?>/>
139
		<path id="graph_in" d="M0 <?=$height?> L 0 <?=$height?>" <?=$attribs['graph_in']?>/>
140
		<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']?>/>
141
		<text id="grid_txt1" x="<?=$width?>" y="<?=$height/4*1?>" <?=$attribs['grid_txt']?> text-anchor="end"> </text>
142
		<text id="grid_txt2" x="<?=$width?>" y="<?=$height/4*2?>" <?=$attribs['grid_txt']?> text-anchor="end"> </text>
143
		<text id="grid_txt3" x="<?=$width?>" y="<?=$height/4*3?>" <?=$attribs['grid_txt']?> text-anchor="end"> </text>
144
		<text id="graph_in_lbl" x="5" y="8" <?=$attribs['in']?>><?=gettext("In"); ?></text>
145
		<text id="graph_out_lbl" x="5" y="16" <?=$attribs['out']?>><?=gettext("Out"); ?></text>
146
		<text id="graph_in_txt" x="20" y="8" <?=$attribs['in']?>> </text>
147
		<text id="graph_out_txt" x="20" y="16" <?=$attribs['out']?>> </text>
148
		<text id="ifname" x="<?=$width?>" y="8" <?=$attribs['graphname']?> text-anchor="end"><?=htmlspecialchars($ifname)?></text>
149
		<text id="switch_unit" x="<?=$width*0.55?>" y="5" <?=$attribs['switch_unit']?>><?=gettext("Switch to bytes/s"); ?></text>
150
		<text id="switch_scale" x="<?=$width*0.55?>" y="11" <?=$attribs['switch_scale']?>><?=gettext("AutoScale"); ?> (<?=$scale_type?>)</text>
151
		<text id="date" x="<?=$width*0.33?>" y="5" <?=$attribs['legend']?>> </text>
152
		<text id="time" x="<?=$width*0.33?>" y="11" <?=$attribs['legend']?>> </text>
153
		<text id="graphlast" x="<?=$width*0.55?>" y="17" <?=$attribs['legend']?>><?=gettext("Graph shows last"); ?> <?=$time_interval*$nb_plot?> <?=gettext("seconds"); ?></text>
154
		<polygon id="axis_arrow_x" <?=$attribs['axis']?> points="<?=($width) . "," . ($height)?> <?=($width-2) . "," . ($height-2)?> <?=($width-2) . "," . $height?>"/>
155
		<text id="error" x="<?=$width*0.5?>" y="<?=$height*0.5?>" visibility="hidden" <?=$attribs['error']?> text-anchor="middle"><?=$error_text?></text>
156
		<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>
157
	</g>
158
	<script type="text/ecmascript">
159
	<![CDATA[
160

    
161
/**
162
 * getURL is a proprietary Adobe function, but it's simplicity has made it very
163
 * popular. If getURL is undefined we spin our own by wrapping XMLHttpRequest.
164
 */
165
if (typeof getURL == 'undefined') {
166
	getURL = function(url, callback) {
167
		if (!url) {
168
			throw '<?=gettext("No URL for getURL"); ?>';
169
		}
170

    
171
		try {
172
			if (typeof callback.operationComplete == 'function') {
173
				callback = callback.operationComplete;
174
			}
175
		} catch (e) {}
176
		if (typeof callback != 'function') {
177
			throw '<?=gettext("No callback function for getURL"); ?>';
178
		}
179

    
180
		var http_request = null;
181
		if (typeof XMLHttpRequest != 'undefined') {
182
			http_request = new XMLHttpRequest();
183
		} else if (typeof ActiveXObject != 'undefined') {
184
			try {
185
				http_request = new ActiveXObject('Msxml2.XMLHTTP');
186
			} catch (e) {
187
				try {
188
				http_request = new ActiveXObject('Microsoft.XMLHTTP');
189
				} catch (e) {}
190
			}
191
		}
192
		if (!http_request) {
193
			throw '<?=gettext("Both getURL and XMLHttpRequest are undefined"); ?>';
194
		}
195

    
196
		http_request.onreadystatechange = function() {
197
			if (http_request.readyState == 4) {
198
				callback( { success : true,
199
					content : http_request.responseText,
200
					contentType : http_request.getResponseHeader("Content-Type") } );
201
			}
202
		}
203
		http_request.open('GET', url, true);
204
		http_request.send(null);
205
	}
206
}
207

    
208
var SVGDoc = null;
209
var last_ifin = 0;
210
var last_ifout = 0;
211
var last_ugmt = 0;
212
var max = 0;
213
var plot_in = new Array();
214
var plot_out = new Array();
215

    
216
var max_num_points = <?=$nb_plot?>;  // maximum number of plot data points
217
var step = <?=$width?> / max_num_points ;
218
var unit = 'bits';
219
var scale_type = '<?=$scale_type?>';
220

    
221
function init(evt) {
222
	SVGDoc = evt.target.ownerDocument;
223
	SVGDoc.getElementById("switch_unit").addEventListener("mousedown", switch_unit, false);
224
	SVGDoc.getElementById("switch_scale").addEventListener("mousedown", switch_scale, false);
225

    
226
	fetch_data();
227
}
228

    
229
function switch_unit(event) {
230
	SVGDoc.getElementById('switch_unit').firstChild.data = '<?=gettext("Switch to"); ?> ' + unit + '/s';
231
	unit = (unit == 'bits') ? 'bytes' : 'bits';
232
}
233

    
234
function switch_scale(event) {
235
	scale_type = (scale_type == 'up') ? '<?=gettext("follow"); ?>' : '<?=gettext("up"); ?>';
236
	SVGDoc.getElementById('switch_scale').firstChild.data = 'AutoScale (' + scale_type + ')';
237
}
238

    
239
function fetch_data() {
240
	getURL('<?=$fetch_link?>', plot_data);
241
}
242

    
243
function plot_data(obj) {
244
	// Show datetimelegend
245
	var now = new Date();
246
	var time = LZ(now.getHours()) + ":" + LZ(now.getMinutes()) + ":" + LZ(now.getSeconds());
247
	SVGDoc.getElementById('time').firstChild.data = time;
248
	var date = (now.getMonth()+1) + "/" + now.getDate() + "/" + now.getFullYear();
249
	SVGDoc.getElementById('date').firstChild.data = date;
250

    
251
	if (!obj.success) {
252
		return handle_error();  // getURL failed to get data
253
	}
254

    
255
	var t = obj.content.split("|");
256
	var ugmt = parseFloat(t[0]);  // ugmt is an unixtimestamp style
257
	var ifin = parseInt(t[1], 10);    // number of bytes received by the interface
258
	var ifout = parseInt(t[2], 10);   // number of bytes sent by the interface
259
	var scale;
260

    
261
	if (!isNumber(ifin) || !isNumber(ifout)) {
262
		return handle_error();
263
	}
264

    
265
	var diff_ugmt  = ugmt - last_ugmt;
266
	var diff_ifin  = ifin - last_ifin;
267
	var diff_ifout = ifout - last_ifout;
268

    
269
	if (diff_ugmt == 0) {
270
		diff_ugmt = 1;  /* avoid division by zero */
271
	}
272

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

    
298
	plot_in[plot_in.length] = diff_ifin / diff_ugmt;
299
	plot_out[plot_out.length]= diff_ifout / diff_ugmt;
300
	var index_plot = plot_in.length - 1;
301

    
302
	SVGDoc.getElementById('graph_in_txt').firstChild.data = formatSpeed(plot_in[index_plot], unit);
303
	SVGDoc.getElementById('graph_out_txt').firstChild.data = formatSpeed(plot_out[index_plot], unit);
304

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

    
327
	var rmax;  // max, rounded up
328

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

    
355
			if (i == 8) {
356
				rmax *= 1.024;
357
			}
358
		}
359
	}
360

    
361
	scale = <?=$height?> / rmax;
362

    
363
	/* change labels accordingly */
364
	SVGDoc.getElementById('grid_txt1').firstChild.data = formatSpeed(3*rmax/4, unit);
365
	SVGDoc.getElementById('grid_txt2').firstChild.data = formatSpeed(2*rmax/4, unit);
366
	SVGDoc.getElementById('grid_txt3').firstChild.data = formatSpeed(rmax/4, unit);
367

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

    
378
	SVGDoc.getElementById('error').setAttributeNS(null, 'visibility', 'hidden');
379
	SVGDoc.getElementById('graph_in').setAttributeNS(null, 'd', path_in);
380
	SVGDoc.getElementById('graph_out').setAttributeNS(null, 'd', path_out);
381

    
382
	setTimeout('fetch_data()', <?=1000*$time_interval?>);
383
}
384

    
385
function handle_error() {
386
	SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'visible');
387
	setTimeout('fetch_data()', <?=1000*$time_interval?>);
388
}
389

    
390
function isNumber(a) {
391
	return typeof a == 'number' && isFinite(a);
392
}
393

    
394
function formatSpeed(speed, unit) {
395
	if (unit == 'bits') {
396
		return formatSpeedBits(speed);
397
	}
398
	if (unit == 'bytes') {
399
		return formatSpeedBytes(speed);
400
	}
401
}
402

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

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

    
427
function LZ(x) {
428
	return (x < 0 || x > 9 ? "" : "0") + x;
429
}
430

    
431
    ]]>
432
	</script>
433
</svg>
(75-75/235)