An introduction to Cacti graphs and the RRDtool

You'll be looking into the actual graph creation process and what features Cacti supports.

Creating graphs with the RRDtool

Cacti uses the RRDtool to store the polled data. In addition to just storing the data, the RRDtool is also used to create the actual performance graphs.

If you now expect to see a fully-featured charting application, you will be disappointed. The RRDtool graph functionality offers only a very limited range of chart types. They can either be line charts, area charts, or a combination of both. There is no 3D option available, nor are there any other types of charts such as Pie or Scatter charts. This may be a disadvantage for some at first, but concentrating on only a few basic chart types makes it a fast specialized rendering engine for these. Being fast in displaying the raw RRD data is the main focus of the RRDtool graphing engine.

There are several graphing features available for plotting the data. The most commonly used types are:

LINE: The data is drawn as a line which can be formatted by width and type (for example, dashed line)

VRULE: A fixed vertical line is drawn at a defined value

HRULE: A fixed horizontal line is drawn at a predefined value (for example, threshold limits)

AREA: A solid filled area chart is drawn. Several area charts can be stacked together

Each of these graph types can be combined together to build the final chart image.

Let us dive into the graph creation process here to get a better understanding of the RRDtool graphing capabilities.

You need to have the RRDtool in your path for the following commands to work.

Basic RRDtool graph creation

Let's begin with the RRD example and use that RRD file as the basis for our graphs.

A note for Windows users The following examples also work for Windows. Simply replace the RRDtool command with the full path to the RRDtool binary, for example, use C:\rrdtool\rrdtool.exe instead of rrdtool. You will also have to copy the DejaVu font from the RRDtool directory to your Windows fonts directory.

I have created a Perl script which will help in the creation of the RRD file and its automatic update with random data. In order to create the test RRD file, use the following command:

perl create_rrdfile_linux.pl test.rrd

If you have installed the RRDtool to C:\rrdtool you can use the following command for Windows:

perl create_rrdfile_windows.pl test.rrd

Having created the test data, you can now start to generate your first RRDtool-based graph. It is going to be a very simple graph displaying only the pure data.

So what does this command actually do? Using the command, you defined a start and end time in the Unix time format and defined the RRD file and data set you wanted to plot. You also told RRDtool to draw a two-pixel line (LINE2) using this data set and stored the resulting graph as data_image.png. The RRDtool automatically creates the X- and Y-axis for you and also inserts the time and value description. This is the most basic way of creating an RRDtool-based graph.

Advanced RRDtool graph creation

Although this basic graph image already has a lot of information in it, it is still missing some important features. It neither describes what is being graphed, nor does it provide additional information such as threshold breaches or MAX/MIN values. So, let's go back to this basic graph and look at how you can enhance it step-by-step using some of the advanced RRDtool features.

Adding a label and title to the graph

The first enhancement to our graph will be the addition of a label and a graph title. For this you can use the --vertical-label and --title parameters:

The resulting graph now has a title at the top and a description to the left as can be seen in the following image:

As you can see, the RRDtool command added a rotated description to the Y-axis and also added the title at the top of the graph. The graph is now bigger in dimensions than the first one. The RRDtool uses only the width and height information to set the actual chart size. Everything else must be added to the graph separately. You can see more about how this works in the following examples.

Adding a legend to the graph

Now that you have added some description to the graph, you can also add a legend to it. For this, you are going to use the LAST, AVERAGE, and MAX poller values. The function of the GPRINT item is to add additional graph information to the legend. You are also going to add a description field to the LINE2 item. Adding a description to the LINE or AREA items will automatically create a legend entry for you.

The LAST, AVERAGE, and MAX values are always calculated using the data limited by the start and end time. Therefore they directly relate to the chart being displayed.

As you can see, the legend was added to the bottom of the graph, expanding its height. By adding a description to the LINE2 line (Interface eth0) the description was automatically placed at the bottom along with the color being used to draw that line. The GPRINT text and values have then been added right after the description. If you want to add some more text to the next line, you need to make sure that the last GPRINT value contains a \n (newline) string at the end.

In this example, you can also see that the RRDtool did not increase the width of the graph to fit the legend in it. The Maximum value has been silently dropped. GPRINT statements do not automatically increase the graph width, so you will need to increase the width yourself. This can be done by using the –width parameter.

Adding a threshold line to the graph

