-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathugreen-diskiomon
318 lines (275 loc) · 11.2 KB
/
ugreen-diskiomon
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/bash
# TheLinuxGuy 07/15/2024 - Modified for DXP6800 Pro static mapping of LEDs via HCTL because ata sucks.
# function for removing lockfile
exit-ugreen-diskiomon() {
if [[ -f "/var/run/ugreen-diskiomon.lock" ]]; then
rm "/var/run/ugreen-diskiomon.lock"
fi
kill $smart_check_pid 2>/dev/null
kill $zpool_check_pid 2>/dev/null
kill $disk_online_check_pid 2>/dev/null
}
# trap exit and remove lockfile
trap 'exit-ugreen-diskiomon' EXIT
# check if script is already running
if [[ -f "/var/run/ugreen-diskiomon.lock" ]]; then
echo "ugreen-diskiomon already running!"
exit 1
fi
touch /var/run/ugreen-diskiomon.lock
# use variables from config file (unRAID specific)
if [[ -f /boot/config/plugins/ugreenleds-driver/settings.cfg ]]; then
source /boot/config/plugins/ugreenleds-driver/settings.cfg
fi
# load environment variables
if [[ -f /etc/ugreen-leds.conf ]]; then
source /etc/ugreen-leds.conf
fi
# led-disk mapping (see https://github.com/miskcoo/ugreen_dx4600_leds_controller/pull/4)
MAPPING_METHOD=${MAPPING_METHOD:=ata} # ata, hctl, serial
led_map=(disk1 disk2 disk3 disk4 disk5 disk6 disk7 disk8)
# hctl, $> lsblk -S -x hctl -o hctl,serial,name
# NOTE: It is reported that the order below should be adjusted for each model.
# Please check the disk mapping section in https://github.com/miskcoo/ugreen_dx4600_leds_controller/blob/master/README.md.
# hctl_map=("0:0:0:0" "1:0:0:0" "2:0:0:0" "3:0:0:0" "4:0:0:0" "5:0:0:0" "6:0:0:0" "7:0:0:0")
# TheLinuxGuy DXP6800
hctl_map=("2:0:0:0" "3:0:0:0" "4:0:0:0" "5:0:0:0" "6:0:0:0" "7:0:0:0" "0:0:0:0" "1:0:0:0")
# serial number, $> lsblk -S -x hctl -o hctl,serial,name
serial_map=(${DISK_SERIAL})
# ata number, $> ls /sys/block | egrep ata\d
ata_map=("ata1" "ata2" "ata3" "ata4" "ata5" "ata6" "ata7" "ata8")
if which dmidecode > /dev/null; then
product_name=$(dmidecode --string system-product-name)
case "${product_name}" in
DXP6800*) # tested on DXP6800 Pro
echo "Found UGREEN DXP6800 series"
hctl_map=("2:0:0:0" "3:0:0:0" "4:0:0:0" "5:0:0:0" "0:0:0:0" "1:0:0:0")
ata_map=("ata3" "ata4" "ata5" "ata6" "ata1" "ata2")
;;
DX4600*) # tested on DX4600 Pro
echo "Found UGREEN DX4600 series"
;;
DX4700*)
echo "Found UGREEN DX4700 series"
;;
DXP2800*) # see issue #19
echo "Found UGREEN DXP2800 series"
;;
DXP4800*)
echo "Found UGREEN DXP4800 series"
;;
DXP8800*) # tested on DXP8800 Plus
echo "Found UGREEN DXP8800 series"
# using the default mapping
;;
*)
if [[ "${MAPPING_METHOD}" == "hctl" || "${MAPPING_METHOD}" == "ata" ]]; then
echo -e "\033[0;31mUsing the default HCTL order. Please check it maps to your disk slots correctly."
echo -e "If you confirm that the HCTL order is correct, or find it is different, you can "
echo -e "submit an issue to let us know, so we can update the script."
echo -e "Please read the disk mapping section in the link below for more details. "
echo -e " https://github.com/miskcoo/ugreen_dx4600_leds_controller/blob/master/README.md\033[0m"
fi
;;
esac
elif [[ "${MAPPING_METHOD}" == "hctl" || "${MAPPING_METHOD}" == "ata" ]]; then
echo -e "\033[0;31minstalling the tool `dmidecode` is suggested; otherwise the script cannot detect your device and adjust the hctl/ata_map\033[0m"
fi
declare -A devices
# set monitor SMART information to true by default if not running unRAID
if [[ -f /etc/unraid-version ]]; then
CHECK_SMART=false
else
CHECK_SMART=${CHECK_SMART:=true}
fi
# polling rate for smartctl. 360 seconds by default
CHECK_SMART_INTERVAL=${CHECK_SMART_INTERVAL:=360}
# refresh interval from disk leds
LED_REFRESH_INTERVAL=${LED_REFRESH_INTERVAL:=0.1}
# whether to check zpool health
CHECK_ZPOOL=${CHECK_ZPOOL:=false}
# polling rate for checking zpool health. 5 seconds by default
CHECK_ZPOOL_INTERVAL=${CHECK_ZPOOL_INTERVAL:=5}
# polling rate for checking disk online. 5 seconds by default
CHECK_DISK_ONLINE_INTERVAL=${CHECK_DISK_ONLINE_INTERVAL:=5}
COLOR_DISK_HEALTH=${COLOR_DISK_HEALTH:="255 255 255"}
COLOR_DISK_UNAVAIL=${COLOR_DISK_UNAVAIL:="255 0 0"}
COLOR_ZPOOL_FAIL=${COLOR_ZPOOL_FAIL:="255 0 0"}
COLOR_SMART_FAIL=${COLOR_SMART_FAIL:="255 0 0"}
BRIGHTNESS_DISK_LEDS=${BRIGHTNESS_DISK_LEDS:="255"}
{ lsmod | grep ledtrig_oneshot > /dev/null; } || { modprobe -v ledtrig_oneshot && sleep 2; }
function disk_enumerating_string() {
if [[ $MAPPING_METHOD == ata ]]; then
ls -ahl /sys/block | sed 's/\/$//' | awk '{
if (match($0, /ata[0-9]+/)) {
ata = substr($0, RSTART, RLENGTH);
if (match($0, /[^\/]+$/)) {
basename = substr($0, RSTART, RLENGTH);
print basename, ata;
}
}
}'
elif [[ $MAPPING_METHOD == hctl || $MAPPING_METHOD == serial ]]; then
lsblk -S -o name,${MAPPING_METHOD},tran | grep sata
else
echo Unsupported mapping method: ${MAPPING_METHOD}
exit 1
fi
}
echo Enumerating disks based on $MAPPING_METHOD...
declare -A dev_map
while read line
do
blk_line=($line)
key=${blk_line[1]}
val=${blk_line[0]}
dev_map[${key}]=${val}
echo $MAPPING_METHOD ${key} ">>" ${dev_map[${key}]}
done <<< "$(disk_enumerating_string)"
# initialize LEDs
declare -A dev_to_led_map
for i in "${!led_map[@]}"; do
led=${led_map[i]}
if [[ -d /sys/class/leds/$led ]]; then
echo oneshot > /sys/class/leds/$led/trigger
echo 1 > /sys/class/leds/$led/invert
echo 100 > /sys/class/leds/$led/delay_on
echo 100 > /sys/class/leds/$led/delay_off
echo "$COLOR_DISK_HEALTH" > /sys/class/leds/$led/color
echo "$BRIGHTNESS_DISK_LEDS" > /sys/class/leds/$led/brightness
# find corresponding device
_tmp_str=${MAPPING_METHOD}_map[@]
_tmp_arr=(${!_tmp_str})
if [[ -v "dev_map[${_tmp_arr[i]}]" ]]; then
dev=${dev_map[${_tmp_arr[i]}]}
if [[ -f /sys/class/block/${dev}/stat ]]; then
devices[$led]=${dev}
dev_to_led_map[$dev]=$led
else
# turn off the led if no disk installed on this slot
echo 0 > /sys/class/leds/$led/brightness
echo none > /sys/class/leds/$led/trigger
fi
else
# turn off the led if no disk installed on this slot
echo 0 > /sys/class/leds/$led/brightness
echo none > /sys/class/leds/$led/trigger
fi
fi
done
# construct zpool device mapping
declare -A zpool_ledmap
if [ "$CHECK_ZPOOL" = true ]; then
echo Enumerating zpool devices...
while read line
do
zpool_dev_line=($line)
zpool_dev_name=${zpool_dev_line[0]}
zpool_scsi_dev_name="unknown"
# zpool_dev_state=${zpool_dev_line[1]}
case "$zpool_dev_name" in
sd*)
# remove the trailing partition number
zpool_scsi_dev_name=$(echo $zpool_dev_name | sed 's/[0-9]*$//')
;;
dm*)
# find the underlying block device of the encrypted device
dm_slaves=($(ls /sys/block/${zpool_dev_name}/slaves))
zpool_scsi_dev_name=${dm_slaves[0]}
;;
*)
echo Unsupported zpool device type ${zpool_dev_name}.
;;
esac
# if the detected scsi device can be found in the mapping array
#echo zpool $zpool_dev_name ">>" $zpool_scsi_dev_name ">>" ${dev_to_led_map[${zpool_scsi_dev_name}]}
if [[ -v "dev_to_led_map[${zpool_scsi_dev_name}]" ]]; then
zpool_ledmap[$zpool_dev_name]=${dev_to_led_map[${zpool_scsi_dev_name}]}
echo "zpool device" $zpool_dev_name ">>" $zpool_scsi_dev_name ">> LED:"${zpool_ledmap[$zpool_dev_name]}
fi
done <<< "$(zpool status -L | egrep ^\\s*\(sd\|dm\))"
function zpool_check_loop() {
while true; do
while read line
do
zpool_dev_line=($line)
zpool_dev_name=${zpool_dev_line[0]}
zpool_dev_state=${zpool_dev_line[1]}
# TODO: do something if the pool is unhealthy?
if [[ -v "zpool_ledmap[${zpool_dev_name}]" ]]; then
led=${zpool_ledmap[$zpool_dev_name]}
if [[ "$(cat /sys/class/leds/$led/color)" != "$COLOR_DISK_HEALTH" ]]; then
continue;
fi
if [[ "${zpool_dev_state}" != "ONLINE" ]]; then
echo "$COLOR_ZPOOL_FAIL" > /sys/class/leds/$led/color
echo Disk failure detected on /dev/$dev at $(date +%Y-%m-%d' '%H:%M:%S)
fi
# ==== To recover from an error, you should restart the script ====
## case "${zpool_dev_state}" in
## ONLINE)
## # echo "$COLOR_DISK_HEALTH" > /sys/class/leds/$led/color
## ;;
## *)
## echo "255 0 0" > /sys/class/leds/$led/color
## ;;
## esac
fi
done <<< "$(zpool status -L | egrep ^\\s*\(sd\|dm\))"
sleep ${CHECK_ZPOOL_INTERVAL}s
done
}
zpool_check_loop &
zpool_check_pid=$!
fi
# check disk health if enabled
if [ "$CHECK_SMART" = true ]; then
(
while true; do
for led in "${!devices[@]}"; do
if [[ "$(cat /sys/class/leds/$led/color)" != "$COLOR_DISK_HEALTH" ]]; then
continue;
fi
dev=${devices[$led]}
if [[ -z $(smartctl -H /dev/${dev} | grep PASSED) ]]; then
echo "$COLOR_SMART_FAIL" > /sys/class/leds/$led/color
echo Disk failure detected on /dev/$dev at $(date +%Y-%m-%d' '%H:%M:%S)
continue
fi
done
sleep ${CHECK_SMART_INTERVAL}s
done
) &
smart_check_pid=$!
fi
# check disk online status
(
while true; do
for led in "${!devices[@]}"; do
dev=${devices[$led]}
if [[ "$(cat /sys/class/leds/$led/color)" != "$COLOR_DISK_HEALTH" ]]; then
continue;
fi
if [[ ! -f /sys/class/block/${dev}/stat ]]; then
echo "$COLOR_DISK_UNAVAIL" > /sys/class/leds/$led/color 2>/dev/null
echo Disk /dev/$dev went offline at $(date +%Y-%m-%d' '%H:%M:%S)
continue
fi
done
sleep ${CHECK_DISK_ONLINE_INTERVAL}s
done
) &
disk_online_check_pid=$!
# monitor disk activities
declare -A diskio_data_rw
while true; do
for led in "${!devices[@]}"; do
# if $dev does not exist, diskio_new_rw="", which will be safe
diskio_new_rw="$(cat /sys/block/${devices[$led]}/stat 2>/dev/null)"
if [ "${diskio_data_rw[$led]}" != "${diskio_new_rw}" ]; then
echo 1 > /sys/class/leds/$led/shot
fi
diskio_data_rw[$led]=$diskio_new_rw
done
sleep ${LED_REFRESH_INTERVAL}s
done