Project

General

Profile

Download (7.28 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * graph_cpu.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2023 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2004-2006 T. Lechat <dev@lechat.org>
10
 * Copyright (c) 2004-2006 Jonathan Watt <jwatt@jwatt.org>
11
 * All rights reserved.
12
 *
13
 * originally based on m0n0wall (http://m0n0.ch/wall)
14
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
15
 * All rights reserved.
16
 *
17
 * Licensed under the Apache License, Version 2.0 (the "License");
18
 * you may not use this file except in compliance with the License.
19
 * You may obtain a copy of the License at
20
 *
21
 * http://www.apache.org/licenses/LICENSE-2.0
22
 *
23
 * Unless required by applicable law or agreed to in writing, software
24
 * distributed under the License is distributed on an "AS IS" BASIS,
25
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26
 * See the License for the specific language governing permissions and
27
 * limitations under the License.
28
 */
29

    
30
##|+PRIV
31
##|*IDENT=page-diagnostics-cpuutilization
32
##|*NAME=Diagnostics: CPU Utilization
33
##|*DESCR=Allow access to the 'Diagnostics: CPU Utilization' page.
34
##|*MATCH=graph_cpu.php*
35
##|*MATCH=stats.php*
36
##|-PRIV
37

    
38
require_once("guiconfig.inc");
39

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

    
46
/********* Other conf *******/
47

    
48
$nb_plot = 120;  // maximum number of data points to plot in the graph
49
$fetch_link = "stats.php?stats=cpu";
50

    
51
//SVG attributes
52
$attribs['axis']='fill="black" stroke="black"';
53
$attribs['cpu']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="7"';
54
$attribs['graph_cpu']='fill="none" stroke="#FF0000" stroke-opacity="0.8"';
55
$attribs['legend']='fill="black" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
56
$attribs['grid_txt']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="6"';
57
$attribs['grid']='stroke="gray" stroke-opacity="0.5"';
58
$attribs['error']='fill="blue" font-family="Arial" font-size="4"';
59
$attribs['collect_initial']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
60

    
61
$height=100;  // SVG internal height : do not modify
62
$width=200;   // SVG internal width  : do not modify
63

    
64
/********* Graph DATA **************/
65
print('<?xml version="1.0" encoding="UTF-8"?>' . "\n");?>
66
<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);">
67
	<g id="graph">
68
		<rect id="bg" x1="0" y1="0" width="100%" height="100%" fill="white"/>
69
		<line id="axis_x" x1="0" y1="0" x2="0" y2="100%" <?=$attribs['axis']?>/>
70
		<line id="axis_y" x1="0" y1="100%" x2="100%" y2="100%" <?=$attribs['axis']?>/>
71
		<polygon id="axis_arrow_x" <?=$attribs['axis']?> points="<?=($width) . "," . ($height)?> <?=($width-2) . "," . ($height-2)?> <?=($width-2) . "," . $height?>"/>
72
		<path id="graph_cpu" d="" <?=$attribs['graph_cpu']?>/>
73
		<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']?>/>
74
		<text id="grid_txt1" x="100%" y="25%" <?=$attribs['grid_txt']?> text-anchor="end">75%</text>
75
		<text id="grid_txt2" x="100%" y="50%" <?=$attribs['grid_txt']?> text-anchor="end">50%</text>
76
		<text id="grid_txt3" x="100%" y="75%" <?=$attribs['grid_txt']?> text-anchor="end">25%</text>
77
		<text id="graph_cpu_txt" x="4" y="8" <?=$attribs['cpu']?>> </text>
78
		<text id="error" x="50%" y="50%" visibility="hidden" <?=$attribs['error']?> text-anchor="middle"><?=gettext("Cannot get CPU load"); ?></text>
79
		<text id="collect_initial" x="50%" y="50%" visibility="hidden" <?=$attribs['collect_initial']?> text-anchor="middle"><?=gettext("Collecting initial data, please wait"); ?>...</text>
80
	</g>
81
	<script type="text/ecmascript">
82
		<![CDATA[
83

    
84
/**
85
 * getURL is a proprietary Adobe function, but it's simplicity has made it very
86
 * popular. If getURL is undefined we spin our own by wrapping XMLHttpRequest.
87
 */
88
if (typeof getURL == 'undefined') {
89
	getURL = function(url, callback) {
90
		if (!url) {
91
			throw '<?=gettext("No URL for getURL"); ?>';
92
		}
93

    
94
		try {
95
			if (typeof callback.operationComplete == 'function') {
96
				callback = callback.operationComplete;
97
			}
98
		} catch (e) {}
99
		if (typeof callback != 'function') {
100
			throw '<?=gettext("No callback function for getURL"); ?>';
101
		}
102

    
103
		var http_request = null;
104
		if (typeof XMLHttpRequest != 'undefined') {
105
			http_request = new XMLHttpRequest();
106
		} else if (typeof ActiveXObject != 'undefined') {
107
			try {
108
				http_request = new ActiveXObject('Msxml2.XMLHTTP');
109
			} catch (e) {
110
				try {
111
					http_request = new ActiveXObject('Microsoft.XMLHTTP');
112
				} catch (e) {}
113
			}
114
		}
115
		if (!http_request) {
116
			throw '<?=gettext("Both getURL and XMLHttpRequest are undefined"); ?>';
117
		}
118

    
119
		http_request.onreadystatechange = function() {
120
			if (http_request.readyState == 4) {
121
				callback( { success : true,
122
					content : http_request.responseText,
123
					contentType : http_request.getResponseHeader("Content-Type") } );
124
			}
125
		}
126
		http_request.open('GET', url, true);
127
		http_request.send(null);
128
	}
129
}
130

    
131
var SVGDoc = null;
132
var last_cpu_total = 0;
133
var last_cpu_idle = 0;
134
var diff_cpu_total = 0;
135
var diff_cpu_idle = 0;
136
var cpu_data = new Array();
137

    
138
var max_num_points = <?=$nb_plot?>;  // maximum number of plot data points
139
var step = <?=$width?> / max_num_points;  // plot X division size
140
var scale = <?=$height?> / 100;
141

    
142
function init(evt) {
143
	SVGDoc = evt.target.ownerDocument;
144
	fetch_data();
145
}
146

    
147
function fetch_data() {
148
	getURL('<?=$fetch_link?>', plot_cpu_data);
149
}
150

    
151
function plot_cpu_data(obj) {
152
	if (!obj.success) {
153
		return handle_error();  // getURL failed to get current CPU load data
154
	}
155

    
156
	var cpu = parseInt(obj.content);
157
	if (!isNumber(cpu)) {
158
		return handle_error();
159
	}
160

    
161
	switch (cpu_data.length) {
162
		case 0:
163
			SVGDoc.getElementById("collect_initial").setAttributeNS(null, 'visibility', 'visible');
164
			cpu_data[0] = cpu;
165
			fetch_data();
166
			return;
167
		case 1:
168
			SVGDoc.getElementById("collect_initial").setAttributeNS(null, 'visibility', 'hidden');
169
			break;
170
		case max_num_points:
171
			// shift plot to left if the maximum number of plot points has been reached
172
			var i = 0;
173
			while (i < max_num_points) {
174
				cpu_data[i] = cpu_data[++i];
175
			}
176
			--cpu_data.length;
177
	}
178

    
179
	cpu_data[cpu_data.length] = cpu;
180

    
181
	var path_data = "M 0 " + (<?=$height?> - (cpu_data[0] * scale));
182
	for (var i = 1; i < cpu_data.length; ++i) {
183
		var x = step * i;
184
		var y_cpu = <?=$height?> - (cpu_data[i] * scale);
185
		path_data += " L" + x + " " + y_cpu;
186
	}
187

    
188
	SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'hidden');
189
	SVGDoc.getElementById('graph_cpu_txt').firstChild.data = cpu + '%';
190
	SVGDoc.getElementById('graph_cpu').setAttributeNS(null, "d", path_data);
191

    
192
	fetch_data();
193
}
194

    
195
function handle_error() {
196
	SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'visible');
197
	fetch_data();
198
}
199

    
200
function isNumber(a) {
201
	return typeof a == 'number' && isFinite(a);
202
}
203

    
204
		]]>
205
	</script>
206
</svg>
(65-65/228)