Menu

[r5]: / trunk / www / graph_cpu.php  Maximize  Restore  History

Download this file

175 lines (144 with data), 6.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/local/bin/php -f
<?php
/*
graph_cpu.php
part of m0n0wall (http://m0n0.ch/wall)
Copyright (C) 2004-2005 T. Lechat <dev@lechat.org> and Manuel Kasper <mk@neon1.net>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/********* Other conf *******/
$nb_plot=120; //NB plot in graph
$fetch_link = "stats.cgi?cpu";
//Style
$style['bg']="fill:white;stroke:none;stroke-width:0;opacity:1;";
$style['axis']="fill:black;stroke:black;stroke-width:1;";
$style['cpu']="fill:#435370; font-family:Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size:7;";
$style['graph_cpu']="fill:none;stroke:#435370;stroke-width:1;opacity:0.8;";
$style['legend']="fill:black; font-family:Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size:4;";
$style['grid_txt']="fill:gray; font-family:Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size:6;";
$style['grid']="stroke:gray;stroke-width:1;opacity:0.5;";
$style['error']="fill:blue; font-family:Arial; font-size:4;";
$style['collect_initial']="fill:gray; font-family:Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size:4;";
$error_text = "Cannot get CPU load";
$height=100; //SVG internal height : do not modify
$width=200; //SVG internal width : do not modify
/********* Graph DATA **************/
header("Content-type: image/svg+xml");
print('<?xml version="1.0" encoding="iso-8859-1"?>' . "\n");?><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)">
<g id="graph" style="visibility:visible">
<rect id="bg" x1="0" y1="0" x2="<?=$width?>" y2="<?=$height?>" style="<?=$style['bg']?>"/>
<line id="axis_x" x1="0" y1="0" x2="0" y2="<?=$height?>" style="<?=$style['axis']?>"/>
<line id="axis_y" x1="0" y1="<?=$height?>" x2="<?=$width?>" y2="<?=$height?>" style="<?=$style['axis']?>"/>
<path id="graph_cpu" d="M0 <?=$height?> L 0 <?=$height?>" style="<?=$style['graph_cpu']?>"/>
<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?>" style="<?=$style[grid]?>"/>
<text id="grid_txt1" x="<?=$width?>" y="<?=$height/4*1?>" style="<?=$style['grid_txt']?> text-anchor:end">75%</text>
<text id="grid_txt2" x="<?=$width?>" y="<?=$height/4*2?>" style="<?=$style['grid_txt']?> text-anchor:end">50%</text>
<text id="grid_txt3" x="<?=$width?>" y="<?=$height/4*3?>" style="<?=$style['grid_txt']?> text-anchor:end">25%</text>
<text id="graph_cpu_txt" x="4" y="8" style="<?=$style['cpu']?>"> </text>
<polygon id="axis_arrow_x" style="<?=$style['axis']?>" points="<?=($width) . "," . ($height)?> <?=($width-2) . "," . ($height-2)?> <?=($width-2) . "," . $height?>"/>
<text id="error" x="<?=$width*0.5?>" y="<?=$height*0.5?>" style="visibility:hidden;<?=$style['error']?> text-anchor:middle"><?=$error_text?></text>
<text id="collect_initial" x="<?=$width*0.5?>" y="<?=$height*0.5?>" style="visibility:hidden;<?=$style['collect_initial']?> text-anchor:middle">Collecting initial data, please wait...</text>
</g>
<script type="text/ecmascript"><![CDATA[
var SVGDoc;
var last_cpu_total=0;
var last_cpu_idle=0;
var diff_cpu_total=0;
var diff_cpu_idle=0;
plot_cpu = new Array();
var isfirst=1;
var index_plot=0;
var step = <?=$width?> / <?=$nb_plot?> ;
function init(evt) {
SVGDoc = evt.getTarget().getOwnerDocument();
go();
}
function go() {
getURL('<?=$fetch_link?>',urlcallback);
}
function urlcallback(obj) {
var error = 0;
//shift plot to left if nb_plot is already completed
var i=0;
if(index_plot > <?=$nb_plot?>)
{
while (i <= <?=$nb_plot?>)
{
var a=i+1;
plot_cpu[i]=plot_cpu[a];
i=i+1;
}
index_plot = <?=$nb_plot?>;
plot_cpu[index_plot]=0;
}
//if Geturl returns something
if (obj.success){
var cpu = parseInt(obj.content);
var scale;
if(!isNumber(cpu)) {
goerror();
return;
} else {
SVGDoc.getElementById("error").getStyle().setProperty ('visibility', 'hidden');
}
if(isfirst) {
SVGDoc.getElementById("collect_initial").getStyle().setProperty ('visibility', 'visible');
go();
isfirst=0;
return;
} else SVGDoc.getElementById("collect_initial").getStyle().setProperty ('visibility', 'hidden');
plot_cpu[index_plot] = cpu;
SVGDoc.getElementById('graph_cpu_txt').getFirstChild().setData(plot_cpu[index_plot] + '%');
scale = <?=$height?> / 100;
i = 0;
while (i <= index_plot)
{
var x = step * i;
var y_cpu= <?=$height?> - (plot_cpu[i] * scale);
if(i==0) {
var path_cpu = "M" + x + " " + y_cpu;
}
else
{
var path_cpu = path_cpu + " L" + x + " " + y_cpu;
}
i = i + 1;
}
index_plot = index_plot+1;
SVGDoc.getElementById('graph_cpu').setAttribute("d", path_cpu);
go();
}
else
{ //In case of Geturl fails
goerror();
}
}
function goerror() {
SVGDoc.getElementById("error").getStyle().setProperty ('visibility', 'visible');
go();
}
function isNumber(a) {
return typeof a == 'number' && isFinite(a);
}
function LZ(x) {
return (x < 0 || x > 9 ? "" : "0") + x
}
]]></script>
</svg>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.