<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
echo json_encode(getSystemInfo());
// Main function to get all system info
function getSystemInfo() {
return [
"cpu" => getCpuInfo(),
"memory" => getMemoryInfo(),
"storage" => getStorageInfo(),
];
}
// Get CPU load and history
function getCpuInfo() {
$os = getOS();
$load = [];
if ($os === 'Windows') {
// Windows: Use WMIC for CPU load
$output = shell_exec("wmic cpu get loadpercentage");
preg_match_all('/\d+/', $output, $matches);
$load = array_map('floatval', $matches[0]);
} else {
// Linux and macOS: Use sys_getloadavg
$load = sys_getloadavg();
}
return [
"load" => isset($load[0]) ? number_format($load[0], 2) . " %" : "N/A",
"history" => array_map(function($value) {
return number_format($value, 2);
}, $load)
];
}
// Get memory usage
function getMemoryInfo() {
$os = getOS();
if ($os === 'Windows') {
// Windows: Use WMIC for memory stats
$totalMemory = shell_exec("wmic os get totalvisiblememorysize /value");
$freeMemory = shell_exec("wmic os get freephysicalmemory /value");
preg_match('/\d+/', $totalMemory, $totalMatch);
preg_match('/\d+/', $freeMemory, $freeMatch);
$totalMemoryKB = isset($totalMatch[0]) ? intval($totalMatch[0]) : 0;
$freeMemoryKB = isset($freeMatch[0]) ? intval($freeMatch[0]) : 0;
} else {
// Linux and macOS: Use /proc/meminfo or vm_stat
if ($os === 'Linux') {
$totalMemory = shell_exec("grep MemTotal /proc/meminfo | awk '{print $2}'") * 1024;
$freeMemory = shell_exec("grep MemAvailable /proc/meminfo | awk '{print $2}'") * 1024;
} else {
// macOS: Use vm_stat for memory stats
$totalMemory = shell_exec("sysctl hw.memsize | awk '{print $2}'");
$freeMemory = shell_exec("vm_stat | grep 'free' | awk '{print $3}' | sed 's/\\.$//'");
$freeMemory = $freeMemory * 4096; // Convert to bytes
}
$totalMemoryKB = intval($totalMemory) / 1024;
$freeMemoryKB = intval($freeMemory) / 1024;
}
$totalMemoryGB = $totalMemoryKB / 1024 / 1024; // Convert to GB
$freeMemoryGB = $freeMemoryKB / 1024 / 1024; // Convert to GB
$usedMemoryGB = $totalMemoryGB - $freeMemoryGB;
$usagePercentage = $totalMemoryGB > 0 ? ($usedMemoryGB / $totalMemoryGB) * 100 : 0;
return [
"total" => number_format($totalMemoryGB, 2) . " GB",
"used" => number_format($usedMemoryGB, 2) . " GB",
"usage" => number_format($usagePercentage, 2) . " %"
];
}
// Get storage info
function getStorageInfo() {
$os = getOS();
$output = [];
if ($os === 'Windows') {
// Windows: Use WMIC for disk stats
$rawOutput = shell_exec("wmic logicaldisk get name, size, freespace");
$lines = array_slice(explode("\n", $rawOutput), 1);
foreach ($lines as $line) {
$columns = preg_split('/\s+/', trim($line));
if (count($columns) === 3) {
$filesystem = $columns[0];
$size = intval($columns[1]) / 1024 / 1024 / 1024; // Convert to GB
$free = intval($columns[2]) / 1024 / 1024 / 1024; // Convert to GB
$used = $size - $free;
$usage = $size > 0 ? ($used / $size) * 100 : 0;
$output[] = [
"filesystem" => $filesystem,
"size" => number_format($size, 2) . " GB",
"used" => number_format($used, 2) . " GB",
"usage" => number_format($usage, 2) . " %"
];
}
}
} else {
// Linux and macOS: Use df command
$rawOutput = shell_exec("df -h --output=source,size,used,pcent | grep -v 'tmpfs' | grep -v 'Filesystem'");
$lines = explode("\n", trim($rawOutput));
foreach ($lines as $line) {
$columns = preg_split('/\s+/', $line);
if (count($columns) >= 4) {
$output[] = [
"filesystem" => $columns[0],
"size" => $columns[1],
"used" => $columns[2],
"usage" => trim($columns[3])
];
}
}
}
return $output;
}
// Detect the operating system
function getOS() {
if (stristr(PHP_OS, 'WIN')) {
return 'Windows';
} elseif (stristr(PHP_OS, 'DAR')) {
return 'macOS';
} elseif (stristr(PHP_OS, 'LINUX')) {
return 'Linux';
}
return 'Unknown';
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]