Bluebells in Smyrton Wood, reused from https://www.geograph.org.uk/photo/3977770

Baking my Raspberry Pi

Written by

in

,

Raspberry Pis are great. They have actually run a lot of my home network for over a decade. And when I say “they”, I actually mean “it”.

I’ve been running a Raspberry Pi B (2+) as my Pi-Hole (DNS level ad-blocking), my PiVPN (a REALLY straightforward way to tunnel all my mobile traffic back through my home router – with the great side-effect of blocking ads on my phone even when I’m out and about!), and even my network storage for home.

In case you hadn’t guessed it, I’m also running Nagios on it (and ntfy.sh as well). There’s actually a few more docker containers also running on it, which will become the topics of future posts.

While this hardware was actually more than capable of doing all this and ticking along in the corner, the Raspberry Pi 5 got released back in 2023 and there was an influx of remarkably cheap Pi 4s as PB Tech tried to clear their stock and people flogged their spares on TradeMe – so I upgraded – and turned my original Pi 2 into a portable WiFi hotspot that tunnels everything back through my home router.

One thing that Pis do have though is a tendency for over-heating – and so I (surprise surprise) wrote a bash script to monitor that.

#!/usr/bin/env bash

# check_raspberry_pi_CPU_temp.sh
#
# -----------------------------------------------------------------------------
# Description:
#   This script checks and returns the CPU temperature of the Raspberry Pi
# -----------------------------------------------------------------------------
# Author:	Phil Tanner <phil@philtanner.com>
# Created:	2023-04
# Last updated:	2026-08-29
#
# Licence: (MIT licence: https://mit-license.org/)
# Copyright © 2026 Phil Tanner
#
# Permission is hereby granted, free of charge,  to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction,  including without limitation the rights
# to use, copy,  modify, merge,  publish, distribute,  sublicense,  and/or sell 
# copies of  the  Software,  and to  permit  persons to  whom  the  Software is
# furnished to do so, subject to the following conditions:
#
# The above  copyright notice  and this permission notice shall  be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”,  WITHOUT  WARRANTY OF ANY KIND,  EXPRESS OR
# IMPLIED,  INCLUDING  BUT NOT  LIMITED TO THE  WARRANTIES OF  MERCHANTABILITY,
# FITNESS FOR  A PARTICULAR PURPOSE  AND NONINFRINGEMENT.  IN  NO  EVENT  SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE  FOR ANY  CLAIM,  DAMAGES OR OTHER
# LIABILITY,  WHETHER IN  AN ACTION  OF CONTRACT,  TORT  OR OTHERWISE,  ARISING
# FROM,  OUT  OF  OR  IN  CONNECTION  WITH  THE  SOFTWARE OR THE  USE  OR OTHER
# DEALINGS IN THE SOFTWARE.
#
# -----------------------------------------------------------------------------
# Version: 1.0.0
# -----------------------------------------------------------------------------
#
# -----------------------------------------------------------------------------
# TODO:
#   • None \o/
# 
# -----------------------------------------------------------------------------


STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

usage()
{
	echo "Raspberry Pi is a low-end mobile computer. So it doesn't have"
   	echo "fans to cool it down like other desktop/laptop CPUs."
	echo "If the temperature rises above 80°C, you will see a little"
	echo "thermometer on your Raspbian desktop. That indicates that your"
	echo "Pi is getting hot."
	echo "As the core temperature rises, the thermometer fills up."
	echo "Then at 85°C, it changes to a full thermometer. However, that"
   	echo "is the maximum recommended operating temperature. After this"
	echo "temperature, the CPU starts throttling and reduces the clock "
	echo "to cool down the temperature. This will decrease the performance."
	echo ""
	echo "This function returns the current CPU temperature "
	echo "*********** in milli degrees C.*********"
   	echo "Pass a warning and critical value **IN MILLI C**, if current CPU "
	echo "temperatures are ABOVE those threshholds, a warning or critical"
	echo "status will be returned."
	echo ""
	echo "Usage:"
	echo "$0 -w $WARNVAL -c $CRITVAL"
	exit $STATE_UNKNOWN
}

EXITVALUE=$STATE_UNKNOWN
STATUS=UNKNOWN

WARNVAL=60000
CRITVAL=70000
CPU_TMP_FILE="/sys/class/thermal/thermal_zone0/temp"

while getopts c:w: OPTNAME; do
	case "$OPTNAME" in
		w)
			WARNVAL="$OPTARG"
			;;
		c)
			CRITVAL="$OPTARG"
			;;
		*)
			usage
			;;
	esac
done

TEMP=$(sed "s/\(\[0-9\]\)$/\1/" < /sys/class/thermal/thermal_zone0/temp)
# TEMP=(($TEMP+0))

if (( $TEMP > $CRITVAL )); then
	STATUS=CRITICAL
	EXITVALUE=$STATE_CRITICAL
else
	if (( $TEMP > $WARNVAL )); then
		STATUS=WARNING
		EXITVALUE=$STATE_WARNING
	else
		STATUS=OK
		EXITVALUE=$STATE_OK
	fi
fi


echo "${STATUS} - CPU Temp: ${TEMP}"
exit $EXITVALUE

Comments

Leave a Reply

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