Skip to content

Instantly share code, notes, and snippets.

@dchakro
Last active December 5, 2021 11:03
Show Gist options
  • Save dchakro/f60ec10aa1ca0438f8b8354a1b4a79d7 to your computer and use it in GitHub Desktop.
Save dchakro/f60ec10aa1ca0438f8b8354a1b4a79d7 to your computer and use it in GitHub Desktop.
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=$(vcgencmd measure_volts core | cut -d'=' -f 2)
temp=$(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
@planetmike
Copy link

@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