Project

General

Profile

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

    
28
##|+PRIV
29
##|*IDENT=page-diagnostics-interfacetraffic
30
##|*NAME=Diagnostics: Interface Traffic
31
##|*DESCR=Allow access to the 'Diagnostics: Interface Traffic' page.
32
##|*MATCH=graph.php*
33
##|-PRIV
34

    
35
require_once("globals.inc");
36
require_once("guiconfig.inc");
37

    
38
header("Last-Modified: " . gmdate("D, j M Y H:i:s") . " GMT");
39
header("Expires: " . gmdate("D, j M Y H:i:s", time()) . " GMT");
40
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP/1.1
41
header("Pragma: no-cache"); // HTTP/1.0
42
header("Content-type: image/svg+xml");
43

    
44
/********** HTTP GET Based Conf ***********/
45
$ifnum = @$_GET["ifnum"];  // BSD / SNMP interface name / number
46
$ifnum = get_real_interface($ifnum);
47
$ifname = @$_GET["ifname"]?$_GET["ifname"]:"Interface $ifnum";  //Interface name that will be showed on top right of graph
48

    
49
/********* Other conf *******/
50
if (isset($config["widgets"]["trafficgraphs"]["scale_type"])) {
51
	$scale_type = $config["widgets"]["trafficgraphs"]["scale_type"];
52
} else {
53
	$scale_type = "up";
54
}
55

    
56
$nb_plot=120;                   //NB plot in graph
57
if ($_GET["timeint"]) {
58
	$time_interval = $_GET["timeint"];		//Refresh time Interval
59
} else {
60
	$time_interval = 3;
61
}
62

    
63
if ($_GET["initdelay"]) {
64
	$init_delay = $_GET["initdelay"];		//Initial Delay
65
} else {
66
	$init_delay = 3;
67
}
68

    
69
//SVG attributes
70
$attribs['axis']='fill="black" stroke="black"';
71
$attribs['in']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="7"';
72
$attribs['out']='fill="#000000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="7"';
73
$attribs['graph_in']='fill="none" stroke="#FF0000" stroke-opacity="0.8"';
74
$attribs['graph_out']='fill="none" stroke="#000000" stroke-opacity="0.8"';
75
$attribs['legend']='fill="black" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
76
$attribs['graphname']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="8"';
77
$attribs['grid_txt']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="6"';
78
$attribs['grid']='stroke="gray" stroke-opacity="0.5"';
79
$attribs['switch_unit']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4" text-decoration="underline"';
80
$attribs['switch_scale']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4" text-decoration="underline"';
81
$attribs['error']='fill="blue" font-family="Arial" font-size="4"';
82
$attribs['collect_initial']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
83

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

    
87
$height=100;            //SVG internal height : do not modify
88
$width=200;             //SVG internal width : do not modify
89

    
90
$fetch_link = "ifstats.php?if=" . htmlspecialchars($ifnum);
91

    
92
/********* Graph DATA **************/
93
print('<?xml version="1.0" encoding="UTF-8"?>' . "\n");?>
94
<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)">
95
	<g id="graph">
96
		<rect id="bg" x1="0" y1="0" width="100%" height="100%" fill="white"/>
97
		<line id="axis_x" x1="0" y1="0" x2="0" y2="100%" <?=$attribs['axis']?>/>
98
		<line id="axis_y" x1="0" y1="100%" x2="100%" y2="100%" <?=$attribs['axis']?>/>
99
		<path id="graph_out" d="M0 <?=$height?> L 0 <?=$height?>" <?=$attribs['graph_out']?>/>
100
		<path id="graph_in" d="M0 <?=$height?> L 0 <?=$height?>" <?=$attribs['graph_in']?>/>
101
		<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']?>/>
102
		<text id="grid_txt1" x="<?=$width?>" y="<?=$height/4*1?>" <?=$attribs['grid_txt']?> text-anchor="end"> </text>
103
		<text id="grid_txt2" x="<?=$width?>" y="<?=$height/4*2?>" <?=$attribs['grid_txt']?> text-anchor="end"> </text>
104
		<text id="grid_txt3" x="<?=$width?>" y="<?=$height/4*3?>" <?=$attribs['grid_txt']?> text-anchor="end"> </text>
