<?php
// 监测服务器的 CPU 和内存使用情况
function getServerLoad() {
$load = sys_getloadavg();
$cpuLoad = $load[0]; // 1 分钟平均负载
$memoryUsage = memory_get_usage(true); // 获取内存使用情况(字节)
// 获取系统总内存和可用内存
$totalMemory = shell_exec('grep MemTotal /proc/meminfo | awk \'{print $2}\'') * 1024;
$freeMemory = shell_exec('grep MemFree /proc/meminfo | awk \'{print $2}\'') * 1024;
$usedMemory = $totalMemory - $freeMemory;
return [
'cpu_load' => $cpuLoad,
'memory_usage' => $memoryUsage,
'total_memory' => $totalMemory,
'used_memory' => $usedMemory,
'free_memory' => $freeMemory
];
}
// 获取服务器负载数据
$serverLoad = getServerLoad();
header('Content-Type: application/json');
echo json_encode($serverLoad, JSON_PRETTY_PRINT);
?>