Cacti and Custom Data Source Monitoring Part 2 of 2

Cacti and Custom Data Source Monitoring Part 2 of 2

Custom Data Sources

Now that Cacti is up and running, let’s get some data from data sources that typically aren’t covered.  The Raspberry Pi is unique in the sense that CPU temperature can be monitored using a program called vcgencmd.  Great thing about this program is it’s already installed.  Run the following command from the terminal.

[bash]
vcgencmd measure_temp
[/bash]

You should get a reading back along the lines of “temp=42.0’C”.  If you do, great.  We’re going to need to create a script that will run on a schedule that will gather the temperature reading and place it into a text file.  Let’s create our shell script using the following command.

[bash]
sudo nano /home/local/Tasks/RPI_CPUTemp.sh
[/bash]

You can use your own text editor if you like, I prefer nano.  Be sure to run as super user.  We can change permissions and rights later using Webmin.  Here is the code I used for my script.

[bash]
#!/bin/sh

# Set variable for File
RPI_CPUTemp_File="/home/local/Tasks/RPI_CPUTemp.txt"

# Run command and output results to that variable
/opt/vc/bin/vcgencmd measure_temp > "$RPI_CPUTemp_File"

# Get variable with results into new variable
RPI_CPUTemp_Var1=$(cat $RPI_CPUTemp_File)

# Remove extra characters out of variable and create new variable
RPI_CPUTemp_Var2=$(echo "$RPI_CPUTemp_Var1" | awk ‘{print substr($0,length-5,2)}’)

# Output the header and reading back into the File
echo "$RPI_CPUTemp_Var2" > "$RPI_CPUTemp_File"
[/bash]

Once all the text is entered into the file, write it, save it, then close it.  Now lets change the permissions to the file so it is level 0777 and set the owner to your MATE username and group.  You’ll also want to be sure the path ownership is set to you MATE username and group.  You can run the script from the terminal now by entering in this command

[bash]
./RPI_CPUTemp.sh
[/bash]

This will create and write the readings to a file called RPI_CPUTemp.txt.  Here is what should appear in that new file.

42

Really simple right!  Now comes the part were we set a scheduled task to run this script every 5 minutes.  I do this in Webmin because it’s a snap.  You can look into using CRON, but I won’t cover that here.  Webmin is so easy, I won’t even cover it.  Suffice it to say, my task runs every 5 minutes.

Now we’ll need to create a script that Cacti will run to gather that text file reading and do something useful with it.  For that I created a perl script that does the leg work.  Enter in this command at the terminal window.

[bash]
sudo nano /usr/share/cacti/site/scripts/RPI_CPUTemp.pl
[/bash]

Here is the code for that perl script.

[perl]
#!/usr/bin/perl

use strict;
use warnings;

open my $file, ‘<’, "/home/local/Tasks/RPI_CPUTemp.txt";
my $firstLine = <$file>;
print $firstLine;
close $file;
[/perl]

Once all the text is entered into the file, write it, save it, then close it.  Now lets change the permissions to the file so it is level 0755.  That’s it, now we’re ready to get inside Cacti and get this value polled and graphed.

Login to Cacti and go to the Console tab and select Data Input Methods under the Collection Methods section, then select Add.  Next enter in the name of the Data Input Method, “Localhost – CPU Temperature”.  Choose Input Type as “Script/Command”.  Enter in the Input String “perl /usr/share/cacti/site/scripts/RPI_CPUTemp.pl” then click Create.

Now Enter in the Output Fields section by clicking the Add link.  In the Field [Output] entry put “rpitemp”, next enter in the Friendly Name “CPU Celsius” followed by the Create button.

From there we continue on to the Templates section and choose Data Templates followed by clicking the Add link.  Enter in the Data Templates Name “Localhost CPU Temperature” and the Data Source Name “|host_description| – CPU Temperature”.  Next choose from the pulldown menu for the Data Input Method “Localhost – CPU Temperature”.  In the Data Source Item enter in “rpitemp” for the Internal Data Source Name.  I then set my Maximum Value at “50” followed by clicking the Create button.  When I did that the Output Field auto populated with “rpitemp – CPU Celsius”.

Next I went back to the Templates section and choose Graph Templates followed by clicking the Add link.  Here I entered in the Template Name “CPU Temperature” and the Graph Template Name “|host_description| – CPU Temperature”.  Then I set the Vertical Label to “Celsius” and clicked the Create button.

Now I added the Graph Template Item by clicking the Add link.  From the Data Source pulldown I selected “Localhost CPU Temperature – (rpitemp)”.  Then I set the color from the pulldown “FF00FF”.  Then I set the Graph Item Type to “AREA”.  Afterwhich I set the Text Format to “CPU Celsius” and clicked Create.

From there I then went to the Management section and selected Devices.  In the list, I selected “Localhost”.  In the Associated Graph Template portion, I selected “CPU Temperature” from the pulldown and clicked the Add button.  Then up above, I clicked the “Create Graphs for this Host”.  In the next windows, I choose “CPU Temperature” from the pulldown menu in the Create row, followed by the Create button.  This takes you back to the last page, click the “Create Graphs for this Host” link again.  Now check the box to the right of “Create: CPU Temperature”.

