# How To How to Graph a Custom Collection Script

This HOWTO walks you through the simplest steps of graphing the output of a single value from a script. As a new user of cacti, I had a difficult time understanding how to graph anything that wasn't canned with the original load. After a lot of playing around, I came up with these procedures which can be built upon for more sophisticated collections and graphs.

I do not use templates in this HOWTO. Templates provide a significant advantage if you are graphing the output for multiple instances or creating graphs for multiple hosts. They help to maintain consistency and simplify setup.

The example here is not realistic, but can be used and expanded upon as a model for creating your own graphs.

I've not put a lot of explanations in the procedures. Refer to the Cacti manual for more details.

Have a tested script ready to go. I used the following script located in `/home/cactiuser/bin` called `random2.pl`:

    #!/usr/bin/perl -w

    print int(rand(10));

This script simply prints out a random integer between 0 and 9 every time it's called.

### Minimal steps to create a graph from a script

1. Create a Data Input Method to tell Cacti how to call the script and what to expect from it.

2. Create a Data Source to tell Cacti how and where the data will be stored.

3. Create a Graph to tell Cacti how the data will be presented in graph form.

4. Add Graph to Graph Tree so you can view the graph.

5. View the Graph.

### Detailed Instructions

1. Create Data Input Method

   - Click the Console tab at the top

   - Click Data Input Methods under Management

   - Click Add

   - Name: `Random 2 Input`

   - Input Type: Script/Command

   - Input String: `/home/cactiuser/bin/random2.pl`

   - Click Create

   - Should see Save Successful at the top of the page.

   - Click Add by Output Fields (there are no input fields for this example)

   - Field [Output]: `random_number`

   - Friendly Name: `Random Number`

   - Update RRD file: Checked

   - Click Create

   - Should see Save Successful and `random_number` listed under Output Fields.

   - Click Save

   - Should see Save Successful and `Random Input` listed in Data Input Methods.

2. Create a Data Source

   - Click Data Sources under Management

   - Click Add in the top right

   - The Data Template Section should be None and None, we're not using templates for this example.

   - Click Create

   - Name: `random2ds`

   - Data Source Path: empty (Cacti will fill this in)

   - Data Input Source: Random 2 Input (this is the data input method you created in step 1)

   - Highlight each of the Associated RRA's using control-click

   - Step: `300` (300 seconds = 5 minutes)

   - Data Source Active: Checked

   - Describe the Data Source Item inside the RRA by...

   - Internal Data Source Name: `random_number`

   - Minimum Value: `0`

   - Maximum Value: `0`

   - Data Source Type: GAUGE

   - Heartbeat: `600`

   - Click Create

   - Should see Save Successful at the top and the Data Source Path should now have a value (my example `<path_rra>/random_number_286.rrd`)

   - Click Turn On Data Source Debugging Mode to see the results of this step.

   - Click Save

   - Should see Save Successful and your new Data Source listed.

3. Create Graph

   - Select Graph Management under Management

   - Click Add

   - Selected Graph Template: None

   - Host: None

   - Click Create

   - Title: `RANDOM NUMBERS`

   - Image Format: PNG

   - Height: `120`

   - Width: `500`

   - Auto Scale: Checked

   - Auto Scale Options: Use --alt-autoscale-max

   - Logarithmic Scaling: Unchecked

   - Rigid Boundaries: Unchecked

   - Auto Padding: Checked

   - Allow Graph Export: Checked

   - Upper Limit: `100`

   - Lower Limit: `0`

   - Base Value: `1000`

   - Unit Value: Empty

   - Unit Exponent Value: `0`

   - Vertical Label: `Random Number`

   - Click Create

   - Should see Save Successful

   - If you click Turn On Graph Debug Mode* now you will see: `Error: can't make a graph without contents`. We need to add Graph Items:

   - Click Add by Graph Items

   - Data Source: No Host - random2ds (random_number)

   - Color: 0000FF (Blue)

   - Graph Item Type: LINE2

   - Consolidation Function: AVERAGE

   - CDEF Function: None

   - Value: Empty

   - GPRINT Type: Normal

   - Text Format: Empty

   - Insert Hard Return: Unchecked

   - Click Create

   - Should see Save Successful, Item #1 listed and a graph under debug (probably with nothing in it yet)

   - To add a legend, click Add by Graph Items again

   - Data Source: No Host - random2ds (random_number)

   - Color: None

   - Graph Item Type: GPRINT

   - Consolidation Function: LAST

   - CDEF Function: None

   - Value: Empty

   - GPRINT Type: Normal

   - Text Format: `Cur:`

   - Insert Hard Return: Unchecked

   - Click Create

   - Should see Save Successful and the graph will have a legend showing the current value. Note: your integer random number has been averaged over the past 5 minutes. At the bottom of the page, click Save.

   - At the bottom of the page, click Save.

   - Should see Save Successful and your graph listed.

4. Add Graph to the Graph Tree

   - Click Graph Trees under Management

   - We'll create a tree called `Scripts` to place our graph

   - Click Add

   - Name: `Scripts`

   - Should see Save Successful

   - Click Add beside Tree Items (to add our graph to this tree)

   - Under Tree Items [graph]...

   - Graph: RANDOM NUMBERS (we named this in step 3)

   - Round Robin Archive: Daily (5 Minute Average)

   - Click Create

   - Should see Save Successful and RANDOM NUMBERS listed under Tree Items

   - Click Save

   - Should see Save Successful and Scripts listed under Graph Trees

5. View Graph

   - Select Graphs tab

   - Select Tree View (Tree beside settings tab)

   - Select Scripts tree on left

   - You may see Random Graph but no graph. It takes two or three polls (10 - 15 minutes) to see a graph. I believe poll 1 to create the rrd, poll 2 to get the first data point and poll 3 to have graph-able data.

   - Now be patient. Hit your refresh button every few minutes. After the first poll I got a few empty graphs. A few minutes later data started showing up.

## Originally written by Kevin der Kinderen, http://kdeuja.com/~kevin/

Copyright (c) 2004-2020 The Cacti Group