Now let's also set a threshold and display a line on the graph marking the threshold. This can be achieved by using the HRULE item. You are going to set a threshold at 50 and use a light grey color to display it on the graph. The following command creates this line and also adds an additional entry to the legend. In addition, you are also going to change the LINE2 item to an AREA item, so the data being displayed is shown as a filled area:

You can see the light gray line being printed horizontally in the image, providing a good overview of when the data exceeds the threshold:

Note the usage of the newline string \n in the description string for the HRULE item. As you can see in the graph, the following text items are added to the next line.

Adding threshold breaches to the graph

You have now seen how you can add a threshold line to the graph, but you probably also want to change the color of the data every time the threshold is breached. Let us assume that you want to have the color go red at or above the threshold and go green once it is below. This can be achieved by using a Computed DEFinition (CDEF) and the LIMIT statement.

You define a CDEF named isGreen which returns a number as long as the value of intspeed is between 0 and 50, otherwise no value is returned. You are going to use this CDEF to change the color of the displayed area.

Instead of using the intspeed value you assign this new CDEF isGreen to the AREA item and change the color of the AREA to green (RGB: 00FF00). You also create a new AREA entry, to which you now assign the intspeed value, set the color to red, and give it a description Over Threshold\n. For this to work correctly, you need to place this new AREA above the old AREA statement.

Why are there two AREA statements? In fact, changing the color of one AREA as it is displayed is not possible, so you need to do a little trick here. The first AREA statement will graph all values in red, also the ones which are below the threshold, as you have seen in the preceding example. With the second AREA statement a green area will be drawn at all data values which are below the threshold. As the color is not transparent, the red area will disappear. You can see the total red area when you remove the second AREA statement.

Run this code from the command line and you will see the resulting graph:

All of the graphs you have just created can be created in Cacti using the Cacti web interface. This section provides a small and very limited overview of the capabilities of the RRDtool graphing functions, but should give you enough ideas to start playing around with it to create your own graphs.

Adding devices to Cacti

A device in Cacti can be anything which can be monitored remotely or locally. This can include storage devices, Windows or UNIX servers, and of course network devices. For Cacti to be able to monitor a device, it needs to be reachable by ping or SNMP, but the actual data retrieval can also be done using scripts and commands, or a set of SNMP queries.

Creating a device

Creating a device in Cacti can be achieved by using the Cacti web interface. You are going to add your first device here. While looking at the different steps it takes to add a device, you are not going too much into the details of every field, as most of the user interface is self-explanatory and provides a detailed description of each field.

Before you start: Create a naming standard If you have not already done so, you should now think about a naming standard for your devices. Creating and keeping to a naming standard is the first step to automation.

Time for action – creating a new device in Cacti

Click on the Devices link under the Management menu. This will open a table with all devices added so far. For a new installation there should only be the localhost device showing its status as Up.

On the top right of the new page click on Add. This is the default position for this Add link.

(Move the mouse over the image to enlarge.)

You will now be presented with the Devices [new] screen. Have a look at this screen and make yourself comfortable with the different fields.

Enter a Description and Hostname (or IP address).

If you add an SNMP-enabled device, select SNMP as the Downed Device Detection method. Otherwise select Ping. When selecting Ping you can choose the protocol type and port to use.

Enter the SNMP community and select the correct version (some additional fields will show up when you choose SNMP Version 3). If the device is not SNMP compatible, you can select Not used.

You can also add some notes. Click on the Create button once you are finished.

What just happened?

You just created your first device within Cacti by providing some basic information such as an IP address and SNMP management options. With this information Cacti is now able to poll the device. However, it still does not have any graphs associated with it.

Selecting host templates for the device

You may have noticed the Host Templates field, but what is a host template? A host template is a predefined package of graphs or data queries which can be assigned to a device. Using a template for complex devices reduces the administrative task for adding devices. Here you are going to assign a template to the device. Host templates can also be selected once the device has been created. Cacti comes with some very basic host templates such as Cisco Router Windows 2000/XP Host or Generic SNMP-enabled Host. All of these contain predefined graphs or data queries for these hosts.

Time for action – adding a host template to the device

Go back to the device overview page by clicking on the Devices link under the Management menu.

What just happened?

By selecting a host template for the device, you have added a predefined package of graph templates and data queries to the host. This is a convenient way of reducing the administrative tasks of adding these manually through the provided drop-down lists.

Adding graphs to the device

Cacti displays performance data as graphs, therefore we are now going to add some basic graphs to the device which we have just added. The first graph which you are going to add is a simple ping graph. Let's go ahead and add the ping template to the host and later add the associated graph to the device.

