Simple CPU and Disk Temperature Check on Synology

I wanted a simple way to check the CPU and disk temperatures on my Synology NAS.

Instead of running several commands every time, I created a small script called temp.

It shows the CPU temperature, HDD temperature, SSD temperature, and disk model.

Script

#!/bin/sh

echo "===== CPU ====="

for h in /sys/class/hwmon/hwmon*; do
    [ -d "$h" ] || continue

    name=$(cat "$h/name" 2>/dev/null)

    [ "$name" = "coretemp" ] || continue

    for t in "$h"/temp*_input; do
        [ -e "$t" ] || continue

        n="${t%_input}"
        label=$(cat "${n}_label" 2>/dev/null)
        value=$(cat "$t" 2>/dev/null)

        [ -n "$value" ] || continue

        temp=$(awk -v v="$value" 'BEGIN {printf "%.1f°C", v/1000}')

        case "$label" in
            "Physical id 0"|"Package id 0")
                printf "%-15s %s\n" "CPU Package" "$temp"
                ;;
            Core*)
                printf "%-15s %s\n" "$label" "$temp"
                ;;
        esac
    done
done

echo
echo "===== DISKS ====="

get_temp()
{
    dev="$1"

    temp=$(smartctl -A "$dev" 2>/dev/null | awk '
        /Temperature_Celsius/ {
            print $10
            exit
        }
        /Airflow_Temperature_Cel/ {
            print $10
            exit
        }
        /^Current Drive Temperature:/ {
            print $(NF-1)
            exit
        }
        /^Temperature:/ {
            print $2
            exit
        }
    ')

    if [ -z "$temp" ]; then
        temp=$(smartctl -A -d sat "$dev" 2>/dev/null | awk '
            /Temperature_Celsius/ {
                print $10
                exit
            }
            /Airflow_Temperature_Cel/ {
                print $10
                exit
            }
            /^Current Drive Temperature:/ {
                print $(NF-1)
                exit
            }
            /^Temperature:/ {
                print $2
                exit
            }
        ')
    fi

    echo "$temp"
}

hdd_num=0
ssd_num=0

for sysdev in /sys/block/sd*; do
    [ -d "$sysdev" ] || continue

    rotational=$(cat "$sysdev/queue/rotational" 2>/dev/null)
    [ "$rotational" = "1" ] || continue

    disk=$(basename "$sysdev")
    dev="/dev/$disk"

    model=$(cat "$sysdev/device/model" 2>/dev/null | \
        sed 's/^[[:space:]]*//;s/[[:space:]]*$//')

    hdd_num=$((hdd_num + 1))
    temp=$(get_temp "$dev")

    if [ -n "$temp" ]; then
        printf "HDD %-3s %-22s %s°C\n" "$hdd_num" "$model" "$temp"
    else
        printf "HDD %-3s %-22s N/A\n" "$hdd_num" "$model"
    fi
done

for sysdev in /sys/block/sd*; do
    [ -d "$sysdev" ] || continue

    rotational=$(cat "$sysdev/queue/rotational" 2>/dev/null)
    [ "$rotational" = "0" ] || continue

    disk=$(basename "$sysdev")
    dev="/dev/$disk"

    model=$(cat "$sysdev/device/model" 2>/dev/null | \
        sed 's/^[[:space:]]*//;s/[[:space:]]*$//')

    ssd_num=$((ssd_num + 1))
    temp=$(get_temp "$dev")

    if [ "$ssd_num" -eq 1 ]; then
        label="SSD"
    else
        label="SSD $ssd_num"
    fi

    if [ -n "$temp" ]; then
        printf "%-7s %-22s %s°C\n" "$label" "$model" "$temp"
    else
        printf "%-7s %-22s N/A\n" "$label" "$model"
    fi
done

Save it as:

/usr/local/bin/temp

Then make it executable:

chmod +x /usr/local/bin/temp

Now I can simply run:

temp

Example output:

===== CPU =====
CPU Package     43.0°C
Core 0          39.0°C
Core 1          41.0°C
Core 2          41.0°C
Core 3          41.0°C

===== DISKS =====
HDD 1   DS42HKVS-78C4GY0     36°C
HDD 2   WD40EJRX-89T1XY0     37°C
HDD 3   WD40EJRX-89T1XY0     39°C
SSD     410G                  39°C

This makes it much easier to quickly check the temperature of my Synology NAS.

Leave a Reply

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