fremio
04-13-2010, 10:17 PM
In response to a large percentage of signups to my hosting service choosing to run Rapidleech on their accounts, I've created a script that runs as a cron job and checks all servers most CPU intensive processes returning them in an array, ordered by % CPU usage.
Here is the function:
function topCpu($server,$user,$pass,$num=10)
{
$number=71+$num*9; //9 lines per process record. 71 is the line where they begin.
$servup = file("http://$user:$pass@$server:2086/scripts2/top"); //login to WHM CPU usage script which conveniently orders cpu processes by highest % load
for($i=71;$i<$number;$i++) //read lines showing only first few processes
{
$status .= "$servup[$i]"; //read into string
}
$status=explode('>', $status); //explode into array kind of based on html tags
$processes=array();
$j=0; //counter for value in process
$k=0; //counter for process
for($i=0;$i<count($status);$i++) //for loop for values separated by > (html code)
{
$l=$i-($k*20); //get the array key adjusted for each process (each process is separated by 20 array keys - just how the html code works out
//get pid (line 3)
if($l==3) { $value=explode('<',$status[$i]); $processes[$k]['pid']=trim($value[0]); } //the html around pid is a tiny bit different
//collect line 6 (user)
if($l==6) { $value=explode(' ',$status[$i]); $processes[$k]['user']=$value[1]; }
//8 (priority)
if($l==12) { $value=explode(' ',$status[$i]); $processes[$k]['priority']=$value[1]; }
//12 (cpu usage)
if($l==12) { $value=explode(' ',$status[$i]); $processes[$k]['cpu']=$value[1]; }
//16 (memory usage)
if($l==16) { $value=explode(' ',$status[$i]); $processes[$k]['mem']=$value[1]; }
//18 (command)
if($l==18) { $value=explode(' ',$status[$i]); $processes[$k]['command']=$value[1]; }
$j++;
if($j==20)
{
$k++; //once you get all the process information, set it to the next record
$j=0; //reset the counter for the individual process values
}
}
return $processes;
}
$processes=topCpu('server.hostname.or.ip','username','password','10');
for($i=0;$i<count($processes);$i++)
{
$process=$processes[$i];
//Here you can do whatever you need to do, like set an if statement to only do something if a process doesn't belong to root and is using above a certain percentage of CPU.
echo("$process[pid] $process[cpu] - $process[mem] - $process[user] - $process[command]<br />");
}
Run a loop through all your servers and set the script to mail() you a text message with the user, command, and cpu %.
Hope this helps somebody out, I know it's helping me keep abuse to a minimum on my servers.
Yea, if cPanel decides to significantly alter the text on that script it'll have to be modified a little bit, so don't use it for very essential functions on your host.
Here is the function:
function topCpu($server,$user,$pass,$num=10)
{
$number=71+$num*9; //9 lines per process record. 71 is the line where they begin.
$servup = file("http://$user:$pass@$server:2086/scripts2/top"); //login to WHM CPU usage script which conveniently orders cpu processes by highest % load
for($i=71;$i<$number;$i++) //read lines showing only first few processes
{
$status .= "$servup[$i]"; //read into string
}
$status=explode('>', $status); //explode into array kind of based on html tags
$processes=array();
$j=0; //counter for value in process
$k=0; //counter for process
for($i=0;$i<count($status);$i++) //for loop for values separated by > (html code)
{
$l=$i-($k*20); //get the array key adjusted for each process (each process is separated by 20 array keys - just how the html code works out
//get pid (line 3)
if($l==3) { $value=explode('<',$status[$i]); $processes[$k]['pid']=trim($value[0]); } //the html around pid is a tiny bit different
//collect line 6 (user)
if($l==6) { $value=explode(' ',$status[$i]); $processes[$k]['user']=$value[1]; }
//8 (priority)
if($l==12) { $value=explode(' ',$status[$i]); $processes[$k]['priority']=$value[1]; }
//12 (cpu usage)
if($l==12) { $value=explode(' ',$status[$i]); $processes[$k]['cpu']=$value[1]; }
//16 (memory usage)
if($l==16) { $value=explode(' ',$status[$i]); $processes[$k]['mem']=$value[1]; }
//18 (command)
if($l==18) { $value=explode(' ',$status[$i]); $processes[$k]['command']=$value[1]; }
$j++;
if($j==20)
{
$k++; //once you get all the process information, set it to the next record
$j=0; //reset the counter for the individual process values
}
}
return $processes;
}
$processes=topCpu('server.hostname.or.ip','username','password','10');
for($i=0;$i<count($processes);$i++)
{
$process=$processes[$i];
//Here you can do whatever you need to do, like set an if statement to only do something if a process doesn't belong to root and is using above a certain percentage of CPU.
echo("$process[pid] $process[cpu] - $process[mem] - $process[user] - $process[command]<br />");
}
Run a loop through all your servers and set the script to mail() you a text message with the user, command, and cpu %.
Hope this helps somebody out, I know it's helping me keep abuse to a minimum on my servers.
Yea, if cPanel decides to significantly alter the text on that script it'll have to be modified a little bit, so don't use it for very essential functions on your host.
