One of the issues with Cpanel servers, especially if you offer shared hosting, is to monitor the load averages as well as processes and identify resource abuse.
After having little luck in finding any decent monitoring scripts, especially ones which are easy to install, I have written a script in php which can be installed on a cpanel server as the root user and then executed using a cron to run it every 60 seconds.
Installation is simple:
1. adjust the destination email address
2. upload script to root directory
3. chmod the file to 755
4. create a cron job as root user to run the script every minuteIMPORTANT: This script will send you appx 1440 emails a day (60 per hour) and if you dont want to be reading all of them or having them fill up your inbox then you need to either set up a special mail account for it or adjust your own mail settings.
In my case I send the results of the script to my own main email address but I have created a folder inside my mail account called “server_checks” and created a mail rule to move these alert emails to the folder and to mark them as read.
So every day I just quickly scan to see if anything is out of the ordinary.
You could if you wanted to, adjust the script to drop the alert data into a remote database too if you did not want to receive emails!
Once installed, the script then sends an email every minute with a snapshot of the current mysql processes, load averages, current running processes giving you a rough idea what is happening on your server.
If you look at the image below, it shows the contents of a special mailbox I have set up which just gathers the abuse monitoring emails and you can see at a glance you can gauge the load averages for the server.
Below is the contents of the email and as you can see it contains various useful information about the server so in the case you notice at some point that there is some high load on the server, you can see the processes happening at that point in time.
Here is the script.
//#######################################################
// server load checker
// created by craigedmonds.com
// 1. upload this php script to /root
// 2. chmod the file to 755
// 3. in ssh type: crontab -u root -e
// 4. add this line: * * * * * /usr/bin/php /root/craigs-load-checker.php
// 5. restart cron service: /etc/init.d/crond restart
//#######################################################//where we will send the emails
$email_to = “you@yourdomain.com”;
$email_from = “you@yourdomain.com”;//#######################################################
//no need to edit below here
//#######################################################$hostname = trim(shell_exec(‘hostname’));
$this_server_ip = gethostbyname(trim($hostname));
$mysql_processes = shell_exec(‘mysqladmin proc stat’);
$process_list = shell_exec(‘top -b -n 1’);
$uptime = shell_exec(‘uptime’);
$total_port80_connections= shell_exec(‘netstat -plan | grep :80 |wc -l’);//get the load averages from the uptime
$explode_load_averages=explode(“load average:”,$uptime);
$explode_load_averages_again=explode(“,”,$explode_load_averages[1]);
$load_avg_current=trim($explode_load_averages_again[0]);
$load_avg_5mins=trim($explode_load_averages_again[1]);
$load_avg_15mins=trim($explode_load_averages_again[2]);//send the email
$subject = $hostname . ” service CHECKER – ” . $uptime;
$address = $email_to;
$headers =
“From: ” . $email_from. “\r\n” .
“Reply-To: ” . $email_from. “\r\n” .
“X-Mailer: PHP/” . phpversion();
$body =”#SERVER_INFO\r\n”;
$body.=”Hostname: ” . $hostname . “\r\n”;
$body.=”IP: ” . $this_server_ip;
$body.=”\r\n\r\n”;
$body.=”#LOAD_AVERAGES\r\n”;
$body.=”Current Load Average: ” . $load_avg_current . “\r\n”;
$body.=”5 Minute Load Average: ” . $load_avg_5mins . “\r\n”;
$body.=”15 Minute Load Average: ” . $load_avg_15mins;
$body.=”\r\n\r\n”;
$body.=”#TOTAL_CONNECTIONS\r\n”;
$body.=”Port 80: ” . $total_port80_connections;
$body.=”\r\n\r\n”;
$body.=”#MYSQL_PROCESS_LIST\r\n”;
$body.= $mysql_processes;
$body.=”\r\n\r\n”;
$body.=”#PROCESS_LIST\r\n”;
$body.= $process_list;
$body.=”\r\n\r\n”;
$body.=”#end of notification”;
mail($address, $subject, $body, $headers);exit;