Project

General

Profile

Download (7.23 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-2024 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");
43
header("Content-type: image/svg+xml");
44

    
45
/********* Other conf *******/
46

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
178
	cpu_data[cpu_data.length] = cpu;
179

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

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

    
191
	fetch_data();
192
}
193

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

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

    
203
		]]>
204
	</script>
205
</svg>
(67-67/232)