Project

General

Profile

Download (7.28 KB) Statistics
| Branch: | Tag: | Revision:
1 7c5833d7 Scott Ullrich
<?php
2 fd9ebcd5 Stephen Beaver
/*
3 c5d81585 Renato Botelho
 * graph_cpu.php
4 fd9ebcd5 Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8 8f585441 Luiz Souza
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
9 c5d81585 Renato Botelho
 * Copyright (c) 2004-2006 T. Lechat <dev@lechat.org>
10
 * Copyright (c) 2004-2006 Jonathan Watt <jwatt@jwatt.org>
11
 * All rights reserved.
12 9da2cf1c Stephen Beaver
 *
13 c5d81585 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
14
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
15
 * All rights reserved.
16 fd9ebcd5 Stephen Beaver
 *
17 b12ea3fb Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 *
21 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
22 fd9ebcd5 Stephen Beaver
 *
23 b12ea3fb Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 */
29 7c5833d7 Scott Ullrich
30 6b07c15a Matthew Grooms
##|+PRIV
31
##|*IDENT=page-diagnostics-cpuutilization
32 5230f468 jim-p
##|*NAME=Diagnostics: CPU Utilization
33 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: CPU Utilization' page.
34
##|*MATCH=graph_cpu.php*
35 9599211d jim-p
##|*MATCH=stats.php*
36 6b07c15a Matthew Grooms
##|-PRIV
37
38 de4333ba Ermal
require_once("guiconfig.inc");
39
40 6c07db48 Phil Davis
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 16e6485b Ermal Luçi
header("Pragma: no-cache"); // HTTP/1.0
44 7c5833d7 Scott Ullrich
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 216cdefa Scott Ullrich
$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 7c5833d7 Scott Ullrich
$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 cd94a9a8 Renato Botelho
print('<?xml version="1.0" encoding="UTF-8"?>' . "\n");?>
66 7c5833d7 Scott Ullrich
<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 6c07db48 Phil Davis
	<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 7c5833d7 Scott Ullrich
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 6c07db48 Phil Davis
	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 7c5833d7 Scott Ullrich
}
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 6c07db48 Phil Davis
	SVGDoc = evt.target.ownerDocument;
144
	fetch_data();
145 7c5833d7 Scott Ullrich
}
146
147
function fetch_data() {
148 6c07db48 Phil Davis
	getURL('<?=$fetch_link?>', plot_cpu_data);
149 7c5833d7 Scott Ullrich
}
150
151
function plot_cpu_data(obj) {
152 6c07db48 Phil Davis
	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 7c5833d7 Scott Ullrich
}
194
195
function handle_error() {
196 6c07db48 Phil Davis
	SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'visible');
197
	fetch_data();
198 7c5833d7 Scott Ullrich
}
199
200
function isNumber(a) {
201 6c07db48 Phil Davis
	return typeof a == 'number' && isFinite(a);
202 7c5833d7 Scott Ullrich
}
203
204 6c07db48 Phil Davis
		]]>
205
	</script>
206 7c5833d7 Scott Ullrich
</svg>