One of the biggest annoyances to me when doing research on the internet is when I find an enticing summary from a search engine query, but, when I try to access the resource, the website is not accessible because [the database server was down | enter your reason here]. This is why for the year of 2009, I am going to be more proactive in monitoring my server’s reliability.
I chose Pingdom, a company that specializes in uptime monitoring services. The 70% off holiday special didn’t hurt that decision either. For a measly $35 this year, I will be notified the minute (literally) that there is a problem with my server. [Track my server (Swift) uptime]
One of the relatively new features that Pingdom added to their services is the ability to call your own scripts on the server and set your own status report. This allows more comprehensive tests that go beyond a simple ping. I use this functionality to check the status of services that I don’t want accessible on the internet directly (e.g. MySQL) as well as server vitals like RAM usage. Here are two simple PHP scripts that demonstrate how I am using the services from Pingdom with their HTTP Custom check.
pingdom-core.php
This is my core services check. It makes sure that web services and MySQL are operational. Simply configure the script with the username and password of a MySQL database. If the script cannot make a connection to the database server, then a ‘MYSQL DOWN’ status will be returned to Pingdom. Of course, if the script doesn’t respond at all, then the web service is down and Pingdom will report a down status.
pingdom-server.php
This script is intended to be an early warning system. It will raise the alarm if something looks out of the ordinary so that I can be prepared for a possible service failure. The following server vitals are checked: swap usage, hard drive usage, and cpu usage. If any one of these goes beyond the preset thresholds, a down status will be returned to Pingdom.
You might be thinking that checks like these could be done with a monitoring script on the server without using Pingdom at all, and there is merit to that argument. However, if you run out of memory and your system comes crashing to a halt, that probably means that services like email, which you would have used to alert yourself to the problem, would no longer be working. By performing these checks remotely, you can sleep a little easier. Plus, Pingdom will generate nifty looking graphs, and I like graphs.
pingdom.php
–Added on 12/4/2009–
This script is a combination of pingdom-core.php and pingdom-server.php if you’re using the free acount
Ideas for the future
I would like to check the Apache or Nginx status reports to determine how many connections there are. This may be useful in detecting a digg or slashdot to temporarily boost resources before it becomes a problem.
How do you track your server’s reliability? Leave a comment below.
AWESOME – Thanks so much for the pingdom.php file – it works perfectly and you saved me many hours of trying to write this myself.
Nice website – excellent content!
Thanks for the script. Your efforts are greatly appreciated!
Great script however after setting this up, Pingdom send Up and Down alerts. Should it not be passing the string so if MySQL is down for example the alert would state that instead of just saying Down? Cheers!
I had to change the $totaltime to as follows, otherwise Pingdom was displaying a “?” for Response Time:
$totaltime = round(($endtime – $starttime) * 1000000); // Time in milliseconds
Thanks!
This comment stands true in 2021.
Great scripts – thanks.
Is there any reason not to use:
$hdUsage = shell_exec(“df -h | grep “.$HD_MOUNT.” | awk ‘{print $5}’ | perl -pe ‘s/%//'”);
So you can make use of the $HD_MOUNT variable?
I’m not familiar with that–is it a global PHP variable? Google didn’t turn up any ready results for that variable and my local system says it doesn’t exist. Do you have a doc link? But if it works and points to the right mount point for you, go for it! 😀
Great Script! Thanks.
Very useful – thanks.
Hi
I just wonder, what is the best value for the $CPU_THRESHOLD ?. How to determine the cpu threshold?
Well, there are several things to consider. The first is the number of CPUs and cores you have. The CPU threshold is not a percentage, but average load which is a bit tricky. I suggest reading up on the wikipedia page to better understand load averages. Then it’s just a matter of judgement and personal preference as to what limit you want to be notified about. A lot is dependent on how you’re using the server in the first place. I use the threshold primarily as an early indicator of a runaway process, so it’s set pretty high.
Hi Jon,
Your post is very helpful. ‘pingdom.php’ is brilliant!
I was wondering if you have the script to monitor CPU usage as well.
MaryAnne,
That part is in the pingdom-server.php file. Essentially the line is `uptime | awk ‘{print $10}’ | perl -pe ‘s/,//’`
oh, right! I didn’t know about the backtick operator having that functionality, I was planning to remove it.
Sorry for the false alarm.
Ahh, the backtick operator executes those lines as a shell command, so exec() is not needed. However, in hindsight the return should be cast to an integer otherwise the conditional won’t work:
$hdUsage = (int)(`df -h | grep ‘/dev/sda1′ | awk ‘{print $5}’ | perl -pe ‘s/%//’`);
Hi Jon,
All the lines are in this format:
$hdUsage = `df -h | grep ‘/dev/sda1’ | awk ‘{print $5}’ | perl -pe ‘s/%//’`;
if ($hdUsage > $HD_THRESHOLD)
$status = ‘HARD DRIVE OVER THRESHOLD’;
I’m assuming you wanted:
$hdUsage = exec(`df -h | grep ‘/dev/sda1’ | awk ‘{print $5}’ | perl -pe ‘s/%//’`);
So that the string is actually executed and you are comparing the return result…?
Steve, where is the exec() missing?
I’m a bit too lazy to remove the unused $HD_MOUNT since it doesn’t break anything, but it was originally intended for the hard drive usage check so that /dev/sda1 wasn’t hardcoded.
ummm didn’t you leave out exec() on the current version?
Additionally the $HD_MOUNT variable is unused.
Otherwise, thanks this has been a big help.
Excellent Work. Very helpful.
I added a new pingdom.php script which combines the previous two for use with a free Pingdom account.
Very helpful! Thank you a lot 🙂
thanks for scripts. extremely helpful!
Very nice – thanks!