Move the get_memory_details functions
This commit is contained in:
parent
5e75879518
commit
540efc355f
|
|
@ -50,55 +50,6 @@
|
|||
}
|
||||
|
||||
//get memory details
|
||||
function get_memory_details() {
|
||||
if (PHP_OS == 'Linux') {
|
||||
$meminfo = file_get_contents("/proc/meminfo");
|
||||
$data = [];
|
||||
|
||||
foreach (explode("\n", $meminfo) as $line) {
|
||||
if (preg_match('/^(\w+):\s+(\d+)\skB$/', $line, $matches)) {
|
||||
$data[$matches[1]] = $matches[2];
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($data['MemTotal']) && isset($data['MemAvailable'])) {
|
||||
$array['total_memory'] = $data['MemTotal'];
|
||||
$array['available_memory'] = $data['MemAvailable'];
|
||||
$array['used_memory'] = $array['total_memory'] - $array['available_memory'];
|
||||
|
||||
$array['memory_usage'] = ($array['used_memory'] / $array['total_memory']) * 100;
|
||||
$array['memory_percent'] = round($array['memory_usage'], 2);
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
||||
if (PHP_OS == 'FreeBSD') {
|
||||
//define the output array
|
||||
$output = [];
|
||||
|
||||
// get the memory information using sysctl
|
||||
exec('sysctl -n hw.physmem hw.pagesize vm.stats.vm.v_free_count vm.stats.vm.v_inactive_count vm.stats.vm.v_cache_count vm.stats.vm.v_wire_count', $output);
|
||||
|
||||
if (count($output) === 6) {
|
||||
list($array['total_memory'], $page_size, $free_pages, $inactive_pages, $cache_pages, $wired_pages) = $output;
|
||||
|
||||
// total memory in bytes
|
||||
$array['total_memory'] = (int)$array['total_memory'];
|
||||
|
||||
// pages to bytes conversion
|
||||
$array['available_memory'] = ($free_pages + $inactive_pages + $cache_pages) * (int)$page_size;
|
||||
$array['used_memory'] = $array['total_memory'] - $array['available_memory'];
|
||||
|
||||
// calculate memory usage percentage
|
||||
$array['memory_usage'] = ($array['used_memory'] / $array['total_memory']) * 100;
|
||||
|
||||
$array['memory_percent'] = round($array['memory_usage'], 2) . '%';
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
$memory_details = get_memory_details();
|
||||
|
||||
//disk usage
|
||||
|
|
|
|||
|
|
@ -2467,4 +2467,57 @@ if (!function_exists('url_get_contents')) {
|
|||
}
|
||||
}
|
||||
|
||||
//get system memory details
|
||||
if (!function_exists('get_memory_details')) {
|
||||
function get_memory_details() {
|
||||
if (PHP_OS == 'Linux') {
|
||||
$meminfo = file_get_contents("/proc/meminfo");
|
||||
$data = [];
|
||||
|
||||
foreach (explode("\n", $meminfo) as $line) {
|
||||
if (preg_match('/^(\w+):\s+(\d+)\skB$/', $line, $matches)) {
|
||||
$data[$matches[1]] = $matches[2];
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($data['MemTotal']) && isset($data['MemAvailable'])) {
|
||||
$array['total_memory'] = $data['MemTotal'];
|
||||
$array['available_memory'] = $data['MemAvailable'];
|
||||
$array['used_memory'] = $array['total_memory'] - $array['available_memory'];
|
||||
|
||||
$array['memory_usage'] = ($array['used_memory'] / $array['total_memory']) * 100;
|
||||
$array['memory_percent'] = round($array['memory_usage'], 2);
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
||||
if (PHP_OS == 'FreeBSD') {
|
||||
//define the output array
|
||||
$output = [];
|
||||
|
||||
// get the memory information using sysctl
|
||||
exec('sysctl -n hw.physmem hw.pagesize vm.stats.vm.v_free_count vm.stats.vm.v_inactive_count vm.stats.vm.v_cache_count vm.stats.vm.v_wire_count', $output);
|
||||
|
||||
if (count($output) === 6) {
|
||||
list($array['total_memory'], $page_size, $free_pages, $inactive_pages, $cache_pages, $wired_pages) = $output;
|
||||
|
||||
// total memory in bytes
|
||||
$array['total_memory'] = (int)$array['total_memory'];
|
||||
|
||||
// pages to bytes conversion
|
||||
$array['available_memory'] = ($free_pages + $inactive_pages + $cache_pages) * (int)$page_size;
|
||||
$array['used_memory'] = $array['total_memory'] - $array['available_memory'];
|
||||
|
||||
// calculate memory usage percentage
|
||||
$array['memory_usage'] = ($array['used_memory'] / $array['total_memory']) * 100;
|
||||
|
||||
$array['memory_percent'] = round($array['memory_usage'], 2) . '%';
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
|||
Loading…
Reference in New Issue