Monitor CPU and GPU Temperature and undervoltage of a Raspberry PI via CLI, SNMP and Cacti
The following is a primer for setting up your Raspberry PI to use SNMP to monitor the CPU and the GPU temperature in Cacti and to monitor the undervoltage condition. The instructions below cover Raspbian 11. I am assuming you are familiar with SNMP.
Raspian
Create a file named /usr/local/sbin/cputemp
#!/bin/bash cpu=$(</sys/class/thermal/thermal_zone0/temp) echo print $cpu/1000 | /usr/bin/perl echo
Create a file named /usr/local/sbin/gputemp
#!/bin/bash gpu=$(/usr/bin/vcgencmd measure_temp) echo $gpu | /usr/bin/awk -F '=' {'print $2'} | /usr/bin/awk -F "'" {'print $1'}
Create a file named /usr/local/sbin/undervoltage
#!/bin/bash # check and see if currently the power supply is undervoltage uv=$(/usr/bin/vcgencmd get_throttled) uvn=$(echo $uv | /usr/bin/awk -F '=' {'print $2'} ) if [ $((uvn & 0x1)) == 0 ]; then echo "Voltage good" echo "0" else echo "Undervoltage now" echo 1 fi
Create a file named /usr/local/sbin/undervoltage-flag
#!/bin/bash # check and see if the power supply was under voltage since last reboot uv=$(/usr/bin/vcgencmd get_throttled) uvn=$(echo $uv | /usr/bin/awk -F '=' {'print $2'} ) if [ $((uvn & 0x10000)) == 0 ]; then echo "Voltage good" echo "0" else echo "Undervoltage flagged" echo 1 fi
Set the permissions on the above files
sudo chmod 775 /usr/local/sbin/cputemp sudo chmod 775 /usr/local/sbin/gputemp sudo chmod 775 /usr/local/sbin/undervoltage-flag sudo chmod 775 /usr/local/sbin/undervoltage
Install snmp by:
sudo apt-get update sudo apt-get install snmpd
edit the file /etc/snmp/snmpd.conf file and add the following lines to the end
extend cputemp /usr/local/sbin/cputemp extend gputemp /usr/local/sbin/gputemp extend gputemp /usr/local/sbin/undervoltage extend gputemp /usr/local/sbin/undervoltage-flag view systemonly included .1.3.6.1.4.1.8072.1.3.2
If you want to be able to monitor the network interfaces via snmp/cacti, add the following line
view systemonly included .1.3.6.1.2.1.2
If you want to be able to monitor the storage usage and memory via snmp/cacti add the following line
view systemonly included .1.3.6.1.2.1.25.2.3.1
find the line near the top of the file that reads
agentaddress 127.0.0.1,[::1]
change it to read
gentaddress udp:161,udp6:[]:161
and remove the “#” in front of it
find the lines:
rocommunity public default -V systemonly rocommunity6 public default -V systemonly
replace the word “public” with different community string. Note the community string is case sensitive.
save your changes and execute
usermod -a -G video Debian-snmp systemctl enable --now snmpd.service
Testing the snmp using net-snmp-utils
replace “public” with your community string
[user@mymachine ~]$ snmpwalk -v 1 -c public my.pi.ip.address nsExtendOutLine NET-SNMP-EXTEND-MIB::nsExtendOutLine."cputemp".1 = STRING: 49.388 NET-SNMP-EXTEND-MIB::nsExtendOutLine."gputemp".1 = STRING: 49.4 NET-SNMP-EXTEND-MIB::nsExtendOutLine."undervoltage".1 = STRING: Voltage good NET-SNMP-EXTEND-MIB::nsExtendOutLine."undervoltage".2 = STRING: 0 NET-SNMP-EXTEND-MIB::nsExtendOutLine."undervoltage-flag".1 = STRING: Voltage good NET-SNMP-EXTEND-MIB::nsExtendOutLine."undervoltage-flag".2 = STRING: 0 [user@mymachine ~]$
Depending on your snmp configuration you may get data from other points in the snmp besides the above two
Cacti Graph Template
Download this zip file, unzip it and import it into cacti, then you can quickly graph the CPU and GPU temperatures