管理パネルで作業していますが、問題に直面しています。サーバーの 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>
よろしくお願いします。