Monitor Your Servers and EC2 Instances with Cacti

I recently had the job of setting up cacti on an Ubuntu an Amazon EC2 Instance, so I thought I’d share with you all how to get it running, and what you can monitor with it.

cacti-header

Note: This tutorial is designed for an ubuntu based server, but will translate to most unix setups

1. How cacti works

Cacti connects to servers using various protocols (mainly SNMP) to query it for certain data, such as load, memory, disk use, etc. But, it’s possible to run custom scripts to query pretty much anything, as I’ll demonstrate here.

2. The Production Environment

For now, we’ll assume that we have 3-4 servers in a ‘production’ environment.
Here’s an example of how we could be using them:

  1. Webserver #1 (web1)
  2. Webserver #2 (web2)
  3. Web Load Balancer (balance1)
  4. Database Server (db1)

This is a pretty standard ‘cluster’ for larger production environments, but don’t worry if you only have one machine… you can follow these instructions for a single machine, and still get some useful data.

Example ‘Production’ Setup

server-diagram

Firstly, we’ll need a nice new machine for monitoring. I’m going to be using an Ubuntu 8.10 Intrepid Ibex install (all my ‘servers’ are actually ec2 instances, but it’s all the same).

We’ll call our new machine/instance ‘monitor’ (very original!)

3. Setting up our ‘Monitor’

If you already have a server with a standard lamp stack on as your ‘monitor’ server, you can skip right down to point 4. Otherwise, let’s get some LAMP (Linux Apache MySQL PHP) on this machine.

Although we’re not going to be serving ‘production’ websites from our ‘monitor’ server, cacti is in-fact a PHP web application, so we still need a LAMP setup to run it.

Here’s what I’m going to install (ubuntu packages).

apt-get install apache2 php5 php5-curl php5-mysql libapache2-mod-php5 mysql-server

During this process you may get prompted to enter a root mysql password. Enter one, and write it down somewhere (safe).

Once these are all downloaded and installed, you should have a fully functioning LAMP setup. Test it by visiting: http://monitor-server-ip/

(If you’re using EC2, you may need to open port 80 first).

3. Installing Cacti

There’s a few different ways to install cacti, you can check for more distro-specific install instructions here.

The simplest way to install cacti is using a package manager, in our case apt-get.
We’re going to install both cacti, and its polling binary (a program which actually goes and checks for data). This second program isn’t essential, since cacti can use php to poll for data, but ‘spine’, as the binary is called, is much much quicker!

apt-get install cacti-cactid

This should download the cacti package and set about installing it.

You will be asked if you want to automatically setup a webserver, choose apache2 (it didn’t work for me).
Then, it’ll ask you for your mysql root password… enter that now.
And, finally, it’ll prompt you to enter a new cacti mysql user password.
I usually leave this blank, and let it generate a random one for me.

So far, so good. but we need to make cacti accessible from the web, otherwise it’s useless to us!
First of all, let’s find where cacti is!

updatedb
locate poller.php
/usr/share/cacti/site/poller.php
/usr/share/cacti/site/lib/api_poller.php
/usr/share/cacti/site/lib/poller.php

For this environment, we’ve got cacti here: /usr/share/cacti/site/

We’re going to use an ‘Alias’ in apache2 to forward all requests for http://xxx.xxx.xxx.xxx/monitor to cacti.

Enable mod_alias for apache2

a2enmod alias

It’s quite likely you already have this installed.

Now, edit your httpd.conf (or for ubuntu, your sites-enabled/site file), and add the following line.

Alias /monitor /usr/share/cacti/site

(Replacing /usr/share/cacti/site with the path you found earlier using ‘locate’)

Looking good, now let’s restart apache, and visit our site at http://monitor-server-ip-etc/monitor

/etc/init.d/apache2 restart

4. Basic Cacti configuration

Once you visit your cacti install for the first time, you should get see a login box similar to this:

cacti-login

Login with using the default details
username: admin
password: admin

You’ll be asked to create a new password to use for logging in to cacti. Think of something good, and again, write this down!

Once you’re password is changed, you should see a screen with lots of ‘paths’ to certain programs. If any are not found, try installing them with apt-get or yum. If all the paths are found, then you’re in business. Click next, and you’ll be looking at cacti for the first time!

