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, 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, as well as the ones which are below the threshold, like you can see in the previous example. With the second AREA statement, a green area will be drawn at all data values that 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 this:

    rrdtool graph -w 500 data_image.png --start 1488153600 --end 
1488218400 \
--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:

All of the graphs you have just created can be created in Cacti using the Cacti web interface. This section does provide 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.