-
Notifications
You must be signed in to change notification settings - Fork 0
/
30-disk-health
59 lines (51 loc) · 1.73 KB
/
30-disk-health
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
#!/bin/bash
# Set colors for better visibility
green='\033[0;32m'
yellow='\033[0;33m'
red='\033[0;31m'
reset='\033[0m'
# Function to check disk status and display appropriate color
check_disk_status() {
disk="$1"
status=$(sudo smartctl -H $disk | awk '/test result/ {print $NF}')
temperature=$(sudo smartctl -A $disk | awk '/Temperature_Celsius|Temperature_Internal/ {print $10}')
# Set temperature to 0 if it's not a valid number
if ! [[ "$temperature" =~ ^[0-9]+$ ]]; then
temperature=00
fi
# Set the temperature color based on the thresholds
if [ "$temperature" -ge 1 ] && [ "$temperature" -le 50 ]; then
temp_color=$green
elif [ "$temperature" -ge 51 ] && [ "$temperature" -le 60 ]; then
temp_color=$yellow
else
temp_color=$red
fi
if [ "$status" == "PASSED" ]; then
echo -e "${green}OK${reset} (${temp_color}${temperature}°C${reset})"
elif [ "$status" == "FAILED!" ]; then
echo -e "${red}FAIL${reset} (${temp_color}${temperature}°C${reset})"
else
echo -e "${yellow}WARN${reset} (${temp_color}${temperature}°C${reset})"
fi
}
# Display disk information and status with aligned columns in groups of 3
echo "" # blank
echo -e "DISK HEALTH [${green}OK${reset}/${yellow}WARN${reset}/${red}FAIL${reset}]"
count=0
for disk in $(lsblk -o NAME -nl | grep -E '^sd[a-z]$|^nvme[0-9]n[0-9]$|^mmcblk[0-9]$|^xvd[a-z]$'); do
status=$(check_disk_status "/dev/$disk")
printf " %-11s%s" "$disk" "$status"
((count++))
# Add a newline after every 3 disks
if [ $count -lt 3 ]; then
echo -n " "
else
echo
count=0
fi
done
# Add a newline if the last group doesn't have 3 disks
if [ $count -ne 0 ]; then
echo
fi