cacti-screen

5. Basic Cacti Configuration

Now that we have cacti installed on our machine, we need to get a few basics set up.
First of all, we’re going to change our poller to ‘spine’ (if you’re using spine), since it’s quite a bit quicker.
To do this, choose ‘settings’, then ‘poller’, and change your poller from ‘cmd.php’ to ‘spine’.

cacti-spine-selection

It’s worth making sure that both the intervals below this are set to ’5 minutes’, that’s the 2 dropdown boxes below this one.

By default, cacti sets up some monitoring on the machine it’s installed on. But, this might be a little confusing for us. So if you’re not monitoring the machine cacti is installed on, delete the standard ‘localhost’ device.

To do this, choose ‘devices’ from the menu on the right, and delete the only entry in there. (along with any graphs associated with it).

We should now have a clean slate to start with. Even if you are monitoring the machine you have cacti installed on, I’d still recommend following the step above, since we’re going to connect to our servers a little differently :)

Before we get cracking with talking to our servers (the ones we want to monitor and graph), we need to add a task to the crontab on our ‘monitor’ server.

crontab -e
*/5 * * * * /usr/bin/php /usr/share/cacti/site/poller.php

Remember: Replace /usr/bin/php with your php path (you can find it by using):

which php
/usr/bin/php

Replace /usr/share/cacti/site/ with your cacti site path (we found that earlier to setup our apache alias).

With our cron saved, our server is will now start ‘doing stuff’ every 5 minutes. So, let’s login to one of the machines we wish to monitor.
I’m going to be starting with web1, but you might want to start with any other server, even ‘monitor’ (localhost).

6. Setting up Target Servers (to monitor)

On our ‘target’ machines, we need to install snmpd:

sudo apt-get install snmp snmpd

This will install the Simple Network Management Protocol Daemon on the server, enabling us to connect to it and as ‘questions’.

But, the target machine isn’t ready yet! We need to configure SNMPD.
For the sakes of simplicity, I’ve created some commands to get it done automatically:

mv /etc/snmp/snmpd.conf /etc/snmp/old.snmpd.conf
echo "rocommunity public" > /etc/snmp/snmpd.conf
sed -i 's/127.0.0.1//g' /etc/default/snmpd
/etc/init.d/snmpd restart

This SNMP setup isn’t the most secure, infact, other people could get your SNMP data, so I’d recommend looking into SNMP2 authentication. But, for now this is just a tutorial!
To add a little more security, you can change the ‘rocommunity’ name to something more obscure… like this:

echo "rocommunity mybigasscoolstring" > /etc/snmp/snmpd.conf

So, we install SNMPD and configure it as above on all the servers we’re going to be monitoring.

7. Adding the Target to Cacti

Fire up your web browser and login to your cacti application.

Choose ‘Devices’ on the left, the ‘Add’ on the top-right’

cacti-device-add

Now, we need to fill in the form correctly, otherwise, we’ll never be able to talk to our target device!

Enter a description that you’ll be able to remember… I’ve chose EC2 Instance Web #1.
Enter the hostname or IP of the target machine. It’s crucial that this is right. I’m going to use ‘localhost’, but the screenshot shows an external hostname for illustration.
!! Warning !! This screenshot is wrong! Device type should be: ‘ucd/net SNMP Host’ !
cacti-new-device
!! Warning !! This screenshot is wrong! Device type should be: ‘ucd/net SNMP Host’ !

Set the device type to ‘ucd/net SNMP Host’, and SNMP Version to 1…
Port 161 is the default SNMP port. This will have to be open on the target machine for it to get the SNMP requests.
Finally, set the ‘community’, if you changed it earlier from ‘public’ to something else (like: ‘mybigasscoolstring’), then set the same string here.

That’s it, click the ‘create’ button!

The next page is confusing, it looks the same, but has this at the top.

save-success

Scroll down to the bottom of the page, and you should see that there is already a data query associated with this host… ‘SNMP Interface Statistics’.
If everything’s working, the status of this query should say ‘Success, x Items, y Rows’

cacti-data-queries-pre

The interface statistics are stats about ethernet input/output, overall, pretty useful, but before a short break, let’s add some more from the dropdown.
Add ‘SNMP – Get Mounted Partitions’, click add, and do the same for ‘SNMP – Processor Info’

Once you’re done with that, the bottom of the page should look like this:

cacti-data-queries-post

Time for a Recap

All the work so-far has been quite intense, with little to show for it. I know, you may be feeling quite bored right now. But we have made some great progress!

Here’s what you’ve done so far.

  1. Installed cacti on ‘monitor’ machine
  2. Configured ‘target’ machine to accept SNMP connections
  3. Configured cacti to talk over SNMP to our ‘target’ machine
  4. Got some data from our ‘target’ machine (that’s the ‘success’ bits)

So, now that you know what you’ve done so far, let’s get cracking again. Now we can start to get some graphs going!

Building our first graphs

Now that cacti can talk to our target machine, and get some useful data, we want to get that graphed out.

Two of the first things we’d usual graph are server load, and memory use. These are some of the most critical aspects of a server’s performance. But by default, the graphs cacti has for these are pretty dumb.

Luckily, Hans Fugal has realized this, and built some graph templates for us to use, for free! (respect).

Firstly, Download Hans’ load average template.

Now, Download Hans’ memory usage template

These are both XML files, so save them to your computer in their original format.

Using Graph Templates

In cacti, choose the ‘Import Templates’ from the menu on the left.

Choose Hans’ load average XML template and click upload. If all goes according to plan, you should see some success messages like this:

cacti-import-success

Follow the same process for Hans’ Memory usage template.

Adding Graphs for Device

Now that we’ve imported these 2 new templates, we can connect them to the data that we’re collecting via SNMP from our target machine to make an actual graph.

Click on ‘devices’ on the left, then select your target machine we setup earlier.
You should get the same page we were using earlier to add data queries.

Now, we’re going to add graph templates to this device, using the form just above data queries. Add ‘ucd/net – Load Average (Hans)’, ‘ucd/net – Load Average (Hans)’, and ‘Netware – File System Activity’.

Once you’re done, your graph templates and data queries should look like this:

cacti-graph-templates-added

Now, go to the top of the page and click the link ‘Create Graphs for this Host’

The next page can look a little overwhelming, but we’re only going to choose some of the options.

From the top section, we’re going to choose Hans’ templates (highlighted here in yellow)

graph-templates-selected-part-1

And from the interface statistics query, we’ll choose our ethernet adapter:

graph-templates-selected-part-2

Once you’ve selected these 3 options, click the ‘Create’ button at the bottom. You should get a confirmation message saying that they’ve been added.

created-graph

Placing Graphs in Trees

If you click on ‘Graph Management’ on the left, you should now see a list of your 3 graphs for your target machine. Cool!

Now, click on one of them, and see if a graph has been generated.
If not, don’t panic, you have to wait 5 minutes for the cron to build graphs.

I’ve you’ve waited quite a long time and graphs are still not appearing, then try changing your poller from ‘spine’ back to ‘cmd.php’ in your poller options under ‘settings’… and wait 5 more minutes.

If you’re graphs are showing up, you can now add them to a tree, so they’re viewable under the ‘graphs’ tab at the top.

Select your 3 graphs, and choose ‘Add to tree’ from the dropdown box.
Choose the tree you wish to add them to, and click go.

Now, you can click the ‘graphs’ tab at the top and watch your beautiful graphs build over the next 24 hours.

But, if you can’t wait until then, here’s one I made earlier for you to see.

working-graph

Just to note…. data on graphs like: 851m or 628m actually means 0.851 and 0.628 (just to clear that up).

That’s about it!

So, hopefully this has helped you get cacti working to monitor load, memory, and traffic on a remote or local machine.

If you’re still struggling with this, try googling for other tutorials and use them in combination with mine…

If you’re still struggling, as I said at the beginning, I am a freelancer, and you could always pay me to do it. email: elliot at haughin.com or twitter: elliothaughin

I won’t be able to answer every support comment for such an exhaustive post and process, but I will do my best. For extra help, check the official cacti forums.