Time for action – adding graphs to the device

Go back to the device overview page by clicking on the Devices link under the Management menu.

Click on the device you have just created.

In the Associated Graph Templates section select the Unix - Ping Latency from the drop-down list and click on the Add button.

Click on the Save button at the bottom of the page.

Go to the top of the page and click on, Create Graphs for this Host.

Select Create: Unix - Ping Latency.

Click on the Create button.

A new screen will appear, where you can choose a legend color and text, but for now, just click on Create.

You will be redirected back to the graphs selection screen with the entry we selected being greyed out.

What just happened?

You just added your first graph to a Cacti device by adding a graph template to the device and selecting it during the graph creation screen. Cacti will now start to poll the data for this graph and generate the associated RRD file for it.

The Unix templates Except for the Ping Latency template, all other Unix templates are for the localhost only and will not provide any information for remote systems.

Adding interface graphs to a device

Adding interface graphs is a little different from adding a generic one such as the ping graph. Normal network devices have several network interfaces, all of which can be polled for performance data. Cacti provides a nice interface for selecting the different network interfaces using the web interface. In the following section you are going to look into this kind of graph selection.

Time for action – adding interface graphs to a device

Go back to the device overview page by clicking on the Devices link under the Management menu.

Click on the device you have just created or create any other SNMP capable device having network interfaces.

Configure the device to use SNMP and click on the Save or Create button.

Make sure that the following information appears at the top of the page. This will show that the device is SNMP capable.

In the Associated Data Queries section select the SNMP – Interface Statistics from the drop-down list and click on the Add button. If it is already there, then skip this step.

Click on the Save button at the bottom of the page.

Go to the top of the page and click on Create Graphs for this Host.

On the new page which appears, select the interfaces you want to monitor and select a Graph type from the drop-down list.

Click on the Create button.

What just happened?

You added the SNMP – Interface data query to the device and selected some interfaces from the interfaces list.

The SNMP – Interface data query is a special package, containing the graph definitions and a kind of blue-print for Cacti to poll information for the interfaces of a device.

Adding devices to the Cacti tree

The Cacti tree lists sub-trees, hosts, and graphs in a tree-like interface. It is the main user interface for the Graphs tab. There can be more than one tree which allows for a granular definition of the tree structure.

Before creating the Cacti tree, think about a good structure for it. Changing the tree later is going to involve quite some manual work, so it is better to have this set up correctly beforehand, so it is better to have this set up correctly beforehand.

Creating a tree

Cacti already has a default tree defined which holds the localhost. You are going to leave this default tree empty and create your very own tree.

Time for action – creating a Cacti tree

Click on Graph Trees under the Management menu.

You will see the Default Tree. Click on the Add link to the top right of that table.

Enter a name, for example, Customer A.

Click on the Create button.

What just happened?

You created a new Cacti Tree called Customer A which you can now use to add all Customer A-specific entries. Using separate trees for customers or business units will enable you to better allow or deny access to these for specific users.

Sub-tree items

A sub-tree item enables the creation of sub-entries to the Cacti tree. These can be entries such as "Country", "Site", or a "Business Unit". Creating sub-tree items allows end users to easily find their devices on the Cacti Tree.

Time for action – adding a sub-tree

Click on Add at the top right of the Tree Items table.

Select Header as the Tree Item Type.

Enter Country A as the title.

Click on the Create button.

Click on the (Add) link next to the new Country A entry.

Keep the Parent Item to Country A and the Tree Item Type to Header.

Enter Site A as the title.

Click on the Create button.

What just happened?

You created your first site for Customer A. You can now use this tree to fit all countries, sites, and buildings into a nice manageable tree structure. Your end users will immediately recognize the structure and will be able to quickly find the necessary information.

Adding a device to the tree

Now that you have created a tree and its sub-tree items, you can move on and add a device to the tree.

Time for action – adding a device to the Cacti tree

Click on the (Add) link next to the Site A entry.

Select Host as Tree Item Type.

From the Host drop-down list, select the host which you created earlier. There should also be a Localhost listed. This is the Cacti server.

Leave all the others to their defaults.

Click on the Create button.

What just happened?

You just added your first device to your newly created Cacti tree. You can also add single graphs to the tree by changing the Tree Item type.

Summary

In this article you have learned quite a bit about the RRDtool graph generation features. Specifically you have covered the following:

Creating some basic graphs using the RRDtool

Adding advanced features such as threshold line and color changes based on the threshold

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.