Last active
February 15, 2024 08:14
-
-
Save saeedvir/c4adf8ed3e646f6892b9d7980d95d65b to your computer and use it in GitHub Desktop.
php get server info (+shared-host) without use COM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Saeed Abdollahian | |
Telegram: | |
https://t.me/PhpWebDeveloper | |
*/ | |
function getServerLoad() | |
{ | |
$load = 0; | |
if (function_exists('sys_getloadavg')) { | |
$sysLoad = sys_getloadavg(); | |
$load = $sysLoad[0]; | |
return $load; | |
} else { | |
if (stristr(PHP_OS, 'win')) { | |
$typeperfCounter = '\processor(_total)\% processor time'; | |
exec('typeperf -sc 1 "' . $typeperfCounter . '"', $output); | |
$line = explode(',', $output[2]); | |
$load = trim($line[1], '"'); | |
//or use | |
//exec('wmic cpu get loadpercentage /all', $output); | |
//$load = $output[1]; | |
} else { | |
$procInfo = file('/proc/loadavg'); | |
if (isset($procInfo[0])) { | |
$procInfo = explode(' ', $procInfo[0]); | |
$load = round(($procInfo[0] + $procInfo[1] + $procInfo[2]) / 3); | |
} | |
} | |
} | |
return $load; | |
} | |
function getServerMemoryInfo(){ | |
$mem_info =[]; | |
$mem_info['script_usage'] = memory_get_usage(); | |
$mem_info['limit'] = ini_get('memory_limit'); | |
if (stristr(PHP_OS, 'win')) { | |
exec('wmic ComputerSystem get TotalPhysicalMemory', $totalMemory); | |
$mem_info['total'] = $totalMemory[1]; | |
unset($totalMemory); | |
exec('wmic OS get FreePhysicalMemory /Value 2>&1', $freePhysicalMemory); | |
$mem_info['free'] = getNumericsFromString($freePhysicalMemory[2])[0]; | |
unset($freePhysicalMemory); | |
$mem_info['total_usage'] = $mem_info['total']-$mem_info['free']; | |
}else{ | |
$proc_info = file('/proc/meminfo'); | |
if (isset($proc_info[0])) { | |
$mem_info['total'] = getNumericsFromString($proc_info[0])[0]*1024; | |
$mem_info['free'] = getNumericsFromString($proc_info[1])[0]*1024; | |
} | |
} | |
return $mem_info; | |
} | |
function getServerUptime(){ | |
$uptime = 0; | |
if (stristr(PHP_OS, 'win')) { | |
exec('wmic OS get LastBootUpTime /Value 2>&1', $lastBootUpTime); | |
$uptime = getNumericsFromString($lastBootUpTime[2])[0]; | |
}else{ | |
$proc_info = file('/proc/uptime'); | |
if (isset($proc_info[0])) { | |
$uptime = getNumericsFromString($proc_info[0])[0]; | |
} | |
} | |
return $uptime; | |
} | |
function getNumericsFromString($str) { | |
preg_match_all('/\d+/', $str, $matches); | |
return $matches[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment