Home >Backend Development >PHP Tutorial >PHP code for recording server load, memory, and cpu status
This article introduces a piece of code that uses PHP to record server load, memory usage, and CPU status. Friends in need can refer to it.
By calling the system command top, and then using the function explode, the server load, memory usage, current CPU status and other information can be recorded. The code is as follows: <?php /** * 记录服务器负载、内存使用、cpu状态 * 每10秒检测一次 * edit by bbs.it-home.org */ while(1){ exec('top -b -n 1 -d 3',$out); $Cpu = explode(' ', $out[2]); $Mem = explode(' ', $out[3]); $Swap = explode(' ', $out[4]); //var_dump($Cpu,$Mem,$Swap); $cpu = str_replace(array('%us,',' '),'',$Cpu[1]); $mem = str_replace(array('k used,',' '),'',$Mem[2]); $swap = str_replace(array('k cached',' '),'',$Swap[5]); echo date('md H').' '.$cpu.' '.intval($mem/1024).' '.intval($swap/1024).chr(10); sleep(10); } ?> |