evilmoe
Goto Top

CPU Auslastung unter Debian (nicht loadavg)!

Hallo,


ich möchte gerne die CPU Auslastung haben die unter "top" zu sehen ist. Woher nimmt "top" denn diese Werte? Der Grund war ich nicht "top" haben möchte ist:

Unter PHP funktioniert shell_exec("top -n 1"); nicht. Ich kriege einfach keine Werte zurück. Sonst funktioniert alles nur das nicht...

Danke..

Content-ID: 83808

Url: https://administrator.de/forum/cpu-auslastung-unter-debian-nicht-loadavg-83808.html

Ausgedruckt am: 02.04.2025 um 09:04 Uhr

EvilMoe
EvilMoe 24.03.2008 um 22:03:52 Uhr
Goto Top
Habs doch noch selbst hinbekommen:
function cpu() {
	function _time() {
		$shell = shell_exec('cat /proc/stat | grep "cpu"');  
		
		$parts = explode(" ", preg_replace("!cpu +!", "", $shell));  
		
		$return = array();
		$return['user'] = $parts;  
		$return['nice'] = $parts[1];  
		$return['system'] = $parts[2];  
		$return['idle'] = $parts[3];  
		return $return;
	}
	$time1 = _time();
	sleep(1);
	$time2 = _time();
	
	$delta = array();
 
	foreach ($time1 as $k=>$v) {
		$delta[$k] = $time2[$k] - $v;
	}
 
	$deltaTotal = array_sum($delta);
	$percentages = array();
 
	foreach ($delta as $k=>$v) {
		$percentages[$k] = round($v / $deltaTotal * 100, 2);
	}
	return $percentages;

}