Project

General

Profile

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

    
36
##|+PRIV
37
##|*IDENT=page-diagnostics-cpuutilization
38
##|*NAME=Diagnostics: CPU Utilization page
39
##|*DESCR=Allow access to the 'Diagnostics: CPU Utilization' page.
40
##|*MATCH=graph_cpu.php*
41
##|-PRIV
42

    
43
require_once("guiconfig.inc");
44

    
45
header("Last-Modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT" );
46
header("Expires: " . gmdate( "D, j M Y H:i:s", time() ) . " GMT" );
47
header("Cache-Control: no-store, no-cache, must-revalidate" ); // HTTP/1.1
48
header("Cache-Control: post-check=0, pre-check=0", FALSE );
49
header("Pragma: no-cache"); // HTTP/1.0
50
header("Content-type: image/svg+xml");
51

    
52
/********* Other conf *******/
53

    
54
$nb_plot = 120;  // maximum number of data points to plot in the graph
55
$fetch_link = "stats.php?stats=cpu";
56

    
57
//SVG attributes
58
$attribs['axis']='fill="black" stroke="black"';
59
$attribs['cpu']='fill="#FF0000" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="7"';
60
$attribs['graph_cpu']='fill="none" stroke="#FF0000" stroke-opacity="0.8"';
61
$attribs['legend']='fill="black" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
62
$attribs['grid_txt']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="6"';
63
$attribs['grid']='stroke="gray" stroke-opacity="0.5"';
64
$attribs['error']='fill="blue" font-family="Arial" font-size="4"';
65
$attribs['collect_initial']='fill="gray" font-family="Tahoma, Verdana, Arial, Helvetica, sans-serif" font-size="4"';
66

    
67
$height=100;  // SVG internal height : do not modify
68
$width=200;   // SVG internal width  : do not modify
69

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

    
90
/**
91
 * getURL is a proprietary Adobe function, but it's simplicity has made it very
92
 * popular. If getURL is undefined we spin our own by wrapping XMLHttpRequest.
93
 */
94
if (typeof getURL == 'undefined') {
95
  getURL = function(url, callback) {
96
    if (!url)
97
      throw '<?=gettext("No URL for getURL"); ?>';
98

    
99
    try {
100
      if (typeof callback.operationComplete == 'function')
101
        callback = callback.operationComplete;
102
    } catch (e) {}
103
    if (typeof callback != 'function')
104
      throw '<?=gettext("No callback function for getURL"); ?>';
105

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

    
122
    http_request.onreadystatechange = function() {
123
      if (http_request.readyState == 4) {
124
        callback( { success : true,
125
                    content : http_request.responseText,
126
                    contentType : http_request.getResponseHeader("Content-Type") } );
127
      }
128
    }
129
    http_request.open('GET', url, true);
130
    http_request.send(null);
131
  }
132
}
133

    
134
var SVGDoc = null;
135
var last_cpu_total = 0;
136
var last_cpu_idle = 0;
137
var diff_cpu_total = 0;
138
var diff_cpu_idle = 0;
139
var cpu_data = new Array();
140

    
141
var max_num_points = <?=$nb_plot?>;  // maximum number of plot data points
142
var step = <?=$width?> / max_num_points;  // plot X division size
143
var scale = <?=$height?> / 100;
144

    
145
function init(evt) {
146
  SVGDoc = evt.target.ownerDocument;
147
  fetch_data();
148
}
149

    
150
function fetch_data() {
151
  getURL('<?=$fetch_link?>', plot_cpu_data);
152
}
153

    
154
function plot_cpu_data(obj) {
155
  if (!obj.success)
156
    return handle_error();  // getURL failed to get current CPU load data
157

    
158
  var cpu = parseInt(obj.content);
159
  if (!isNumber(cpu))
160
    return handle_error();
161

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

    
180
  cpu_data[cpu_data.length] = cpu;
181

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

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

    
193
  fetch_data();
194
}
195

    
196
function handle_error() {
197
  SVGDoc.getElementById("error").setAttributeNS(null, 'visibility', 'visible');
198
  fetch_data();
199
}
200

    
201
function isNumber(a) {
202
  return typeof a == 'number' && isFinite(a);
203
}
204

    
205
    ]]>
206
  </script>
207
</svg>
(86-86/252)