cacti template to monitor raspberry pi temperature

After very long time I had some time to do smth with my raspberry pi.

I always wanted to have small server in home to monitor all of my network devices in home. Raspberry pi is almost ideal for it. However I cannot run Zabbix on it, because Zabbix is not adopted to such small devices. So I decided to use cacti. After digging google I spotted this link:
Cacti Pi

There is nice tutorial how to intall and optimize cacti to run on raspberry pi. I followed it and after about 15 minutes I had full setup of cacti.
However I wanted to monitor my raspberry pi’s temperature. It can be done from CLI:

/opt/vc/bin/vcgencmd measure_temp

So I’ve writed small script:

#!/usr/bin/perl
 
open(PROCESS, "/opt/vc/bin/vcgencmd measure_temp |");
$avg = <PROCESS>;
close(PROCESS);
 
$avg =~ s/^.*=(\d{2}\.\d)'?C?$//;
 
print "$1";

Here is some edit:

#!/usr/bin/perl
delete @ENV{qw(PATH)};
$ENV{PATH} = "/usr/bin:/bin";
$path = $ENV{'PATH'};
 
open(PROCESS, "/opt/vc/bin/vcgencmd measure_temp |");
$avg = <PROCESS>;
close(PROCESS);
 
$avg =~ s/^.*=(\d{2}\.\d)'?C?$//;
 
print "$1";

And put it to:

/var/www/cacti/scripts/temp_rpi.pl

Of course You need to:

chown pi:users /var/www/cacti/scripts/temp_rpi.pl

After that create new data input method in cacti:

Data input methods

Then added cacti templates and data sources (files to import to cacti 0.8.8a are attached to this post), but graphs didn’t show. After digging google, I’ve found the problem: www-data user must be added to video and plugdev group.

After that You will have nice graphs of temperature on your raspberry pi:
Raspberry pi temperature graph

And that’s all.

Files to import in cacti:
Cacti data template
Cacti graph template

Posted in Uncategorized Tagged with: , ,
16 comments on “cacti template to monitor raspberry pi temperature
  1. n00badmin says:

    Amazing job sq4ind!

    Thanks for the link!

    I will be throwing this into my Cacti Pi install ASAP!

  2. tonu says:

    Hi, I’m a little bit in trouble…

    Using Your script gives me “WARNING: Result from CMD not valid. Partial Result: U” in cacti log. I guess, the problem is in the Perl script… I was trying a few different options and found that: when I edit the last line of the script and write it like ” print “49.5”; ” then cacti receives data OK but when using ” print $1 ” or the orig. scripts ” print “$1″ ” I’ll get the above error message.

    Can You point me where the problem is? Thanks in advance!

  3. tonu says:

    Solved… the answer is in Your text. Thanks.

  4. Gianl says:

    I have the same problem
    “WARNING: Result from CMD not valid. Partial Result: U” in cacti log.

    Can You point me where the problem is? Thanks in advance!

  5. David says:

    Thanks for the integration! This extends the usefulness of the RPi as a metrics box.

  6. Steve says:

    Thanks for that.

    Very useful.

    Just tried out Cacti, and adding the TEMP to the Graphs was useful.

  7. 1s2 says:

    Hi,

    if you have ever thought about accessing the temperature or other locale environment data of a Raspi remotely via snmp you may want to have a look to this article:
    http://www.onestep2.at/en/blog/remote-sensor-raspberry-pi-locale-temperature-monitoring-snmp-and-cacti

    Bye

  8. r.hundstorf says:

    Great step by step instructions!!
    till
    “Then added cacti templates and data sources (files to import to cacti 0.8.8a are attached to this post), but graphs didn’t show. After digging google, I’ve found the problem: www-data user must be added to video and plugdev group.”

    wtf? can you please specify what to do?!

    how to import, how to www-data add to video and plugdev?

    • sq4ind says:

      You should use this one:
      usermod -g video www-data
      usermod -g plugdev www-data

      After that everything should work.

      • buri says:

        You should use this one:
        usermod -a -G video www-data
        usermod -a -G plugdev www-data

        because “-g” meaing change the primary group of a user and that means, that you rewrite video group with plugdev and script without that doesn’t work.

  9. Monno says:

    Great work
    Just added to my PI, and works great
    Thanks

  10. Dejan says:

    Really nice tutorial but i’am a little bit in trouble. Ich hope you can help me.

    this is my script:

    #!/usr/bin/perl

    use warnings;
    use strict;

    delete @ENV{qw(PATH)};
    $ENV{PATH} = “/usr/bin:/bin”;
    my $path = $ENV{‘PATH’};

    open(PROCESS, “/opt/vc/bin/vcgencmd measure_temp |”);
    my $avg = ;
    close(PROCESS);

    $avg =~ s/^.*=(\d{2}\.\d)’?C?$//;

    print “$1”;

    when i execute it in terminal:

    perl -w temp_rpi.pl
    37.9

    but graphs didn’t show and i try it with

    usermod -g video www-data
    usermod -g plugdev www-data

    sudo tail -f poller-error.log

    Use of uninitialized value $1 in string at /usr/share/cacti/site/scripts/temp_rpi.pl line 16.
    Use of uninitialized value $1 in string at /usr/share/cacti/site/scripts/temp_rpi.pl line 16.
    Use of uninitialized value $1 in string at /usr/share/cacti/site/scripts/temp_rpi.pl line 16.
    Use of uninitialized value $1 in string at /usr/share/cacti/site/scripts/temp_rpi.pl line 16.
    Use of uninitialized value $1 in string at /usr/share/cacti/site/scripts/temp_rpi.pl line 16.

    I hope you can help me.

    thx

  11. Lucien says:

    Hello,
    I’ve installed Cacti Version 0.8.8b.
    I can’t get the temperature displayed, the temperature graph is there though.
    I have this error in my cacti log:
    CMDPHP: Poller[0] Host[1] DS[8] WARNING: Result from CMD not valid. Partial Result: U
    I copy/pasted your script:

    #!/usr/bin/perl
    delete @ENV{qw(PATH)};
    $ENV{PATH} = “/usr/bin:/bin”;
    $path = $ENV{‘PATH’};

    open(PROCESS, “/opt/vc/bin/vcgencmd measure_temp |”);
    $avg = ;
    close(PROCESS);

    $avg =~ s/^.*=(\d{2}\.\d)’?C?$//;

    print “$1”;

    When I execute it, it returns the temperature:

    sudo perl temp_rpi.pl
    42.2

    If, like tonu said earlier, I change
    print “$1”;
    to
    print “40”;

    The temperature graph is working.

    What’s wrong???

  12. Alex Az says:

    Great work
    Thank you so much
    I test it on Raspberry Pi 3. it’s work.

Leave a Reply

Your email address will not be published. Required fields are marked *

*