Project

General

Profile

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

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

    
71
require_once("guiconfig.inc");
72

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

    
79
/********* Other conf *******/
80

    
81
$nb_plot = 120;  // maximum number of data points to plot in the graph
82
$fetch_link = "stats.php?stats=cpu";
83

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

    
94
$height=100;  // SVG internal height : do not modify
95
$width=200;   // SVG internal width  : do not modify
96

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

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

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

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

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

    
164
var SVGDoc = null;
165
var last_cpu_total = 0;
166
var last_cpu_idle = 0;
167
var diff_cpu_total = 0;
168
var diff_cpu_idle = 0;
169
var cpu_data = new Array();
170

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

    
175
function init(evt) {
176
	SVGDoc = evt.target.ownerDocument;
177
	fetch_data();
178
}
179

    
180
function fetch_data() {
181
	getURL('<?=$fetch_link?>', plot_cpu_data);
182
}
183

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

    
189
	var cpu = parseInt(obj.content);
190
	if (!isNumber(cpu)) {
191
		return handle_error();
192
	}
193

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

    
212
	cpu_data[cpu_data.length] = cpu;
213

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

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

    
225
	fetch_data();
226
}
227

    
228
function handle_error() {
229
	SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'visible');
230
	fetch_data();
231
}
232

    
233
function isNumber(a) {
234
	return typeof a == 'number' && isFinite(a);
235
}
236

    
237
		]]>
238
	</script>
239
</svg>
(76-76/234)