Web Hosting Talk







View Full Version : Memory/CPU Intensive Process Notification Script for cPanel


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('&nbsp;',$status[$i]); $processes[$k]['user']=$value[1]; }
//8 (priority)
if($l==12) { $value=explode('&nbsp;',$status[$i]); $processes[$k]['priority']=$value[1]; }
//12 (cpu usage)
if($l==12) { $value=explode('&nbsp;',$status[$i]); $processes[$k]['cpu']=$value[1]; }
//16 (memory usage)
if($l==16) { $value=explode('&nbsp;',$status[$i]); $processes[$k]['mem']=$value[1]; }
//18 (command)
if($l==18) { $value=explode('&nbsp;',$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.

MikeDVB
04-13-2010, 10:20 PM
Nice touch but I can see a simpler way to do it - if I find some extra time I'll try to clean the code up and post it up.

fremio
04-13-2010, 10:46 PM
Looking forward to it.

Kinda just threw this together for a quick fix. If you have a way that's not dependent on WHM's HTML output and can be easily used to check multiple servers I'd love it.

MikeDVB
04-13-2010, 10:49 PM
I'd probably just do a "ps aux" and parse that :)

If I get time I'll toss it together probably in perl.

mellow-h
04-14-2010, 12:03 AM
Cpanel DCPUMON uses "top" to find out the high CPU usage users, similar to the one you are generating. Is that the mail functionality why you are not using that?

BH-Greg
04-14-2010, 12:16 AM
This is good after all, I would like to see Mikes once its done. :)

MikeDVB
04-14-2010, 12:19 AM
Cpanel DCPUMON uses "top" to find out the high CPU usage users, similar to the one you are generating. Is that the mail functionality why you are not using that?

Semi-off-topic but anybody wonder why it's not cpumond and not dcpumon? :)

mellow-h
04-14-2010, 12:22 AM
Semi-off-topic but anybody wonder why it's not cpumond and not dcpumon? :)
Because Cpanel developer doesn't want it to be :P

MikeDVB
04-14-2010, 12:28 AM
I mean... mysqld, httpd, sshd, proftpd, pureftpd....... dcpumon???

mellow-h
04-14-2010, 12:29 AM
DCPUMON is developed by cpanel, doesn't it depend on them whether they would like to call it a daemon or not? :)

MikeDVB
04-14-2010, 12:32 AM
DCPUMON is developed by cpanel, doesn't it depend on them whether they would like to call it a daemon or not? :)
I'm just saying... why not just call it "cpumon" then... why put the d in front? I know it doesn't really matter but it's just a bit strange imho :)

mellow-h
04-14-2010, 12:37 AM
I'm just saying... why not just call it "cpumon" then... why put the d in front? I know it doesn't really matter but it's just a bit strange imho :)
I think it means "Daily CPU Monitor", d doesn't stand for daemon here :P

net
04-14-2010, 12:39 AM
Moved > Hosting Security and Technology Tutorials.

fremio
04-14-2010, 01:18 AM
I'd probably just do a "ps aux" and parse that :)

If I get time I'll toss it together probably in perl.

The advantage of this over that is that you can use this on your main server to find out the user from multiple servers.

I set it up to text me when it finds an intensive process working great.

fremio
04-14-2010, 01:28 AM
Cpanel DCPUMON uses "top" to find out the high CPU usage users, similar to the one you are generating. Is that the mail functionality why you are not using that?

Can DCPUMON be accessed from outside of the server without ssh?

mellow-h
04-14-2010, 01:30 AM
Can DCPUMON be accessed from outside of the server without ssh?
Yes, but it has some different set of structure. It can be viewed here:

/usr/local/cpanel/bin/dcpumonview

PhotonServers
05-01-2010, 04:53 PM
I would do the best method and the fastest which is what Mr.MikeDVB mentioned to do a ps aux on ssh. It will give you a list of who is using alot of memory and cpu usage. :)

HalfDollarHosting
11-08-2011, 06:28 PM
Hi, I know this thread is old, but is there any new version of script, new tool for monitoring?
ty