How I use Pingdom’s HTTP Custom Feature

December 26, 2008 · 19 comments

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

[download 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

[download 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–

[download pingdom.php]

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.

Be Sociable, Share!

{ 17 comments… read them below or add one }

David Gitman January 30, 2012 at 3:58 pm

Great Script! Thanks.

Reply

Simon December 8, 2011 at 12:22 am

Very useful – thanks.

Reply

napi March 6, 2011 at 1:15 am

Hi
I just wonder, what is the best value for the $CPU_THRESHOLD ?. How to determine the cpu threshold?

Reply

Jon Stacey March 6, 2011 at 12:46 pm

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.

Reply

MaryAnne February 8, 2011 at 9:05 pm

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.

Reply

Jon Stacey February 8, 2011 at 9:41 pm

MaryAnne,

That part is in the pingdom-server.php file. Essentially the line is uptime | awk '{print $10}' | perl -pe 's/,//'

Reply

Jim Carter III January 10, 2011 at 5:20 pm

just what i was looking for, thanks for sharing.

minor addition, i threw in a: header(“content-type: text/xml”); to prettify the output.

Reply

Steve December 1, 2010 at 11:47 pm

oh, right! I didn’t know about the backtick operator having that functionality, I was planning to remove it.

Sorry for the false alarm.

Reply

Jon Stacey December 1, 2010 at 11:36 pm

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/%//’);

Reply

Steve December 1, 2010 at 11:07 pm

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…?

Reply

Jon Stacey December 1, 2010 at 10:27 pm

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.

Reply

Steve December 1, 2010 at 10:20 pm

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.

Reply

Louie Miranda November 3, 2010 at 7:23 pm

Excellent Work. Very helpful.

Reply

Jon Stacey December 4, 2009 at 5:43 pm

I added a new pingdom.php script which combines the previous two for use with a free Pingdom account.

Reply

Fouad November 24, 2009 at 1:45 pm

Very helpful! Thank you a lot :)

Reply

Daniel October 5, 2009 at 12:47 pm

thanks for scripts. extremely helpful!

Reply

Quentin Stafford-Fraser June 13, 2009 at 4:42 am

Very nice – thanks!

Reply

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post: