Skip to content

Instantly share code, notes, and snippets.

@KristupasSavickas
Last active July 5, 2018 10:26
Show Gist options
  • Save KristupasSavickas/a20beef4a6bdea4208d4de1a72b93998 to your computer and use it in GitHub Desktop.
Save KristupasSavickas/a20beef4a6bdea4208d4de1a72b93998 to your computer and use it in GitHub Desktop.
Shell script to print HDD temperatures (no root required!)
#!/bin/sh
# Dependencies:
# 1. qdbus
# sudo apt install qdbus
# 2. udisks2
# sudo apt install udisks2 && sudo systemctl start udisks2
#
# This could probably be rewritten to use dbus-send instead of qdbus.
TEMP_LOW_MAX="35"
TEMP_MED_MAX="55"
COLOR_LOW="42" # Green
COLOR_MED="43" # Yellow
COLOR_HIGH="41" # Red
print_help() {
echo "hdd_temps - print temperatures for all available hard drives"
echo "Usage: hdd_temps [options]"
echo "Options:"
echo " -c | --colorize colorize temperatures"
echo " -h | --help print this page"
}
dbus_call(){
local type bus object method
type=$1
bus=$2
object=$3
method=$4
qdbus --$type $bus $object $method 2>/dev/null
}
round() {
printf "%.0f" "$1"
}
get_color() {
local celcius rounded color
celcius=$1
rounded=$(round $celcius)
if [ $rounded -lt $TEMP_LOW_MAX ]; then
color=$COLOR_LOW
elif [ $rounded -lt $TEMP_MED_MAX ]; then
color=$COLOR_MED
else
color=$COLOR_HIGH
fi
echo $color
}
print_temps() {
local colorize type bus vendor model kelvins celcius temp_str
colorize=$1
type="system"
bus="org.freedesktop.UDisks2"
colorize=$1
dbus_call $type $bus | grep drives\/ | while read -r object; do
vendor=$(dbus_call $type $bus $object $bus.Drive.Vendor)
model=$(dbus_call $type $bus $object $bus.Drive.Model)
kelvins=$(dbus_call $type $bus $object $bus.Drive.Ata.SmartTemperature)
if [ $? -eq 0 ]; then
celcius=$(echo $kelvins - 273 | bc)
if [ -z "$colorize" ]; then
color="0"
else
color=$(get_color $celcius)
fi
printf "%-25s" "$model:"
echo -n "\e[${color}m${celcius}\e[0m\n"
fi
done
}
main() {
local colorize
while [ $# -gt 0 ]; do
case $1 in
-c|--colorize)
colorize=$1
shift
;;
-h|--help)
print_help
return 0
;;
*)
echo "Unknown uption - $(echo $1 | sed 's/-//g')" 1>&2
print_help
return 1
;;
esac
done
print_temps $colorize
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment