Project

General

Profile

Download (7.73 KB) Statistics
| Branch: | Tag: | Revision:
1 7c5833d7 Scott Ullrich
<?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 7ac5a4cb Scott Ullrich
/*
32
	pfSense_MODULE:	graph
33
*/
34 7c5833d7 Scott Ullrich
35 6b07c15a Matthew Grooms
##|+PRIV
36
##|*IDENT=page-diagnostics-cpuutilization
37
##|*NAME=Diagnostics: CPU Utilization page
38
##|*DESCR=Allow access to the 'Diagnostics: CPU Utilization' page.
39
##|*MATCH=graph_cpu.php*
40
##|-PRIV
41
42 16e6485b Ermal Luçi
header("Last-Modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT" );
43
header("Expires: " . gmdate( "D, j M Y H:i:s", time() ) . " GMT" );
44
header("Cache-Control: no-store, no-cache, must-revalidate" ); // HTTP/1.1
45
header("Cache-Control: post-check=0, pre-check=0", FALSE );
46
header("Pragma: no-cache"); // HTTP/1.0
47 7c5833d7 Scott Ullrich
header("Content-type: image/svg+xml");
48
49
/********* Other conf *******/
50
51
$nb_plot = 120;  // maximum number of data points to plot in the graph
52
$fetch_link = "stats.php?stats=cpu";
53
54
//SVG attributes
55
$attribs['axis']='fill="black" stroke="black"';
56 216cdefa Scott Ullrich
$attribs['cpu']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="7"';
57
$attribs['graph_cpu']='fill="none" stroke="#FF0000" stroke-opacity="0.8"';
58 7c5833d7 Scott Ullrich
$attribs['legend']='fill="black" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
59
$attribs['grid_txt']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="6"';
60
$attribs['grid']='stroke="gray" stroke-opacity="0.5"';
61
$attribs['error']='fill="blue" font-family="Arial" font-size="4"';
62
$attribs['collect_initial']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
63
64
$height=100;  // SVG internal height : do not modify
65
$width=200;   // SVG internal width  : do not modify
66
67
/********* Graph DATA **************/
68
print('<?xml version="1.0" encoding="iso-8859-1"?>' . "\n");?>
69
<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);">
70
  <g id="graph">
71
    <rect id="bg" x1="0" y1="0" width="100%" height="100%" fill="white"/>
72
    <line id="axis_x" x1="0" y1="0" x2="0" y2="100%" <?=$attribs['axis']?>/>
73
    <line id="axis_y" x1="0" y1="100%" x2="100%" y2="100%" <?=$attribs['axis']?>/>
74
    <polygon id="axis_arrow_x" <?=$attribs['axis']?> points="<?=($width) . "," . ($height)?> <?=($width-2) . "," . ($height-2)?> <?=($width-2) . "," . $height?>"/>
75
    <path id="graph_cpu" d="" <?=$attribs['graph_cpu']?>/>
76
    <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']?>/>
77
    <text id="grid_txt1" x="100%" y="25%" <?=$attribs['grid_txt']?> text-anchor="end">75%</text>
78
    <text id="grid_txt2" x="100%" y="50%" <?=$attribs['grid_txt']?> text-anchor="end">50%</text>
79
    <text id="grid_txt3" x="100%" y="75%" <?=$attribs['grid_txt']?> text-anchor="end">25%</text>
80
    <text id="graph_cpu_txt" x="4" y="8" <?=$attribs['cpu']?>> </text>
81
    <text id="error" x="50%" y="50%"  visibility="hidden" <?=$attribs['error']?> text-anchor="middle">Cannot get CPU load</text>
82
    <text id="collect_initial" x="50%" y="50%"  visibility="hidden" <?=$attribs['collect_initial']?> text-anchor="middle">Collecting initial data, please wait...</text>
83
  </g>
84
  <script type="text/ecmascript">
85
    <![CDATA[
86
87
/**
88
 * getURL is a proprietary Adobe function, but it's simplicity has made it very
89
 * popular. If getURL is undefined we spin our own by wrapping XMLHttpRequest.
90
 */
91
if (typeof getURL == 'undefined') {
92
  getURL = function(url, callback) {
93
    if (!url)
94
      throw 'No URL for getURL';
95
96
    try {
97
      if (typeof callback.operationComplete == 'function')
98
        callback = callback.operationComplete;
99
    } catch (e) {}
100
    if (typeof callback != 'function')
101
      throw 'No callback function for getURL';
102
103
    var http_request = null;
104
    if (typeof XMLHttpRequest != 'undefined') {
105
      http_request = new XMLHttpRequest();
106
    }
107
    else if (typeof ActiveXObject != 'undefined') {
108
      try {
109
        http_request = new ActiveXObject('Msxml2.XMLHTTP');
110
      } catch (e) {
111
        try {
112
          http_request = new ActiveXObject('Microsoft.XMLHTTP');
113
        } catch (e) {}
114
      }
115
    }
116
    if (!http_request)
117
      throw 'Both getURL and XMLHttpRequest are undefined';
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
}
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
  SVGDoc = evt.target.ownerDocument;
144
  fetch_data();
145
}
146
147
function fetch_data() {
148
  getURL('<?=$fetch_link?>', plot_cpu_data);
149
}
150
151
function plot_cpu_data(obj) {
152
  if (!obj.success)
153
    return handle_error();  // getURL failed to get current CPU load data
154
155
  var cpu = parseInt(obj.content);
156
  if (!isNumber(cpu))
157
    return handle_error();
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>