105
		<text id="graph_in_lbl" x="5" y="8" <?=$attribs['in']?>><?=gettext("In"); ?></text>
106
		<text id="graph_out_lbl" x="5" y="16" <?=$attribs['out']?>><?=gettext("Out"); ?></text>
107
		<text id="graph_in_txt" x="20" y="8" <?=$attribs['in']?>> </text>
108
		<text id="graph_out_txt" x="20" y="16" <?=$attribs['out']?>> </text>
109
		<text id="ifname" x="<?=$width?>" y="8" <?=$attribs['graphname']?> text-anchor="end"><?=htmlspecialchars($ifname)?></text>
110
		<text id="switch_unit" x="<?=$width*0.55?>" y="5" <?=$attribs['switch_unit']?>><?=gettext("Switch to bytes/s"); ?></text>
111
		<text id="switch_scale" x="<?=$width*0.55?>" y="11" <?=$attribs['switch_scale']?>><?=gettext("AutoScale"); ?> (<?=$scale_type?>)</text>
112
		<text id="date" x="<?=$width*0.33?>" y="5" <?=$attribs['legend']?>> </text>
113
		<text id="time" x="<?=$width*0.33?>" y="11" <?=$attribs['legend']?>> </text>
114
		<text id="graphlast" x="<?=$width*0.55?>" y="17" <?=$attribs['legend']?>><?=sprintf(gettext("Graph shows last %s seconds"), $time_interval*$nb_plot)?></text>
115
		<polygon id="axis_arrow_x" <?=$attribs['axis']?> points="<?=($width) . "," . ($height)?> <?=($width-2) . "," . ($height-2)?> <?=($width-2) . "," . $height?>"/>
116
		<text id="error" x="<?=$width*0.5?>" y="<?=$height*0.5?>" visibility="hidden" <?=$attribs['error']?> text-anchor="middle"><?=$error_text?></text>
117
		<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>
118
	</g>
119
	<script type="text/ecmascript">
