我在我的管理面板上工作,我遇到了一个问题。我正在使用一个输出我的服务器 CPU 使用率的小脚本,我想将它集成到一个 javascript 图表中。我无法让它工作。我想知道如何使变量在 javascript 图表中工作。
我的PHP:
<?php
$stat1 = file('/proc/stat');
sleep(1);
$stat2 = file('/proc/stat');
$info1 = explode(" ", preg_replace("!cpu +!", "", $stat1[0]));
$info2 = explode(" ", preg_replace("!cpu +!", "", $stat2[0]));
$dif = array();
$dif['user'] = $info2[0] - $info1[0];
$dif['nice'] = $info2[1] - $info1[1];
$dif['sys'] = $info2[2] - $info1[2];
$dif['idle'] = $info2[3] - $info1[3];
$total = array_sum($dif);
$cpu = array();
foreach($dif as $x=>$y) $cpu[$x] = round($y / $total * 100, 1);
$usedcpu = $cpu['user'] + $cpu['nice'] + $cpu['sys'];
?>
还有我的 javascript
<script>
$(document).ready(function() {
// Demo #1
// we use an inline data source in the example, usually data would be fetched from a server
var data = "<?php echo $usedcpu; ?>";
function getData() {
return data;
}
// setup control widget
var updateInterval = 30;
$("#updateInterval").val(updateInterval).change(function () {
var v = $(this).val();
if (v && !isNaN(+v)) {
updateInterval = +v;
if (updateInterval < 1)
updateInterval = 1;
if (updateInterval > 2000)
updateInterval = 2000;
$(this).val("" + updateInterval);
}
});
// setup plot
var options = {
series: {
shadowSize: 0,
color: '#c0382b',
lines: {
fill: true
}
}, // drawing is faster without shadows
yaxis: { min: 0, max: 10 },
xaxis: { show: false },
grid: { backgroundColor: '#ffffff', borderColor: 'transparent' },
};
var plot = $.plot($("#demo-1"), [ getData() ], options);
function update() {
plot.setData([ getData() ]);
// since the axes don't change, we don't need to call plot.setupGrid()
plot.draw();
setTimeout(update, updateInterval);
}
update();
});
</script>
我希望你们能帮助我,提前谢谢!