Project

General

Profile

Download (7.19 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-2016 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2004-2006 T. Lechat <dev@lechat.org>
8
 * Copyright (c) 2004-2006 Jonathan Watt <jwatt@jwatt.org>
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

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

    
36
require_once("guiconfig.inc");
37

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

    
44
/********* Other conf *******/
45

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
177
	cpu_data[cpu_data.length] = cpu;
178

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

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

    
190
	fetch_data();
191
}
192

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

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

    
202
		]]>
203
	</script>
204
</svg>
(61-61/225)