The below code basically queries a PHP file, that queries a database for cpu_names (4 total in my test case) and the associated loads on each. I then need to take each of those four CPU times, add them together and then divide by the total number of CPUs. This essentially gets me total CPU load. So, for the test case this code upgrades 5 total CPU gauges, one of which is the total. The only problem, no matter which way I try it I can't sum these posts! Thanks.
setInterval(function() {
fill_sum=0.0;
$("canvas[name='gauge']").each(function(){
var cpu_name=this.innerHTML;
var ctx = this.getContext('2d');
var padding = this.style.padding.replace(/[^0-9]+/, '');
var w=this.width
var h=this.height;
//var fill_percent=.02;
$.post("BH_Responder.php",
{cpu_use: true, cpu_name: cpu_name},
function(data, status){
fill_percent = data;
ctx.clearRect(0, 0, w, h);
DrawGauge(ctx, w, h, fill_percent);
}
).done(function() {
fill_sum = fill_sum+parseFloat(fill_percent);
});
});
alert(fill_sum);
total_percent = fill_sum/<?php echo $num_rows_cpu; ?>;
$("canvas[name='master_gauge']").each(function(){
var ctx = this.getContext('2d');
var padding = this.style.padding.replace(/[^0-9]+/, '');
var w=this.width
var h=this.height;
//var fill_percent=.02;
ctx.clearRect(0, 0, w, h);
DrawGauge(ctx, w, h, total_percent);
});
}, 100);
Section of PHP script Responder script:
}elseif (isset($_POST['cpu_use']) == true && empty($_POST['cpu_use'])==false && $_POST['cpu_use']==true){
$cpu_name = $_POST['cpu_name'];
$sql= mysql_query("SELECT used FROM tbl_cpu_use WHERE cpu_name='$cpu_name' ORDER BY id DESC LIMIT 1;");
echo (mysql_num_rows($sql) != 0) ? mysql_result($sql,0,'used') : NULL;
}