In my current project, I’ve been looking into website performance and measuring how long it takes for webpages to be composed. Although there are plenty of tools both commercial and non-commercial which is available to do just that, they either cost too much or is a one-time data capture tool (which would be quite laborious if we have to constantly trigger it every 5 mins manually).

Instead I investigated on Cacti to see if this commonly used tool by Network/System Admin folks for monitoring network devices and systems can be reworked to let me monitor webpage loading time.

And it did wonderfully…

Cacti is a great application written in PHP, which wraps around the rrdtool. I’m not going to go into what rrdtool is, you can find out more at the rrdtool website.

What I liked about using Cacti and rrdtool is scalability and performance. If I’m going to be polling for readings every 5 mins, I would want a database which will be able to handle the large number of data points. Also, Cacti provides a means to allow me to create my own custom monitoring agents. With this feature, I can pretty much create any kind of monitoring agents I like.

This post will be split into 2 3 parts. Part 1 will walk through the php agent script which you can download from here, and how to test that it’s usable for you. In Part 2, I will cover how to configure Cacti to use the agent script, and to start collecting the data into an rra file. In Part 3, I will cover how to create a graph from the data collected in Part 2 and to draw nice looking graphs like this..

AskAboutPHP Homepage Stats

To begin with…
Needless to say, you need to have a copy of Cacti running in your own environment. In my case, I have Ubuntu 8.04 and I just used the Synaptic Package Manager to install a copy. For more installation options, check out the Cacti website.

Next, go grab my script files from here. Inside you should find 2 php scripts – class.pageload.php and pageload-agent.php. I’ve commented both files so hopefully they should be quite self-explanatory.

pageload-agent.php is the agent file, and it takes in 1 parameter, namely the url to your page you want to monitor. class.pageload.php is where the script logic resides. It uses php CURL library functions to make calls to the url you specified and to return with the timing information.

Install the scripts.
Remember, these scripts are meant to be command line executed, so you don’t need them to be in your apache web directories. Just install them in directories which are accessible by Cacti. In my case, I just placed them into my user home directory, in a directory called ‘pageload’.

~$ mkdir pageload
~$ cd pageload

Next, you need to make pageload-agent.php executable. To do this, just do:

~$ chmod +x pageload-agent.php

Testing the scripts
To test the script is very easy. Decide on the url you want to monitor, in my case I will use AskAboutPHP’s homepage url, and just execute the script like so:

~/pageload$ ./pageload-agent.php

You should get something like this as the output:

dns:0.004425 con:0.006053 pre:0.006063 str:0.117481 ttl:0.180139 sze:30507 spd:169352

This is the format that Cacti is expecting as its data source. Cacti will read these numbers in and store them in the rrd archive files. So what do they mean?

dns: is the time in seconds until name resolving was complete
con: is the time in seconds it took to establish the connection
pre: is the time in seconds from start until just before file transfer begins
str: is the time in seconds until the first byte is about to be transferred
ttl: is the total transaction time in seconds for last transfer
sze: is the total number of bytes downloaded
spd: is the average download speed

This concludes Part 1. In Part 2, I will step through how to get Cacti setup to start using these scripts to collect the webpage loading time.

Download: download icon

Part 1 | Part 2 | Part 3