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
|
pfSense_MODULE: graph
|
62
|
*/
|
63
|
|
64
|
##|+PRIV
|
65
|
##|*IDENT=page-diagnostics-cpuutilization
|
66
|
##|*NAME=Diagnostics: CPU Utilization
|
67
|
##|*DESCR=Allow access to the 'Diagnostics: CPU Utilization' page.
|
68
|
##|*MATCH=graph_cpu.php*
|
69
|
##|*MATCH=stats.php*
|
70
|
##|-PRIV
|
71
|
|
72
|
require_once("guiconfig.inc");
|
73
|
|
74
|
header("Last-Modified: " . gmdate("D, j M Y H:i:s") . " GMT");
|
75
|
header("Expires: " . gmdate("D, j M Y H:i:s", time()) . " GMT");
|
76
|
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP/1.1
|
77
|
header("Pragma: no-cache"); // HTTP/1.0
|
78
|
header("Content-type: image/svg+xml");
|
79
|
|
80
|
/********* Other conf *******/
|
81
|
|
82
|
$nb_plot = 120; // maximum number of data points to plot in the graph
|
83
|
$fetch_link = "stats.php?stats=cpu";
|
84
|
|
85
|
//SVG attributes
|
86
|
$attribs['axis']='fill="black" stroke="black"';
|
87
|
$attribs['cpu']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="7"';
|
88
|
$attribs['graph_cpu']='fill="none" stroke="#FF0000" stroke-opacity="0.8"';
|
89
|
$attribs['legend']='fill="black" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
|
90
|
$attribs['grid_txt']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="6"';
|
91
|
$attribs['grid']='stroke="gray" stroke-opacity="0.5"';
|
92
|
$attribs['error']='fill="blue" font-family="Arial" font-size="4"';
|
93
|
$attribs['collect_initial']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
|
94
|
|
95
|
$height=100; // SVG internal height : do not modify
|
96
|
$width=200; // SVG internal width : do not modify
|
97
|
|
98
|
/********* Graph DATA **************/
|
99
|
print('<?xml version="1.0" encoding="UTF-8"?>' . "\n");?>
|
100
|
<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);">
|
101
|
<g id="graph">
|
102
|
<rect id="bg" x1="0" y1="0" width="100%" height="100%" fill="white"/>
|
103
|
<line id="axis_x" x1="0" y1="0" x2="0" y2="100%" <?=$attribs['axis']?>/>
|
104
|
<line id="axis_y" x1="0" y1="100%" x2="100%" y2="100%" <?=$attribs['axis']?>/>
|
105
|
<polygon id="axis_arrow_x" <?=$attribs['axis']?> points="<?=($width) . "," . ($height)?> <?=($width-2) . "," . ($height-2)?> <?=($width-2) . "," . $height?>"/>
|
106
|
<path id="graph_cpu" d="" <?=$attribs['graph_cpu']?>/>
|
107
|
<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']?>/>
|
108
|
<text id="grid_txt1" x="100%" y="25%" <?=$attribs['grid_txt']?> text-anchor="end">75%</text>
|
109
|
<text id="grid_txt2" x="100%" y="50%" <?=$attribs['grid_txt']?> text-anchor="end">50%</text>
|
110
|
<text id="grid_txt3" x="100%" y="75%" <?=$attribs['grid_txt']?> text-anchor="end">25%</text>
|
111
|
<text id="graph_cpu_txt" x="4" y="8" <?=$attribs['cpu']?>> </text>
|
112
|
<text id="error" x="50%" y="50%" visibility="hidden" <?=$attribs['error']?> text-anchor="middle"><?=gettext("Cannot get CPU load"); ?></text>
|
113
|
<text id="collect_initial" x="50%" y="50%" visibility="hidden" <?=$attribs['collect_initial']?> text-anchor="middle"><?=gettext("Collecting initial data, please wait"); ?>...</text>
|
114
|
</g>
|
115
|
<script type="text/ecmascript">
|
116
|
<![CDATA[
|
117
|
|
118
|
/**
|
119
|
* getURL is a proprietary Adobe function, but it's simplicity has made it very
|
120
|
* popular. If getURL is undefined we spin our own by wrapping XMLHttpRequest.
|
121
|
*/
|
122
|
if (typeof getURL == 'undefined') {
|
123
|
getURL = function(url, callback) {
|
124
|
if (!url) {
|
125
|
throw '<?=gettext("No URL for getURL"); ?>';
|
126
|
}
|
127
|
|
128
|
try {
|
129
|
if (typeof callback.operationComplete == 'function') {
|
130
|
callback = callback.operationComplete;
|
131
|
}
|
132
|
} catch (e) {}
|
133
|
if (typeof callback != 'function') {
|
134
|
throw '<?=gettext("No callback function for getURL"); ?>';
|
135
|
}
|
136
|
|
137
|
var http_request = null;
|
138
|
if (typeof XMLHttpRequest != 'undefined') {
|
139
|
http_request = new XMLHttpRequest();
|
140
|
} else if (typeof ActiveXObject != 'undefined') {
|
141
|
try {
|
142
|
http_request = new ActiveXObject('Msxml2.XMLHTTP');
|
143
|
} catch (e) {
|
144
|
try {
|
145
|
http_request = new ActiveXObject('Microsoft.XMLHTTP');
|
146
|
} catch (e) {}
|
147
|
}
|
148
|
}
|
149
|
if (!http_request) {
|
150
|
throw '<?=gettext("Both getURL and XMLHttpRequest are undefined"); ?>';
|
151
|
}
|
152
|
|
153
|
http_request.onreadystatechange = function() {
|
154
|
if (http_request.readyState == 4) {
|
155
|
callback( { success : true,
|
156
|
content : http_request.responseText,
|
157
|
contentType : http_request.getResponseHeader("Content-Type") } );
|
158
|
}
|
159
|
}
|
160
|
http_request.open('GET', url, true);
|
161
|
http_request.send(null);
|
162
|
}
|
163
|
}
|
164
|
|
165
|
var SVGDoc = null;
|
166
|
var last_cpu_total = 0;
|
167
|
var last_cpu_idle = 0;
|
168
|
var diff_cpu_total = 0;
|
169
|
var diff_cpu_idle = 0;
|
170
|
var cpu_data = new Array();
|
171
|
|
172
|
var max_num_points = <?=$nb_plot?>; // maximum number of plot data points
|
173
|
var step = <?=$width?> / max_num_points; // plot X division size
|
174
|
var scale = <?=$height?> / 100;
|
175
|
|
176
|
function init(evt) {
|
177
|
SVGDoc = evt.target.ownerDocument;
|
178
|
fetch_data();
|
179
|
}
|
180
|
|
181
|
function fetch_data() {
|
182
|
getURL('<?=$fetch_link?>', plot_cpu_data);
|
183
|
}
|
184
|
|
185
|
function plot_cpu_data(obj) {
|
186
|
if (!obj.success) {
|
187
|
return handle_error(); // getURL failed to get current CPU load data
|
188
|
}
|
189
|
|
190
|
var cpu = parseInt(obj.content);
|
191
|
if (!isNumber(cpu)) {
|
192
|
return handle_error();
|
193
|
}
|
194
|
|
195
|
switch (cpu_data.length) {
|
196
|
case 0:
|
197
|
SVGDoc.getElementById("collect_initial").setAttributeNS(null, 'visibility', 'visible');
|
198
|
cpu_data[0] = cpu;
|
199
|
fetch_data();
|
200
|
return;
|
201
|
case 1:
|
202
|
SVGDoc.getElementById("collect_initial").setAttributeNS(null, 'visibility', 'hidden');
|
203
|
break;
|
204
|
case max_num_points:
|
205
|
// shift plot to left if the maximum number of plot points has been reached
|
206
|
var i = 0;
|
207
|
while (i < max_num_points) {
|
208
|
cpu_data[i] = cpu_data[++i];
|
209
|
}
|
210
|
--cpu_data.length;
|
211
|
}
|
212
|
|
213
|
cpu_data[cpu_data.length] = cpu;
|
214
|
|
215
|
var path_data = "M 0 " + (<?=$height?> - (cpu_data[0] * scale));
|
216
|
for (var i = 1; i < cpu_data.length; ++i) {
|
217
|
var x = step * i;
|
218
|
var y_cpu = <?=$height?> - (cpu_data[i] * scale);
|
219
|
path_data += " L" + x + " " + y_cpu;
|
220
|
}
|
221
|
|
222
|
SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'hidden');
|
223
|
SVGDoc.getElementById('graph_cpu_txt').firstChild.data = cpu + '%';
|
224
|
SVGDoc.getElementById('graph_cpu').setAttributeNS(null, "d", path_data);
|
225
|
|
226
|
fetch_data();
|
227
|
}
|
228
|
|
229
|
function handle_error() {
|
230
|
SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'visible');
|
231
|
fetch_data();
|
232
|
}
|
233
|
|
234
|
function isNumber(a) {
|
235
|
return typeof a == 'number' && isFinite(a);
|
236
|
}
|
237
|
|
238
|
]]>
|
239
|
</script>
|
240
|
</svg>
|