Project

General

Profile

Download (7.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	$Id$
4
	part of m0n0wall (http://m0n0.ch/wall)
5
	
6
	Copyright (C) 2004-2005 T. Lechat <dev@lechat.org>, Manuel Kasper <mk@neon1.net>
7
	and Jonathan Watt <jwatt@jwatt.org>.
8
	All rights reserved.
9
	
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12
	
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15
	
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19
	
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

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

    
39

    
40
header("Content-type: image/svg+xml");
41

    
42
/********* Other conf *******/
43

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

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

    
57
$height=100;  // SVG internal height : do not modify
58
$width=200;   // SVG internal width  : do not modify
59

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

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

    
89
    try {
90
      if (typeof callback.operationComplete == 'function')
91
        callback = callback.operationComplete;
92
    } catch (e) {}
93
    if (typeof callback != 'function')
94
      throw 'No callback function for getURL';
95

    
96
    var http_request = null;
97
    if (typeof XMLHttpRequest != 'undefined') {
98
      http_request = new XMLHttpRequest();
99
    }
100
    else if (typeof ActiveXObject != 'undefined') {
101
      try {
102
        http_request = new ActiveXObject('Msxml2.XMLHTTP');
103
      } catch (e) {
104
        try {
105
          http_request = new ActiveXObject('Microsoft.XMLHTTP');
106
        } catch (e) {}
107
      }
108
    }
109
    if (!http_request)
110
      throw 'Both getURL and XMLHttpRequest are undefined';
111

    
112
    http_request.onreadystatechange = function() {
113
      if (http_request.readyState == 4) {
114
        callback( { success : true,
115
                    content : http_request.responseText,
116
                    contentType : http_request.getResponseHeader("Content-Type") } );
117
      }
118
    }
119
    http_request.open('GET', url, true);
120
    http_request.send(null);
121
  }
122
}
123

    
124
var SVGDoc = null;
125
var last_cpu_total = 0;
126
var last_cpu_idle = 0;
127
var diff_cpu_total = 0;
128
var diff_cpu_idle = 0;
129
var cpu_data = new Array();
130

    
131
var max_num_points = <?=$nb_plot?>;  // maximum number of plot data points
132
var step = <?=$width?> / max_num_points;  // plot X division size
133
var scale = <?=$height?> / 100;
134

    
135
function init(evt) {
136
  SVGDoc = evt.target.ownerDocument;
137
  fetch_data();
138
}
139

    
140
function fetch_data() {
141
  getURL('<?=$fetch_link?>', plot_cpu_data);
142
}
143

    
144
function plot_cpu_data(obj) {
145
  if (!obj.success)
146
    return handle_error();  // getURL failed to get current CPU load data
147

    
148
  var cpu = parseInt(obj.content);
149
  if (!isNumber(cpu))
150
    return handle_error();
151

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

    
170
  cpu_data[cpu_data.length] = cpu;
171

    
172
  var path_data = "M 0 " + (<?=$height?> - (cpu_data[0] * scale));
173
  for (var i = 1; i < cpu_data.length; ++i) {
174
    var x = step * i;
175
    var y_cpu = <?=$height?> - (cpu_data[i] * scale);
176
    path_data += " L" + x + " " + y_cpu;
177
  }
178

    
179
  SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'hidden');
180
  SVGDoc.getElementById('graph_cpu_txt').firstChild.data = cpu + '%';
181
  SVGDoc.getElementById('graph_cpu').setAttributeNS(null, "d", path_data);
182

    
183
  fetch_data();
184
}
185

    
186
function handle_error() {
187
  SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'visible');
188
  fetch_data();
189
}
190

    
191
function isNumber(a) {
192
  return typeof a == 'number' && isFinite(a);
193
}
194

    
195
    ]]>
196
  </script>
197
</svg>
(63-63/211)