120
	<![CDATA[
121

    
122
/**
123
 * getURL is a proprietary Adobe function, but it's simplicity has made it very
124
 * popular. If getURL is undefined we spin our own by wrapping XMLHttpRequest.
125
 */
126
if (typeof getURL == 'undefined') {
127
	getURL = function(url, callback) {
128
		if (!url) {
129
			throw '<?=gettext("No URL for getURL"); ?>';
130
		}
131

    
132
		try {
133
			if (typeof callback.operationComplete == 'function') {
134
				callback = callback.operationComplete;
135
			}
136
		} catch (e) {}
137
		if (typeof callback != 'function') {
138
			throw '<?=gettext("No callback function for getURL"); ?>';
139
		}
140

    
141
		var http_request = null;
142
		if (typeof XMLHttpRequest != 'undefined') {
143
			http_request = new XMLHttpRequest();
144
		} else if (typeof ActiveXObject != 'undefined') {
145
			try {
146
				http_request = new ActiveXObject('Msxml2.XMLHTTP');
147
			} catch (e) {
148
				try {
149
				http_request = new ActiveXObject('Microsoft.XMLHTTP');
150
				} catch (e) {}
151
			}
152
		}
153
		if (!http_request) {
154
			throw '<?=gettext("Both getURL and XMLHttpRequest are undefined"); ?>';
155
		}
156

    
157
		http_request.onreadystatechange = function() {
158
			if (http_request.readyState == 4) {
159
				callback( { success : true,
160
					content : http_request.responseText,
161
					contentType : http_request.getResponseHeader("Content-Type") } );
162
			}
163
		}
164
		http_request.open('GET', url, true);
165
		http_request.send(null);
166
	}
167
}
168

    
169
var SVGDoc = null;
170
var last_ifin = 0;
171
var last_ifout = 0;
172
var last_ugmt = 0;
173
var max = 0;
174
var plot_in = new Array();
175
var plot_out = new Array();
176

    
177
var max_num_points = <?=$nb_plot?>;  // maximum number of plot data points
178
var step = <?=$width?> / max_num_points ;
179
var unit = 'bits';
180
var scale_type = '<?=$scale_type?>';
181

    
182
function init(evt) {
183
	SVGDoc = evt.target.ownerDocument;
184
	SVGDoc.getElementById("switch_unit").addEventListener("mousedown", switch_unit, false);
185
	SVGDoc.getElementById("switch_scale").addEventListener("mousedown", switch_scale, false);
186

    
187
	fetch_data();
188
}
189

    
190
function switch_unit(event) {
191
	SVGDoc.getElementById('switch_unit').firstChild.data = '<?=gettext("Switch to"); ?> ' + unit + '/s';
192
	unit = (unit == 'bits') ? 'bytes' : 'bits';
193
}
194

    
195
function switch_scale(event) {
196
	scale_type = (scale_type == 'up') ? '<?=gettext("follow"); ?>' : '<?=gettext("up"); ?>';
197
	SVGDoc.getElementById('switch_scale').firstChild.data = 'AutoScale (' + scale_type + ')';
198
}
199

    
200
function fetch_data() {
201
	getURL('<?=$fetch_link?>', plot_data);
202
}
203

    
204
function plot_data(obj) {
205
	// Show datetimelegend
206
	var now = new Date();
207
	var time = LZ(now.getHours()) + ":" + LZ(now.getMinutes()) + ":" + LZ(now.getSeconds());
208
	SVGDoc.getElementById('time').firstChild.data = time;
209
	var date = (now.getMonth()+1) + "/" + now.getDate() + "/" + now.getFullYear();
210
	SVGDoc.getElementById('date').firstChild.data = date;
211

    
212
	if (!obj.success) {
213
		return handle_error();  // getURL failed to get data
214
	}
215

    
216
	var t = obj.content.split("|");
217
	var ugmt = parseFloat(t[0]);  // ugmt is an unixtimestamp style
218
	var ifin = parseInt(t[1], 10);    // number of bytes received by the interface
219
	var ifout = parseInt(t[2], 10);   // number of bytes sent by the interface
220
	var scale;
221

    
222
	if (!isNumber(ifin) || !isNumber(ifout)) {
223
		return handle_error();
224
	}
225

    
226
	var diff_ugmt  = ugmt - last_ugmt;
227
	var diff_ifin  = ifin - last_ifin;
228
	var diff_ifout = ifout - last_ifout;
229

    
230
	if (diff_ugmt == 0) {
231
		diff_ugmt = 1;  /* avoid division by zero */
232
	}
233

    
234
	last_ugmt = ugmt;
235
	last_ifin = ifin;
236
	last_ifout = ifout;
237
	var graphTimerId = 0;
238
	switch (plot_in.length) {
239
		case 0:
240
			SVGDoc.getElementById("collect_initial").setAttributeNS(null, 'visibility', 'visible');
241
			plot_in[0] = diff_ifin / diff_ugmt;
242
			plot_out[0] = diff_ifout / diff_ugmt;
243
			setTimeout('fetch_data()', <?=1000*($time_interval + $init_delay)?>);
244
			return;
245
		case 1:
246
			SVGDoc.getElementById("collect_initial").setAttributeNS(null, 'visibility', 'hidden');
247
			break;
248
		case max_num_points:
249
			// shift plot to left if the maximum number of plot points has been reached
250
			var i = 0;
251
			while (i < max_num_points) {
252
				plot_in[i] = plot_in[i+1];
253
				plot_out[i] = plot_out[++i];
254
			}
255
			plot_in.length--;
256
			plot_out.length--;
257
	}
258

    
259
	plot_in[plot_in.length] = diff_ifin / diff_ugmt;
260
	plot_out[plot_out.length]= diff_ifout / diff_ugmt;
261
	var index_plot = plot_in.length - 1;
262

    
263
	SVGDoc.getElementById('graph_in_txt').firstChild.data = formatSpeed(plot_in[index_plot], unit);
264
	SVGDoc.getElementById('graph_out_txt').firstChild.data = formatSpeed(plot_out[index_plot], unit);
265

    
266
	/* determine peak for sensible scaling */
267
	if (scale_type == 'up') {
268
		if (plot_in[index_plot] > max) {
269
			max = plot_in[index_plot];
270
		}
271
		if (plot_out[index_plot] > max) {
272
			max = plot_out[index_plot];
273
		}
274
	} else if (scale_type == 'follow') {
275
		i = 0;
276
		max = 0;
277
		while (i < plot_in.length) {
278
			if (plot_in[i] > max) {
279
				max = plot_in[i];
280
			}
281
			if (plot_out[i] > max) {
282
				max = plot_out[i];
283
			}
284
			i++;
285
		}
286
	}
287

    
288
	var rmax;  // max, rounded up
289

    
290
	if (unit == 'bits') {
291
		/* round up max, such that
292
		   100 kbps -> 200 kbps -> 400 kbps -> 800 kbps -> 1 Mbps -> 2 Mbps -> ... */
293
		rmax = 12500;
294
		i = 0;
295
		while (max > rmax) {
296
			i++;
297
			if (i && (i % 4 == 0)) {
298
				rmax *= 1.25;
299
			} else {
300
				rmax *= 2;
301
			}
302
		}
303
	} else {
304
		/* round up max, such that
305
		   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 ... */
306
		rmax = 10240;
307
		i = 0;
308
		while (max > rmax) {
309
			i++;
310
			if (i && (i % 4 == 0)) {
311
				rmax *= 1.25;
312
			} else {
313
				rmax *= 2;
314
			}
315

    
316
			if (i == 8) {
317
				rmax *= 1.024;
318
			}
319
		}
320
	}
321

    
322
	scale = <?=$height?> / rmax;
323

    
324
	/* change labels accordingly */
325
	SVGDoc.getElementById('grid_txt1').firstChild.data = formatSpeed(3*rmax/4, unit);
326
	SVGDoc.getElementById('grid_txt2').firstChild.data = formatSpeed(2*rmax/4, unit);
327
	SVGDoc.getElementById('grid_txt3').firstChild.data = formatSpeed(rmax/4, unit);
328

    
329
	var path_in = "M 0 " + (<?=$height?> - (plot_in[0] * scale));
330
	var path_out = "M 0 " + (<?=$height?> - (plot_out[0] * scale));
331
	for (i = 1; i < plot_in.length; i++) {
332
		var x = step * i;
333
		var y_in = <?=$height?> - (plot_in[i] * scale);
334
		var y_out = <?=$height?> - (plot_out[i] * scale);
335
		path_in += " L" + x + " " + y_in;
336
		path_out += " L" + x + " " + y_out;
337
	}
338

    
339
	SVGDoc.getElementById('error').setAttributeNS(null, 'visibility', 'hidden');
340
	SVGDoc.getElementById('graph_in').setAttributeNS(null, 'd', path_in);
341
	SVGDoc.getElementById('graph_out').setAttributeNS(null, 'd', path_out);
342

    
343
	setTimeout('fetch_data()', <?=1000*$time_interval?>);
344
}
345

    
346
function handle_error() {
347
	SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'visible');
348
	setTimeout('fetch_data()', <?=1000*$time_interval?>);
349
}
350

    
351
function isNumber(a) {
352
	return typeof a == 'number' && isFinite(a);
353
}
354

    
355
function formatSpeed(speed, unit) {
356
	if (unit == 'bits') {
357
		return formatSpeedBits(speed);
358
	}
359
	if (unit == 'bytes') {
360
		return formatSpeedBytes(speed);
361
	}
362
}
363

    
364
function formatSpeedBits(speed) {
365
	// format speed in bits/sec, input: bytes/sec
366
	if (speed < 125000) {
367
		return Math.round(speed / 125) + " <?=gettext("Kbps"); ?>";
368
	}
369
	if (speed < 125000000) {
370
		return Math.round(speed / 1250)/100 + " <?=gettext("Mbps"); ?>";
371
	}
372
	// else
373
	return Math.round(speed / 1250000)/100 + " <?=gettext("Gbps"); ?>";  /* wow! */
374
}
375

    
376
function formatSpeedBytes(speed) {
377
	// format speed in bytes/sec, input:  bytes/sec
378
	if (speed < 1048576) {
379
		return Math.round(speed / 10.24)/100 + " <?=gettext("KB/s"); ?>";
380
	}
381
	if (speed < 1073741824) {
382
		return Math.round(speed / 10485.76)/100 + " <?=gettext("MB/s"); ?>";
383
	}
384
	// else
385
	return Math.round(speed / 10737418.24)/100 + " <?=gettext("GB/s"); ?>";  /* wow! */
386
}
387

    
388
function LZ(x) {
389
	return (x < 0 || x > 9 ? "" : "0") + x;
390
}
391

    
392
    ]]>
393
	</script>
394
</svg>
(61-61/227)