Portál AbcLinuxu, 15. listopadu 2025 06:23
#!/bin/bash
get_disk_temperature() {
local disk_path=$1
local output=$(smartctl -a "$disk_path" 2>/dev/null)
local temperature=$(echo "$output" | awk '/Temperature_Celsius/ { print $10 }')
if [[ -n $temperature ]]; then
echo "$temperature"
else
echo "-1"
fi
}
main() {
local disks_output=$(lsblk -d -n -o NAME)
local disks=($disks_output)
for disk in "${disks[@]}"; do
if [[ $disk == sd* || $disk == nvme* ]]; then
local temperature=$(get_disk_temperature "/dev/$disk")
if [[ $temperature != -1 ]]; then
echo "Disk temperature for $disk: $temperature°C"
else
echo "Disk temperature for $disk is not available."
fi
fi
done
}
main
Ukážka výstupu:
$ ./hddtemp.sh Disk temperature for sda: 35°C Disk temperature for sdb: 39°C Disk temperature for sdc: 38°C Disk temperature for sdd: 32°C Disk temperature for sde is not available.Druhá náhrada hddtemp2.sh rieši zobrazenie iba samotnej teploty pre zvolený disk sd[x] a/alebo nvme[x] zariadenia.
#!/bin/bash
get_disk_temperature() {
local disk_path=$1
local output=$(smartctl -a "$disk_path" 2>/dev/null)
local temperature=$(echo "$output" | awk '/Temperature_Celsius/ { print $10 }')
if [[ -n $temperature ]]; then
echo "$temperature"
else
echo "-1"
fi
}
main() {
local disk_name=$1
if [[ -z $disk_name ]]; then
echo "Error: Disk name not provided."
echo "Usage: $0 <disk_name>>"
exit 1
fi
local disk_path="/dev/$disk_name"
if [[ ! -e $disk_path ]]; then
echo "Error: Disk $disk_name does not exist."
exit 1
fi
local temperature=$(get_disk_temperature "$disk_path")
if [[ $temperature != -1 ]]; then
echo "$temperature°C"
else
echo "Disk temperature for $disk_name is not available."
fi
}
main "$@"
Ukážka výstupu:
$ ./hddtemp2.sh sda 35°CMožno to niekomu pomôže. Tieto scripty vyžadujú inštaláciu balíčka smartmontools
Tiskni
Sdílej:
#!/bin/bash
echo "hdt.sh v0.1 - bash script displays the temperature of hd/nvme disks"
echo " "
echo "--help show usage of script"
echo " "
get_disk_temperature() {
local disk_path=$1
local temperature=$(smartctl -a "$disk_path" 2>/dev/null | awk '/Temperature_Celsius/ { print $10 }')
if [[ -n $temperature ]]; then
echo "$temperature°C"
else
echo "Disk temperature is not available."
fi
}
display_usage() {
echo "Usage: $0 [disk_name]"
echo "disk_name (optional): Name of the disk to display the temperature."
echo "If not provided, temperatures for all eligible disks will be displayed."
echo "Package smartmontools is mandatory."
}
main() {
if [[ $1 == "--help" ]]; then
display_usage
exit 0
fi
if [[ -n $1 ]]; then
local disk_path="/dev/$1"
if [[ -e $disk_path ]]; then
get_disk_temperature "$disk_path"
else
echo "Error: Disk $1 does not exist."
exit 1
fi
else
local disks_output=$(lsblk -d -n -o NAME)
for disk in ${disks_output[@]}; do
if [[ $disk == sd* || $disk == nvme* ]]; then
local disk_path="/dev/$disk"
echo "Disk temperature for $disk: $(get_disk_temperature "$disk_path")"
fi
done
fi
}
main "$@"
Bez roota to nepôjde. Spustil som strace s rootom a bez. Podľa toho čo píše strace tak to spadne na prístupových právachk danému zariadeniu sdX.
openat(AT_FDCWD, "/dev/sda", O_RDONLY|O_NONBLOCK) = 3
ioctl(3, SG_IO, {interface_id='S', dxfer_direction=SG_DXFER_FROM_DEV, cmd_len=6,
cmdp="\x12\x00\x00\x00\x24\x00", mx_sb_len=32, iovec_count=0, dxfer_len=36, tim
eout=60000, flags=0, dxferp="\x00\x00\x05\x02\x5b\x00\x00\x02\x41\x54\x41\x20\x20\x20\x20\x20\x57\x44\x43\x20\x57\x44\x35\x30\x30\x33\x41\x5a\x45\x58\x2d\x30"..., status=0, masked_status=0, msg_status=0, sb_len_wr=0, sbp="", host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
ioctl(3, SG_IO, {interface_id='S', dxfer_direction=SG_DXFER_FROM_DEV, cmd_len=16, cmdp="\x85\x08\x0e\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\xec\x00", mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=60000, flags=0, dxferp="\x7a\x42\xff\x3f\x37\xc8\x10\x00\x00\x00\x00\x00\x3f\x00\x00\x00\x00\x00\x00\x00\x20\x20\x20\x20\x57\x20\x2d\x44\x43\x57\x36\x43"..., status=0, masked_status=0, msg_status=0, sb_len_wr=0, sbp="", host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
openat(AT_FDCWD, "/dev/sda", O_RDONLY|O_NONBLOCK) = -1 EACCES (Permission denied)
write(1, "Smartctl open device: /dev/sda f"..., 57) = 57
exit_group(2) = ?
+++ exited with 2 +++
CAP_FOWNER,CAP_RAWIO. Lenže tieto povolenia sú všeobecne a zatial neviem o tom, že existuje ako vyriešiť riadené prístupu. Na bezpečný prístup k daným funkciam by musela byť aplikácia prepísaná. Čo asi nebude a zrejme bude nutné doprogramovať nejaký výstup cez filesystém sysfs.
Napríklad teploty, napätia a frekvenicie sú v /sys/class/hwmon/
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.