Cacti: Using Cacti to monitor web page loading – Part 1
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..

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 ~/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 http://www.askaboutphp.com/
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.
In: PHP Tutorials · Tagged with: cacti, monitoring, rrdtool

Permalink
Hey, thanks- this looks great. Looking forward to part two.
Permalink
I get this error when testing your script.
“PHP Notice: Undefined offset: 0 in /root/pageload/pageload-agent.php on line 37″
Any Ideas? Your work looks excellent and I have a real use for this!
Permalink
Hi Joe
Regarding your error. It probably means that getPageLoadStats() at line 29 is not returning any valid results.
Can you check your php environment to see if you are able to use the cURL library has been enabled?
Permalink
do you know where I can find php script that loads complete web page, including java scripts and images and measures loading time?
Best regards
Permalink
this doesnt provide true load time experience indicator
Permalink
No, it doesn’t but this is the best I know under a linux server environment. I have another solution using IE but it only runs on a Windows platform.
Do share if you have a solution. Would be happy to change mine.
Permalink
I am getting this error when I try to test a test URL. Any idea what might be causing this. I am a newbie in php so please forgive my ignorance.
PHP Notice: Undefined index:content_type in /home/user/pageload/class.pageload.php on line 51
Your tutorial looks interesting and I am hoping to implement this in production.
Permalink
hi Storm,
php is telling you that there’s no such index called ‘content_type’ in the $info array at line 51.
i suspect that the server you are querying did not have an appropriate ‘Content-Type’ in the header.
see the last bullet point for curl_getinfo doc at
http://www.php.net/manual/en/function.curl-getinfo.php
my script did not check for this. maybe you can just check for whether it is NULL or not.
thanks
Permalink
Hello,
Got the exact same error as above.
I have no experience with scripting in PHP so little knowledge regarding what to do to remedy the error
[root@localhost pageload]# ./pageload-agent.php http://www.askaboutphp.com
dns:0.335653 con:0 pre:0 str:0 ttl:0 sze:0 spd:0[root@localhost pageload]#
PHP Notice: Undefined index: content_type in /root/pageload/class.pageload.php on line 51
So is ther any chance of some help regarding this issue as i would like to see this ran in a production enviroment.
Thanks in advance
Permalink
hi guys, the
$s['content_type'] = $info['content_type'];
at line 51 is not that important for timing. so it’s pretty safe to just comment it out or do a check to see if the key exists or not.
Permalink
Hi
I made a template so it is easy to import into cacti.
If you want it, let me know.
Permalink
Daniel,
Can you please share your templates ?
Permalink
Hi Daniel,
I really would like that template to – please upload it somewhere?
Permalink
Hi All,
thanks for this guy for develop this plugin for monitoring URL’s, i appreciate.
So, I did a template, if anyone wanna this plugin, please, send a e-mail.
Thanks a lot.
Permalink
Hi Fabiano,
Could you please send me this template ?
Thanks a lot
Permalink
I’d like to see your template as well. I would appreciate it.
Permalink
The template for this would be nice, I too would like a copy.
Permalink
是个好东西,谢谢。