Project

General

Profile

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

    
67
##|+PRIV
68
##|*IDENT=page-diagnostics-cpuutilization
69
##|*NAME=Diagnostics: CPU Utilization page
70
##|*DESCR=Allow access to the 'Diagnostics: CPU Utilization' page.
71
##|*MATCH=graph_cpu.php*
72
##|-PRIV
73

    
74
require_once("guiconfig.inc");
75

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

    
82
/********* Other conf *******/
83

    
84
$nb_plot = 120;  // maximum number of data points to plot in the graph
85
$fetch_link = "stats.php?stats=cpu";
86

    
87
//SVG attributes
88
$attribs['axis']='fill="black" stroke="black"';
89
$attribs['cpu']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="7"';
90
$attribs['graph_cpu']='fill="none" stroke="#FF0000" stroke-opacity="0.8"';
91
$attribs['legend']='fill="black" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
92
$attribs['grid_txt']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="6"';
93
$attribs['grid']='stroke="gray" stroke-opacity="0.5"';
94
$attribs['error']='fill="blue" font-family="Arial" font-size="4"';
95
$attribs['collect_initial']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
96

    
97
$height=100;  // SVG internal height : do not modify
98
$width=200;   // SVG internal width  : do not modify
99

    
100
/********* Graph DATA **************/
101
print('<?xml version="1.0" encoding="UTF-8"?>' . "\n");?>
102
<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);">
103
	<g id="graph">
104
		<rect id="bg" x1="0" y1="0" width="100%" height="100%" fill="white"/>
105
		<line id="axis_x" x1="0" y1="0" x2="0" y2="100%" <?=$attribs['axis']?>/>
106
		<line id="axis_y" x1="0" y1="100%" x2="100%" y2="100%" <?=$attribs['axis']?>/>
107
		<polygon id="axis_arrow_x" <?=$attribs['axis']?> points="<?=($width) . "," . ($height)?> <?=($width-2) . "," . ($height-2)?> <?=($width-2) . "," . $height?>"/>
108
		<path id="graph_cpu" d="" <?=$attribs['graph_cpu']?>/>
109
		<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']?>/>
110
		<text id="grid_txt1" x="100%" y="25%" <?=$attribs['grid_txt']?> text-anchor="end">75%</text>
111
		<text id="grid_txt2" x="100%" y="50%" <?=$attribs['grid_txt']?> text-anchor="end">50%</text>
112
		<text id="grid_txt3" x="100%" y="75%" <?=$attribs['grid_txt']?> text-anchor="end">25%</text>
113
		<text id="graph_cpu_txt" x="4" y="8" <?=$attribs['cpu']?>> </text>
114
		<text id="error" x="50%" y="50%" visibility="hidden" <?=$attribs['error']?> text-anchor="middle"><?=gettext("Cannot get CPU load"); ?></text>
115
		<text id="collect_initial" x="50%" y="50%" visibility="hidden" <?=$attribs['collect_initial']?> text-anchor="middle"><?=gettext("Collecting initial data, please wait"); ?>...</text>
116
	</g>
117
	<script type="text/ecmascript">
118
		<![CDATA[
119

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

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

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

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

    
167
var SVGDoc = null;
168
var last_cpu_total = 0;
169
var last_cpu_idle = 0;
170
var diff_cpu_total = 0;
171
var diff_cpu_idle = 0;
172
var cpu_data = new Array();
173

    
174
var max_num_points = <?=$nb_plot?>;  // maximum number of plot data points
175
var step = <?=$width?> / max_num_points;  // plot X division size
176
var scale = <?=$height?> / 100;
177

    
178
function init(evt) {
179
	SVGDoc = evt.target.ownerDocument;
180
	fetch_data();
181
}
182

    
183
function fetch_data() {
184
	getURL('<?=$fetch_link?>', plot_cpu_data);
185
}
186

    
187
function plot_cpu_data(obj) {
188
	if (!obj.success) {
189
		return handle_error();  // getURL failed to get current CPU load data
190
	}
191

    
192
	var cpu = parseInt(obj.content);
193
	if (!isNumber(cpu)) {
194
		return handle_error();
195
	}
196

    
197
	switch (cpu_data.length) {
198
		case 0:
199
			SVGDoc.getElementById("collect_initial").setAttributeNS(null, 'visibility', 'visible');
200
			cpu_data[0] = cpu;
201
			fetch_data();
202
			return;
203
		case 1:
204
			SVGDoc.getElementById("collect_initial").setAttributeNS(null, 'visibility', 'hidden');
205
			break;
206
		case max_num_points:
207
			// shift plot to left if the maximum number of plot points has been reached
208
			var i = 0;
209
			while (i < max_num_points) {
210
				cpu_data[i] = cpu_data[++i];
211
			}
212
			--cpu_data.length;
213
	}
214

    
215
	cpu_data[cpu_data.length] = cpu;
216

    
217
	var path_data = "M 0 " + (<?=$height?> - (cpu_data[0] * scale));
218
	for (var i = 1; i < cpu_data.length; ++i) {
219
		var x = step * i;
220
		var y_cpu = <?=$height?> - (cpu_data[i] * scale);
221
		path_data += " L" + x + " " + y_cpu;
222
	}
223

    
224
	SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'hidden');
225
	SVGDoc.getElementById('graph_cpu_txt').firstChild.data = cpu + '%';
226
	SVGDoc.getElementById('graph_cpu').setAttributeNS(null, "d", path_data);
227

    
228
	fetch_data();
229
}
230

    
231
function handle_error() {
232
	SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'visible');
233
	fetch_data();
234
}
235

    
236
function isNumber(a) {
237
	return typeof a == 'number' && isFinite(a);
238
}
239

    
240
		]]>
241
	</script>
242
</svg>
(75-75/234)