Cacti 0.8 Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Chapter 2. Using Graphs to Monitor Networks and Devices

After having installed and configured Cacti, you will now be able to add your first devices and graphs to the system. This chapter will show you how to add new devices and how to add some performance measurement graphs to them. You will also learn how to group devices using the Cacti tree.

This chapter is going to cover the following topics:

  • Introduction to graph creation with RRDtool
  • Adding devices to Cacti
  • Adding graphs to a device
  • Assigning host templates to a device
  • Adding a device to the Cacti tree

So let's get started…

An introduction to Cacti graphs and the RRDtool

You can learn more about how RRDtool stores data in Appendix C. Now, 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 which you can find in Appendix C and use that RRD file as the basis for our graphs.

Tip

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.

Execute the following code at the command line interface (CLI):

rrdtool graph data_image.png \
--start 1282413600 \
--end 1282468500 \
DEF:intspeed=test.rrd:data:AVERAGE \
LINE2:intspeed#FF0000

This will create the following graph:

Basic RRDtool graph creation

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:

rrdtool graph data_image.png \
--start 1282413600 \
--end 1282468500 \
--vertical-label bps \
--title "Interface Speed" \
DEF:intspeed=test.rrd:data:AVERAGE \
LINE2:intspeed#FF0000

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

Adding a label and title to the graph

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.

Let's look at the following command:

rrdtool graph data_image.png --start 1282413600 --end 1282468500 \
--vertical-label bps --title "Interface Speed" \
DEF:intspeed=test.rrd:data:AVERAGE \
LINE2:intspeed#FF0000:"Interface eth0" \
GPRINT:intspeed:LAST:"Current\:%8.0lf" \
GPRINT:intspeed:AVERAGE:"Average\:%8.0lf" \
GPRINT:intspeed:MAX:"Maximum\:%8.0lf\n"

The resulting image now also contains a small legend at the bottom:

Adding a legend to the graph

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:

rrdtool graph data_image.png --start 1282413600 --end 1282468500 \
--vertical-label bps --title "Interface Speed" \
DEF:intspeed=test.rrd:data:AVERAGE \
HRULE:50#C0C0C0FF:"Threshold ( 50 )\n" \
AREA:intspeed#FF0000:"Interface eth0" \
GPRINT:intspeed:LAST:"Current\:%8.0lf" \
GPRINT:intspeed:AVERAGE:"A
verage\:%8.0lf" \
GPRINT:intspeed:MAX:"Maximum\:%8.0lf\n"

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

Adding a threshold line to the graph

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.

The complete code now looks like the following:

rrdtool graph data_image.png --start 1282413600 --end 1282468500 \
--vertical-label bps --title "Interface Speed" \
DEF:intspeed=test.rrd:data:AVERAGE \
CDEF:isGreen=intspeed,0,50,LIMIT \
HRULE:50#C0C0C0FF:"Threshold ( 50 )\n" \
AREA:intspeed#FF0000:"Over Threshold\n" \
AREA:isGreen#00FF00:"Interface eth0" \
GPRINT:intspeed:LAST:"Current\:%8.0lf" \
GPRINT:intspeed:AVERAGE:"Average\:%8.0lf" \
GPRINT:intspeed:MAX:"Maximum\:%8.0lf\n"

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

Adding threshold breaches to the 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.

Further reading

The RRDtool webpage provides some very good documentation on the RRDtool and the graphing functions. The features you have seen here are only a small set of what is possible with the RRDtool. Unfortunately, providing information on all of the features is beyond the scope of this book, but it is recommended that you especially look at the gallery at http://oss.oetiker.ch/rrdtool/ for some further ideas on the graphs.

Please remember that, although Cacti does provide many of the functions of the RRDtool, there are some which may not yet be available.

Have a go hero – creating a yellow warning area

Let's assume green and red areas are not granular enough, and you also want to have a yellow area where you can immediately see that the threshold is about to be breached. This yellow warning area should be displayed between the values of 45 and 50.

Have a look at the following image:

Have a go hero – creating a yellow warning area

What would you need to change in the above RRDtool command line to get this image?

Solution: You need to add one additional CDEF and another AREA for this to work. You also need to change the isGreen CDEF. The following command line will create and display the yellow warning area and the appropriate legend:

rrdtool graph data_image.png --start 1282413600 --end 1282468500 \
--vertical-label bps --title "Interface Speed" \
DEF:intspeed=test.rrd:data:AVERAGE \
CDEF:isGreen=intspeed,0,44,LIMIT \
CDEF:isYellow=intspeed,45,50,LIMIT \
HRULE:50#C0C0C0FF:"Threshold ( 50 )\n" \
AREA:intspeed#FF0000:"Over Threshold" \
AREA:isYellow#FFFF00:"Warning" \
AREA:isGreen#00FF00:"Good\n" \
COMMENT:"Interface eth0" \
GPRINT:intspeed:LAST:"Current\:%8.0lf" \
GPRINT:intspeed:AVERAGE:"Average\:%8.0lf" \
GPRINT:intspeed:MAX:"Maximum\:%8.0lf\n"

Note that we use a COMMENT item to add the Interface eth0 text at the beginning of the graph legend.