Project

General

Profile

Download (9.01 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	graph_cpu.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
 *  Some or all of this file is based on the m0n0wall project which is
11
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
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
##|+PRIV
62
##|*IDENT=page-diagnostics-cpuutilization
63
##|*NAME=Diagnostics: CPU Utilization
64
##|*DESCR=Allow access to the 'Diagnostics: CPU Utilization' page.
65
##|*MATCH=graph_cpu.php*
66
##|*MATCH=stats.php*
67
##|-PRIV
68

    
69
require_once("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
/********* Other conf *******/
78

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

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

    
92
$height=100;  // SVG internal height : do not modify
93
$width=200;   // SVG internal width  : do not modify
94

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

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

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

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

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

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

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

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

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

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

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

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

    
210
	cpu_data[cpu_data.length] = cpu;
211

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

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

    
223
	fetch_data();
224
}
225

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

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

    
235
		]]>
236
	</script>
237
</svg>
(63-63/228)