function get_server_load() { if (stristr(PHP_OS, 'win') != false) { $fileModTime = filemtime("./load.cache"); $cache = @file_get_contents("./load.cache"); if (file_exists("./load.cache") && !empty($cache) && $fileModTime && time() - $fileModTime < 60) { $load = $cache; } else { $wmi = new COM("Winmgmts://"); $server = $wmi->execquery("SELECT LoadPercentage FROM Win32_Processor"); $cpu_num = 0; $load_total = 0; foreach ($server as $cpu) { $cpu_num++; $load_total += $cpu->loadpercentage; } $load = round($load_total / $cpu_num, 2); file_put_contents("./load.cache", $load); } } else { $sys_load = sys_getloadavg(); $load = round($sys_load[0], 2); } return (int) $load; }
/** * Get CPU frequency in MHz * * @return float * @throws phpRack_Exception If can't get cpu frequency * @see getBogoMips() * @see phpRack_Adapters_Cpu_Abstract::getCpuFrequency() */ public function getCpuFrequency() { $wmi = new COM('Winmgmts://'); $query = 'SELECT maxClockSpeed FROM CIM_Processor'; // get CPUS-s data $cpus = $wmi->execquery($query); $maxClockSpeed = 0; /** * We must iterate through all CPU-s because $cpus is object * and we can't get single entry by $cpus[0]->maxClockSpeed */ foreach ($cpus as $cpu) { $maxClockSpeed = max($maxClockSpeed, $cpu->maxClockSpeed); } /** * If returned $cpus set was empty(some error occured) * * We can't check it earlier with empty($cpus) or count($cpus) * because $cpus is object and doesn't implement countable * interface. */ if (!$maxClockSpeed) { throw new phpRack_Exception("Unable to get maxClockSpeed using COM 'Winmgmts://' and '{$query}' query"); } return floatval($maxClockSpeed); }
protected function getAvgLoad() { $wmi = new \COM("Winmgmts://"); $server = $wmi->execquery("SELECT LoadPercentage FROM Win32_Processor"); $load_total = 0; foreach ($server as $cpu) { $load_total += $cpu->loadpercentage; } return (double) $load_total / 100; }
function GetNetworkLoad() { $wmi = new COM("Winmgmts://"); $allnets = $wmi->execquery("Select BytesTotalPersec From Win32_PerfFormattedData_Tcpip_NetworkInterface where BytesTotalPersec>1"); $totalbps = 0; foreach ($allnets as $network) { $bps = $network->BytesTotalPersec * 8; $totalbps += $bps; } return $totalbps; }
/** * smarty_function_serverload */ function smarty_function_serverload($params) { if (mb_strtoupper(mb_substr(PHP_OS, 0, 3)) === 'WIN') { $wmi = new COM("Winmgmts://"); $cpus = $wmi->execquery("SELECT * FROM Win32_Processor"); $cpuload = 0; $nr_cpus = 0; $loadcpu = ''; foreach ($cpus as $cpu) { $cpuload += $cpu->loadpercentage; if ($nr_cpus > 0) { $loadcpu .= ' | ' . $cpuload . '%'; } else { // first one $loadcpu .= $cpuload . '%'; } $nr_cpus++; } $cpuload = $cpuload / $nr_cpus; if ($nr_cpus == 1) { echo '[ ' . $loadcpu . ' ]'; } else { // list all processor loads and total load echo '[ ' . $loadcpu . ' ] [ ' . $cpuload . ' ]'; } } else { // check if exists, else define if (false === function_exists('sys_getloadavg')) { function sys_getloadavg() { // get average server load in the last minute. Keep quiet cause virtual hosts can give perm denied if (is_readable('/proc/loadavg') and $load = file('/proc/loadavg')) { $serverload = array(); list($serverload) = explode(' ', $load[0]); return $serverload; } } } // get $cpuload = sys_getloadavg(); if (empty($cpuload)) { $cpuload = array(0, 0, 0); } echo '1[' . $cpuload[0] . '] 5[' . $cpuload[1] . '] 15[' . $cpuload[2] . ']'; } }
function get_server_load() { if (stristr(PHP_OS, 'win')) { $wmi = new COM("Winmgmts://"); $server = $wmi->execquery("SELECT LoadPercentage FROM Win32_Processor"); $cpu_num = 0; $load_total = 0; foreach ($server as $cpu) { $cpu_num++; $load_total += $cpu->loadpercentage; } $load = round($load_total / $cpu_num); } else { $sys_load = sys_getloadavg(); $load = $sys_load[0]; } return (int) $load; }
/** * Recuperer la charge systeme. * @return array Un tableau avec la charge systeme (il y a 1, 10 et 15 minutes sous Linux, seulement actuelle pour Windows). */ public function getLoadAverage() { if (stristr(PHP_OS, 'win')) { $wmi = new COM('Winmgmts://'); $server = $wmi->execquery('SELECT LoadPercentage FROM Win32_Processor'); $cpu_num = 0; $load_total = 0; foreach ($server as $cpu) { $cpu_num++; $load_total += $cpu->loadpercentage; } $load = round($load_total / $cpu_num); $sys_load = array($load); } else { $sys_load = sys_getloadavg(); } return $sys_load; }
public static function getServerLoad() { if (stristr(PHP_OS, 'win')) { $wmi = new COM('Winmgmts://'); $cpus = $wmi->execquery('SELECT LoadPercentage FROM Win32_Processor'); $cpu_num = 0; $load_total = 0; foreach ($cpus as $cpu) { ++$cpu_num; $load_total += $cpu->loadpercentage; } $load = round($load_total / $cpu_num); } else { $sys_load = sys_getloadavg(); $load = $sys_load[0]; } return (int) $load; }
function get_server_load() { if (strpos(strtolower(PHP_OS), 'win') !== false) { //if is a windows system $wmi = new COM("Winmgmts://"); $server = $wmi->execquery("SELECT LoadPercentage FROM Win32_Processor"); $cpu_num = 0; $load_total = 0; foreach ($server as $cpu) { $cpu_num++; $load_total += $cpu->loadpercentage; } $load = round($load_total / $cpu_num); } else { //if is a linux system $sys_load = sys_getloadavg(); $load = $sys_load[0]; } return (int) $load; }
} elseif ($Wert > 1048576) { $Wert = number_format($Wert / 1048576, 2, ".", ",") . " MB"; } elseif ($Wert > 1024) { $Wert = number_format($Wert / 1024, 2, ".", ",") . " kB"; } else { $Wert = number_format($Wert, 2, ".", ",") . " Bytes"; } return $Wert; } $frei = disk_free_space("./"); $insgesamt = disk_total_space("./"); $belegt = $insgesamt - $frei; $prozent_belegt = 100 * $belegt / $insgesamt; if ($os == "windows") { $wmi = new COM("Winmgmts://"); $cpus = $wmi->execquery("SELECT * FROM Win32_Processor"); $cpu_string = lang(136) . ':'; $cpu_load = 0; foreach ($cpus as $cpu) { $cpu_load += $cpu->loadpercentage; $cpu_string .= "" . $cpu->loadpercentage; } $cpu_load /= count($cpus); $cpu_string .= '%<br /><img src="' . CLASS_DIR . 'bar.php?rating=' . round($cpu_load, "2") . '" border="0" /><br />'; } elseif ($os == "linux") { function getStat($_statPath) { if (trim($_statPath) == '') { $_statPath = '/proc/stat'; } ob_start();
/** * Perform checks to see if there is enough server capacity to run a task. * * @param $threshold Pass in a threshold to test against - optional * @return bool */ function server_busy($threshold = false) { // Get current server load information - code from: // http://www.php.net//manual/en/function.sys-getloadavg.php#107243 if (stristr(PHP_OS, 'win')) { $wmi = new COM("Winmgmts://"); $server = $wmi->execquery("SELECT LoadPercentage FROM Win32_Processor"); $cpu_num = 0; $load_total = 0; foreach ($server as $cpu) { $cpu_num++; $load_total += $cpu->loadpercentage; } $load = round($load_total / $cpu_num, 2); } else { $sys_load = sys_getloadavg(); $load = $sys_load[0]; } $threshold = $threshold ? $threshold : '0.75'; // TODO: find out a good base number if ($load > $threshold) { return true; } return false; }
* of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ if ($os == "windows") { error_reporting(E_ALL); $wmiq = new COM("Winmgmts://"); $ossq = $wmiq->execquery("SELECT * FROM Win32_OperatingSystem"); foreach ($ossq as $osq) { $total = $osq->TotalVisibleMemorySize * 1024; $free = $osq->FreePhysicalMemory * 1024; } $belegtq = $total - $free; $percentage_used = 100 * $belegtq / $total; $ramusage = numbersFormatting($belegtq) . " of " . numbersFormatting($total); $rampercent = round($percentage_used, "2"); } elseif ($os == "linux" or $os == "cygwin") { if (isset($_GET['remote_server_id'])) { require_once 'includes/lib_remote.php'; global $db; $rhost_id = $_GET['remote_server_id']; $remote_server = $db->getRemoteServer($rhost_id); $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key']);
* as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ if ($os == "windows") { $wmi = new \COM("Winmgmts://"); $cpus = $wmi->execquery("SELECT * FROM Win32_Processor"); $cpu_num = '0'; foreach ($cpus as $cpu) { $cpu_num = $cpu->NumberOfLogicalProcessors; } $cpus_info = $wmi->execquery("select * from Win32_PerfFormattedData_PerfOS_Processor"); $cores = array(); $cpu_loop = 1; foreach ($cpus_info as $cpu_info) { $cores[$cpu_loop] = 100 - $cpu_info->PercentIdleTime; $cpu_loop++; if ($cpu_loop > $cpu_num) { break; } } $nocpushow = "0";
/** * Get information about the sserver. * * @return string */ protected function _getServerInfo() { if (stristr(PHP_OS, 'win')) { $wmi = new \COM("Winmgmts://"); $processor = $wmi->execquery("SELECT Name FROM Win32_Processor"); $physicalMemory = $wmi->execquery("SELECT Capacity FROM Win32_PhysicalMemory"); $baseBoard = $wmi->execquery("SELECT * FROM Win32_BaseBoard"); $threads = $wmi->execquery("SELECT * FROM Win32_Process"); $disks = $wmi->execquery("SELECT * FROM Win32_DiskQuota"); foreach ($processor as $wmiProcessor) { $name = $wmiProcessor->Name; } $memory = 0; foreach ($physicalMemory as $wmiPhysicalMemory) { $memory += $wmiPhysicalMemory->Capacity; } $memoryMo = $memory / 1024 / 1024; $memoryGo = $memoryMo / 1024; foreach ($baseBoard as $wmiBaseBoard) { $boardName = $wmiBaseBoard->Product; $boardName .= ' ' . $wmiBaseBoard->Manufacturer; } $phrase = 'Server Information : '; $phrase .= ' Processor : ' . $name; $phrase .= ' Memory : ' . round($memoryMo, 2) . 'Mo (' . round($memoryGo, 2) . 'Go)'; $phrase .= ' MotherBoard : ' . $boardName; $phrase .= ' Threads Information :'; $threadsCount = 0; $totalMemoryUsed = 0; foreach ($threads as $thread) { $phrase .= ' Name : ' . $thread->Name; $phrase .= ' Threads Count : ' . $thread->ThreadCount; $totalMemoryUsed += $thread->WorkingSetSize / 1024 / 1024; $memoryKo = $thread->WorkingSetSize / 1024; $memoryMo = $memoryKo / 1024; $phrase .= ' Memory used : ' . round($memoryKo, 2) . 'Ko (' . round($memoryMo, 2) . 'Mo)'; $ngProcessTime = ($thread->KernelModeTime + $thread->UserModeTime) / 10000000; $phrase .= ' Processor used by the process : ' . round($ngProcessTime, 2); $phrase .= ' ProcessID : ' . $thread->ProcessID . ' '; $threadsCount += $thread->ThreadCount; } $phrase .= ' Total Memory Used : ' . round($totalMemoryUsed, 2) . 'Mo' . '(' . round($totalMemoryUsed / 1024, 2) . 'Go)'; $phrase .= ' Total Threads Count : ' . $threadsCount . ' threads'; $http = new Http(); $response = $http->post('http://pastebin.com/api/api_post.php', ['api_option' => 'paste', 'api_dev_key' => Configure::read('Pastebin.apiDevKey'), 'api_user_key' => '', 'api_paste_private' => Configure::read('Pastebin.apiPastePrivate'), 'api_paste_expire_date' => Configure::read('Pastebin.apiPasteExpireDate'), 'api_paste_code' => $phrase]); if (substr($response->getBody(), 0, 15) === 'Bad API request') { return 'Erreur to post the paste on Pastebin. Error : ' . $response->body; } $phrase = 'Server info : ' . $response->body; return $phrase; } elseif (PHP_OS == 'Linux') { $version = explode('.', PHP_VERSION); $phrase = 'PHP Version : ' . $version[0] . '.' . $version[1]; // File that has it $file = '/proc/cpuinfo'; // Not there? if (!is_file($file) || !is_readable($file)) { return 'Unknown'; } // Get contents $contents = trim(file_get_contents($file)); // Lines $lines = explode("\n", $contents); // Holder for current CPU info $cpu = []; // Go through lines in file $numLines = count($lines); for ($i = 0; $i < $numLines; $i++) { $line = explode(':', $lines[$i], 2); if (!array_key_exists(1, $line)) { continue; } $key = trim($line[0]); $value = trim($line[1]); // What we want are MHZ, Vendor, and Model. switch ($key) { // CPU model case 'model name': case 'cpu': case 'Processor': $cpu['Model'] = $value; break; // Speed in MHz // Speed in MHz case 'cpu MHz': $cpu['MHz'] = $value; break; case 'Cpu0ClkTck': // Old sun boxes $cpu['MHz'] = hexdec($value) / 1000000; break; // Brand/vendor // Brand/vendor case 'vendor_id': $cpu['Vendor'] = $value; break; // CPU Cores // CPU Cores case 'cpu cores': $cpu['Cores'] = $value; break; } } $phrase .= " Processor : " . $cpu['Model']; return $phrase; } else { return 'This function work only on a Windows system. :('; } }