forked from slowpeek/hddtemp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhddtemp-lt
executable file
·177 lines (137 loc) · 3.83 KB
/
hddtemp-lt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
# MIT license (c) 2022 https://github.com/slowpeek
# Homepage: https://github.com/slowpeek/hddtemp
set -eu
shopt -s nullglob
# A very basic replacement for hddtemp tool missing from Ubuntu
# 22.04. It requires smartctl (smartmontools package).
SCRIPT_VERSION=0.2+git
SCRIPT_SELF=${BASH_SOURCE[0]##*/}
t_red=$'\e[31m'
t_yellow=$'\e[33m'
t_reset=$'\e(B\e[m'
verb=2
_log() {
(( verb < $1 )) && return
echo "$2" "${@:3}" >&2
}
log_warn() { _log 2 "${t_yellow}warning:${t_reset}" "$@"; }
log_err() { _log 1 "${t_red}error:${t_reset}" "$@"; }
bye() {
log_err "$@"
exit 1
}
version() {
echo "$SCRIPT_SELF $SCRIPT_VERSION"
exit
}
usage() {
cat <<EOF
USAGE --
${SCRIPT_SELF}
Show temps for all sd and nvme disks
${SCRIPT_SELF} /dev/sda ...
Show temps for selected disks
Options:
-h, --help Show usage
-q Suppress warnings
-V, --version Show version
Homepage https://github.com/slowpeek/hddtemp
EOF
exit
}
temp_ata() {
local line
for line; do
if [[ $line == *'Temperature_Celsius'* ]]; then
read -r _ _ _ _ _ _ _ _ _ temp <<< "$line"
break
fi
done
}
temp_nvme() {
local line
for line; do
if [[ $line == 'Temperature:'* ]]; then
read -r _ temp <<< "$line"
break
fi
done
}
temp_scsi() {
local line
for line; do
if [[ $line == 'Current Drive Temperature:'* ]]; then
read -r _ _ _ temp <<< "$line"
break
fi
done
}
main() {
local opts
# jetopt hhelp q Vversion
opts=$(getopt -o hqV -l help,version -- "$@") || exit
eval set -- "$opts"
while (( $# )); do
case $1 in
-h|--help)
usage ;;
-V|--version)
version ;;
-q)
(( --verb )) || verb=1
shift ;;
--)
shift
break ;;
esac
done
type -P smartctl &>/dev/null || bye 'smartctl is not available'
(( EUID )) && bye 'smartctl requires root permissions, run with sudo'
# Check smartctl version for NVMe support.
local ver
read -r _ ver _ < <(smartctl -V)
[[ $ver == @([0-5].*|6.[0-4]*) ]] &&
log_warn "startctl supports NVMe devices since v6.5 (yours is v$ver)"
# If no args supplied, assume all sd and nvme disks.
(( $# )) || set -- /dev/sd? /dev/nvme?n?
local items=() l1=0 l2=1
local disk name temp lines line n parser
for disk; do
[[ $disk == /dev/* ]] || continue
name=
temp=
readarray -t lines < <(smartctl -i -A "$disk" 2>&1)
# There are three printers in smartctl overall: ataprint,
# nvmeprint, scsiprint. Leverage the output formats
# peculiarities.
n=0
for line in "${lines[@]}"; do
((++n))
case $line in
'Device Model:'*)
read -r _ _ name <<< "$line"
parser=ata
break ;;
'Model Number:'*)
read -r _ _ name <<< "$line"
parser=nvme
break ;;
'Product:'*)
read -r _ name <<< "$line"
parser=scsi
break ;;
esac
done
[[ -z $name ]] || "temp_$parser" "${lines[@]:n}"
# Only keep the first number.
# Sample values: '31 (Min/Max 0/59)', '29 C'
[[ -z ${temp-} ]] || temp=${temp%%[^0-9]*}
# Collect max width of values.
(( ${#disk} > l1 )) && l1=${#disk}
(( ${#name} > l2 )) && l2=${#name}
items+=("$disk:" "${name:-?}" "${temp:-?}")
done
printf "%-$((l1+1))s %-${l2}s %s\n" "${items[@]}"
}
[[ ! ${BASH_SOURCE[0]} == "$0" ]] || main "$@"