note 109451 modified in function.sys-getloadavg by yannick

From: Date: Fri, 20 Jul 2012 14:03:23 +0000
Subject: note 109451 modified in function.sys-getloadavg by yannick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189321@lists.php.net to get a copy of this message
You can emulate loadavg using this. Can also be used to get iowait

<?php
 function ProcStats()
 {	
	 $fp=fopen("/proc/stat","r");
	 if(false===$fp)
	 		return false;
	 $a=explode(' ',fgets($fp));
	 array_shift($a); //get rid of 'cpu'
	 while(!$a[0])
 		array_shift($a); //get rid of ' '
	 var_dump($a);
	 fclose($fp);
	 return $a;
 }

 $a=ProcStats();
 sleep(5);
 $b=ProcStats();

 
 $total=array_sum($b)-array_sum($a);
 
 $loadavg = round(100* (($b[0]+$b[1]+$b[2]) - ($a[0]+$a[1]+$a[2])) / $total, 2); // user+nice+system
 $iowait= round(100* ($b[4] - $a[4])/$total,2);

?> --was-- You can emulate loadavg using this. Can also be used to get iowait function ProcStats() { $fp=fopen("/proc/stat","r"); if(false===$fp) return false; $a=explode(' ',fgets($fp)); array_shift($a); //get rid of 'cpu' while(!$a[0]) array_shift($a); //get rid of ' ' var_dump($a); fclose($fp); return $a; } $a=ProcStats(); sleep(5); $b=ProcStats(); $total=array_sum($b)-array_sum($a); $loadavg = round(100* (($b[0]+$b[1]+$b[2]) - ($a[0]+$a[1]+$a[2])) / $total, 2); // user+nice+system $iowait= round(100* ($b[4] - $a[4])/$total,2); http://php.net/manual/en/function.sys-getloadavg.php

« previous php.notes (#189321) next »