PHP脚本:查看CPU的即时使用状况

[root@9c9c cpu]# cat cpu.php

</span><span style="font-family:'宋体';">进程</span><span>CPU</span><span style="font-family:'宋体';">占用比率分布</span><span>

$mysql_host="localhost";

$mysql_user="gestation";

$mysql_password="9cyunyuxitong";

$mysql_database="system";

$mysql_table="memcpu";

$link = mysql_pconnect($mysql_host, $mysql_user, $mysql_password) or die("Could not connect");

mysql_select_db($mysql_database) or die("Could not select database");

 

$query_flush="DELETE FROM ".$mysql_table;

mysql_query($query_flush,$link) or die("Flush failed");

 

$list=`/bin/ps -auxc`;

$lines=explode("/n",$list);

$linenu=count($lines);

for($i=1;$i<$linenu;$i++){

   $fields=explode(" ",$lines[$i]);

   $k=0;

   for($j=0;$j

        if(strlen($fields[$j])>0){

            if($k<11){

                 $field[$i][$k]=$fields[$j];

            }

            $k++;

        }

   }

}

for($i=1;$i<$linenu-1;$i++){

   #echo "

";

   $query_insert="INSERT INTO ".$mysql_table." VALUES(";

   for($jj=0;$jj<11;$jj++){

         #echo "

";

         if($jj<10){$query_insert=$query_insert."/"".$field[$i][$jj]."/",";}

         if($jj==10){$query_insert=$query_insert."/"".$field[$i][$jj]."/")";}

   }

   mysql_query($query_insert,$link) or die("INSERT DATA FAILD!!");

}

 

$query_select="select user,pid,cpu,mem,vsz,rss,tty,stat,start,time,command from memcpu order by cpu desc,command";

$result=mysql_query($query_select,$link) or die("SELECT Failed!!");

$num_rows=mysql_num_rows($result);

while($row=mysql_fetch_array($result,$link)){

echo "

";

for($i=0;$i<11;$i++){

   echo "

";

}

echo "

";

}

 

?>

USERPID%CPU%MEMVSZRSSTTYSTATSTARTTIMECOMMAND
".$field[$i][$jj]."
".$row[$i]."

你可能感兴趣的:(PHP脚本:查看CPU的即时使用状况)