Next I went to the Management section and choose Graph Management.  I clicked on the entry “Localhost – CPU Temperature” and verified the entries were correct.  I did the same verification under the Templates section for Graph Templates.

I do realize that this has turned into a fantastic shit show.  Do find comfort that our work is done.  You should see a graph listed when you go to the Graphs tab.  It’ll take time for “stuff” to populate the graph.  To ensure all is working as expected, double check your scripts from the terminal window and echo out the results.

These same steps can be used for other custom data input methods.  Basically, any text file that has a number is a potential source for graph data in Cacti.  The greatest challenge is creating a script that will poll, parse, and create the text file.  If you can get a number, Cacti will graph it.

Now I would like to cover Graph Template Items.  These are useful features that can dress up the graphs with data that draws attention to conditions worth noticing.  For instance, in our CPU temperature graph, it would be helpful to know how far below critical temp we are.  We use Graph Template Items to plot those.  Another example is that our reading is in Celsius.  What if we needed that value to be in Fahrenheit instead.  We used Graph Template Items, but also a sub feature called CDEF Functions to do that conversion.  Yet another example would be to plot something when readings are unknown, to draw attention to a device being offline.  All of these are handled by Graph Template Items.  I’m going to step through these three examples next.

First, lets dive into the CDEF Functions because this will be a fundamental requirement for use later on.  From the Console click on Graph Management under Management.  This will expand a subsection list.  Now we want to click on the link “—CDEFs”.  You’ll see some preset math functions already in there.  We want to add a CDEF for converting temperature from Celsius to Fahrenheit, click the Add link.

Enter in the CDEF name “Celsius to Fahrenheit” and click Create.  Now we will have a CDEF Items list were we can add our functions.  Here is the list of CDEF Item Types and CDEF Item Values to enter in for this function.

Custom String: 1.3
Special Data Source: CURRENT_DATA_SOURCE
Operator: *
Custom String: 32
Operator: +

You will want to be sure that your CDEF list is in the correct order.  Cacti performs these functions in a set way.  If you order is wrong, your results will be wrong.  You should see your items grouped together above, here is what it should look like.

cdef=1.3,CURRENT_DATA_SOURCE,*,32,+

If all looks good, click the Save button.  Now we are done with CDEF functions and ready to put our focus on Graph Template Items.  Lets start by adding a line that will indicate our upper acceptable temperature limit for our CPU.  According to online docs, that value seems to be 80 degrees Celsius.  I’m going to add a upper threshold line at 75 degrees Celsius.

Click Graph Templates from the Console Templates section and choose the CPU Temperature Item.  Next click Add on the Graph Template Items.  Here we want to set our values as such.

Data Source: Localhost CPU Temperature – (rpitemp)
Color: B90094
Opacity/Alpha Channel: 100%
Graph Item Type: HRULE
Consolidated Function: AVERAGE
CDEF Function: None
Value: 75

Click Save and Save again to apply to the Graph Template.  Do note that the auto scaling of the graph may not make the threshold line appear, but be rest assured it is there.  If you want to see it, uncheck the Auto Scale check box in the Graph Template Items window.  You can also set Lower and Upper Limits by checking the boxes for Upper Limit and Lower Limit and entering in the values.  You’ll have a Supplemental Graph Template Data entry appear when you go back into Graph Management and select Localhost CPU Temperature.  I set my upper limit to 85 and my lower to 35.

Now lets add a plot for unknown values in our graph for readings that fail to be made.  This is useful for plotting ping responses from online hosts, not so much for CPU temp.  However, the same steps apply.  First we’ll need to create a CDEF for it.  Enter in these settings for a new CDEF called Unknown.  Here is the list of CDEF Item Types and CDEF Item Values to enter in for this function.

Special Data Source: CURRENT_DATA_SOURCE
Function: UN
Function: INF
Function: UNKN
Function: IF

You should see your items grouped together above, here is what it should look like.

cdef=CURRENT_DATA_SOURCE,UN,INF,UNKN,IF

Now, click Graph Templates from the Console Templates section and choose the CPU Temperature Item.  Next click Add on the Graph Template Items.  Here we want to set our values as such.

Data Source: Localhost CPU Temperature – (rpitemp)
Color: FFC3C0
Opacity/Alpha Channel: 100%
Graph Item Type: AREA
Consolidated Function: AVERAGE
CDEF Function: Unknown

I typically place this Graph Item on the top of my list in the Graph Template Items list.  Viewing the graph will now display a pink area when values are not plotted.  Plotting our temperature in Fahrenheit follows the same basic steps as plotting unknown values.  The only difference would be choosing a different color, graph item type, and CDEF function.  Here is an example of what we would enter in.

Data Source: Localhost CPU Temperature – (rpitemp)
Color: 157419
Opacity/Alpha Channel: 100%
Graph Item Type: LINE2
Consolidated Function: AVERAGE
CDEF Function: Celsius to Fahrenheit

That should cover most of the custom data source and graph functions you could use in Cacti.  I tried to cover as much in a single post, but the steps involved are far too many.  This will be fundamental as I continue on with later posts.  There I will be gathering data from sensors and conditions to be plotted in Cacti.  I’ll also cover some other features in Cacti not covered yet.  Thank you for hanging in there and I hope that you have found this useful information.

Comments are closed.