Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash shell script to print stats about a Raspberry Pi running pihole
#!/usr/bin/env bash
# Define colors
RED='\033[91m'
RED_solid='\033[101m'
GREEN='\033[92m'
GREEN_solid='\033[42m'
CYAN='\033[96m'
NC='\033[0m'
BLUE_solid='\e[44m'
# Gather system details.
dt=$(date -R)
up=$(uptime)
gov=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
freq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq | awk '{printf("%.0f\n"), $1/1000}')
freq=$((freq))
cpuname=$(lscpu | awk '/Model name:/{print $3}')
usedRAM=$(free | awk '/Mem/{printf("%.2f\n"), $3/1024}')
freeRAM=$(free | awk '/Mem/{printf("%.2f\n"), $4/1024}')
cacheRAM=$(free | awk '/Mem/{printf("%.2f\n"), $6/1024}')
# temp=$(cat /sys/class/thermal/thermal_zone0/temp) ; temp=$((temp/1000))
# measurements via vcgencmd, might require sudo access (see the last segment of this script)
volts=$(/opt/vc/bin/vcgencmd measure_volts core | cut -d'=' -f 2)
temp=$(/opt/vc/bin/vcgencmd measure_temp | cut -d'=' -f 2 | cut -d"'" -f 1)
temp_check=$(printf %.0f $temp); temp_check=$((temp_check)); declare -i temp_check
# Print System details
echo -e "\n${GREEN_solid}$dt${NC}"
echo -e "\n${BLUE_solid}CPU - $cpuname${NC}"
echo -e "System voltage = $volts"
if ((temp_check >= 10 && temp_check <= 37)); then
echo -e "CPU temp=${GREEN}$temp°C${NC}"
fi
if ((temp_check > 37)); then
echo -e "CPU temp=${RED}$temp°C${NC}"
fi
if ((temp_check < 10)); then
echo -e "CPU temp=${CYAN}$temp°C${NC}"
fi
if [ "$gov" == "powersave" ]; then
echo -e "CPU Governor=${GREEN}$gov${NC}"
fi
if [ "$gov" == "conservative" ]; then
echo -e "CPU Governor=${CYAN}$gov${NC}"
fi
if [ "$gov" == "performance" ] | [ "$gov" == "ondemand" ]; then
echo -e "CPU Governor=${RED}$gov${NC}"
fi
if [ "$freq" == "700" ]; then
echo -e "Current speed=${GREEN}$freq MHz${NC}"
fi
if [ "$freq" == "1000" ]; then
echo -e "Current speed=${RED}$freq MHz${NC}"
fi
# Print free & used RAM
echo -e "\n${BLUE_solid}RAM${NC}"
echo -e "Free=${GREEN}$freeRAM MB${NC} ; Used=${RED}$usedRAM MB${NC}; Cache=${CYAN}$cacheRAM MB${NC}"
# print pihole status directly from pihole CLI
echo -e "\n${BLUE_solid}Pihole${NC}"
pihole status
# Check system status.
# Requires package dnsutils on linux.
echo -e "\n\n${BLUE_solid}Checking DNS resolution${NC}"
dig google.com +noall +answer +stats | awk '$3 == "IN" && $4 == "A"{ip=$5}/Query time:/{t=$4 " " $5}/SERVER:/{serv=$3} END{print "IP (Twitter):"ip, "\nTime: "t, "\nDNS server: "serv}'
# print system uptime
echo -e "${RED_solid} $up ${NC}"
# Potential alternatives
# temp=$(/opt/vc/bin/vcgencmd measure_temp | egrep -o '[0-9]*\.[0-9]*')
# freq=$(/opt/vc/bin/vcgencmd measure_clock arm | cut -d'=' -f 2) # in MHz
# freq=$((freq/1000000))
# Troubleshooting:
# if you get the error "VCHI initialization failed"
# on running the above commands via vcgencmd, then you need
# to add your default "unprivilaged" user to the group 'video'
# Run:
# sudo usermod -G video piuser
@dchakro

This comment has been minimized.

Copy link
Owner Author

@dchakro dchakro commented May 16, 2020

The current updated version should look like the screenshot above on a Raspberry Pi running pihole.

The script lists (in this particular order):

  • System date and time
  • CPU (Voltage, temperature, CPU governor, CPU clock speed)

    The CPU governor is red if it is in performance or in ondemand mode
    It is cyan if it is in conservative mode
    It is green if it is in powersave mode
    CPU temperature is cyan if it is 10°C or low, red if it is higher than 37°C, green if it is between 10 to 37.

  • RAM usage (free, being used by processes, cached/buffer ram
  • Pihole status from Pihole CLI
  • Result of DNS resolution using dig. Requires dnsutils.
  • System uptime and average CPU load.

The script doesn't require sudo access. Only requires that the running 'user' be in the group video on raspberry pi. This is required for vcgencmd to work without sudo.

@PTRFRLL

This comment has been minimized.

Copy link

@PTRFRLL PTRFRLL commented May 19, 2020

Thanks for this. I updated my .bashrc file to run this when I log in via SSH:

if [[ -n $SSH_CONNECTION ]]; then
    pi.status
fi
@planetmike

This comment has been minimized.

Copy link

@planetmike planetmike commented May 26, 2020

Celsius temperature isn't intuitive to me, so I tweaked this script like:

Added after line 27:
tempf=/opt/vc/bin/vcgencmd measure_temp | awk -F "[=\']" '{print($2 * 1.8)+32}'

(after the equal you need a back-tick, and at the very end of the line as well, the comment system is interpreting them as code)

Then in lines 35, 38, and 41, I changed temp to tempf and the "C" to "F"

@dchakro

This comment has been minimized.

Copy link
Owner Author

@dchakro dchakro commented Jul 20, 2020

@planetmike do you know of a way to detect if the system uses °F or °C ? I can code that in to be automatic based on that particular system pref.
Planning to submit this script as a PR to pihole.

@planetmike

This comment has been minimized.

Copy link

@planetmike planetmike commented Jul 21, 2020

@planetmike do you know of a way to detect if the system uses °F or °C ? I can code that in to be automatic based on that particular system pref.

The /opt/vc/bin/vcgencmd measure_temp tool returns the temperature in C. My snippet of code is just doing the math to generate the F equivalent. I don't know if the PiHole system is able tor return info at the command line. I don't see the ability to read the status of pihole temperature at the command line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment