-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskTempMonitoring.sh
More file actions
990 lines (859 loc) · 37.2 KB
/
DiskTempMonitoring.sh
File metadata and controls
990 lines (859 loc) · 37.2 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
#!/bin/bash
# Drive Temperature Monitor with Color Coding & Bar Graph
# Displays all drives with temperatures color-coded from green (cool) to red (hot)
# Temperature scale now uses a 1-color-per-degree map for 30°C..65°C
# Bar graph now dynamically expands when columns are hidden
# Truvis Thornton [ http://truv.is ]
# ============================================================================
# Default Configuration Variables
# ============================================================================
# Time window for min/max statistics (in hours)
# Default: 24 hours
STATS_TIME_WINDOW=24
# Hide drives with no temperature data (N/A)
# Options: true | false
HIDE_NA_DRIVES=false
# Temperature display format
# Options: "both" | "celsius" | "fahrenheit"
TEMP_DISPLAY="both"
# Use in-memory tracking instead of file-based logging
# Options: true | false
# When true: Stats are tracked in memory only (lost on restart)
# When false: Stats are logged to LOG_FILE for persistence
USE_MEMORY_TRACKING=false
# Show/Hide columns
# Options: true | false
SHOW_STATS=true # Show min/max temperature column
SHOW_SERIAL=true # Show serial number column
SHOW_MODEL=true # Show model column
# Refresh interval in seconds
REFRESH_INTERVAL=2
# Log file location (only used when USE_MEMORY_TRACKING=false)
LOG_FILE="/var/log/drive_temps.log"
# ============================================================================
# Command-line Argument Parsing
# ============================================================================
show_help() {
cat << EOF
Drive Temperature Monitor - Version 2
USAGE:
sudo $0 [OPTIONS]
OPTIONS:
-h, --help Show this help message
-t, --temp-display FORMAT
Temperature display format: both, celsius, fahrenheit
Default: both
-w, --stats-window HOURS
Time window for min/max statistics in hours
Default: 24
-r, --refresh SECONDS Refresh interval in seconds
Default: 2
-m, --memory Use in-memory tracking (data lost on restart)
Default: file-based logging
-l, --log-file PATH Log file location for temperature tracking
Default: /var/log/drive_temps.log
-n, --hide-na Hide drives with no temperature data
Default: show all drives
--show-stats Show min/max temperature column (default)
--hide-stats Hide min/max temperature column
--show-serial Show serial number column (default)
--hide-serial Hide serial number column
--show-model Show model column (default)
--hide-model Hide model column
EXAMPLES:
# Run with defaults
sudo $0
# Show only Celsius, hide serial and model columns
sudo $0 --temp-display celsius --hide-serial --hide-model
# Use in-memory tracking with 5-second refresh
sudo $0 --memory --refresh 5
# Show only temperature bar and drive name
sudo $0 --hide-stats --hide-serial --hide-model
EOF
exit 0
}
# Parse command-line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
;;
-t|--temp-display)
TEMP_DISPLAY="$2"
if [[ ! "$TEMP_DISPLAY" =~ ^(both|celsius|fahrenheit)$ ]]; then
echo "Error: Invalid temperature display format. Use: both, celsius, or fahrenheit"
exit 1
fi
shift 2
;;
-w|--stats-window)
STATS_TIME_WINDOW="$2"
if ! [[ "$STATS_TIME_WINDOW" =~ ^[0-9]+$ ]]; then
echo "Error: Stats window must be a positive integer"
exit 1
fi
shift 2
;;
-r|--refresh)
REFRESH_INTERVAL="$2"
if ! [[ "$REFRESH_INTERVAL" =~ ^[0-9]+$ ]]; then
echo "Error: Refresh interval must be a positive integer"
exit 1
fi
shift 2
;;
-m|--memory)
USE_MEMORY_TRACKING=true
shift
;;
-l|--log-file)
LOG_FILE="$2"
shift 2
;;
-n|--hide-na)
HIDE_NA_DRIVES=true
shift
;;
--show-stats)
SHOW_STATS=true
shift
;;
--hide-stats)
SHOW_STATS=false
shift
;;
--show-serial)
SHOW_SERIAL=true
shift
;;
--hide-serial)
SHOW_SERIAL=false
shift
;;
--show-model)
SHOW_MODEL=true
shift
;;
--hide-model)
SHOW_MODEL=false
shift
;;
*)
echo "Error: Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# ============================================================================
# Check if running as root (needed for smartctl)
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (sudo)"
exit 1
fi
# Check if smartmontools is installed
if ! command -v smartctl &> /dev/null; then
echo "smartctl not found. Please install smartmontools:"
echo " Ubuntu/Debian: sudo apt install smartmontools"
echo " RHEL/CentOS: sudo yum install smartmontools"
echo " Arch: sudo pacman -S smartmontools"
exit 1
fi
# Color map for 30°C .. 65°C (one color code per degree).
# Index 0 -> 30°C, Index 35 -> 65°C
COLOR_MAP=(22 22 28 28 34 34 34 40 40 46 46 70 76 112 190 11 11 220 220 214 214 214 208 208 202 202 210 210 167 167 167 131 131 124 88 1)
# Function to get color based on temperature (foreground)
get_color() {
local temp=$1
# If temp is between 30 and 65 use the provided per-degree map
if [ "$temp" -ge 30 ] && [ "$temp" -le 65 ]; then
local idx=$((temp - 30))
local code="${COLOR_MAP[$idx]}"
if [ -n "$code" ]; then
echo -e "\033[38;5;${code}m"
return
fi
fi
# Fallback gradient for temps outside 30..65
if [ "$temp" -gt 65 ]; then
echo -e "\033[38;5;52m" # Very dark red
elif [ "$temp" -ge 61 ]; then
echo -e "\033[38;5;88m"
elif [ "$temp" -ge 55 ]; then
echo -e "\033[38;5;202m"
elif [ "$temp" -ge 50 ]; then
echo -e "\033[38;5;208m"
elif [ "$temp" -ge 45 ]; then
echo -e "\033[38;5;214m"
elif [ "$temp" -ge 40 ]; then
echo -e "\033[38;5;221m"
elif [ "$temp" -ge 35 ]; then
echo -e "\033[38;5;193m"
elif [ "$temp" -ge 30 ]; then
echo -e "\033[38;5;154m"
elif [ "$temp" -ge 25 ]; then
echo -e "\033[38;5;46m"
elif [ "$temp" -ge 20 ]; then
echo -e "\033[38;5;48m"
elif [ "$temp" -ge 16 ]; then
echo -e "\033[38;5;50m"
else
echo -e "\033[38;5;93m" # Purple (very cool)
fi
}
# Function to get background color based on temperature
get_bg_color() {
local temp=$1
# Keep existing background gradient. This can be updated to a per-degree map if desired.
if [ "$temp" -ge 63 ]; then
echo -e "\033[48;5;88m"
elif [ "$temp" -ge 61 ]; then
echo -e "\033[48;5;124m"
elif [ "$temp" -ge 59 ]; then
echo -e "\033[48;5;160m"
elif [ "$temp" -ge 57 ]; then
echo -e "\033[48;5;196m"
elif [ "$temp" -ge 55 ]; then
echo -e "\033[48;5;202m"
elif [ "$temp" -ge 53 ]; then
echo -e "\033[48;5;208m"
elif [ "$temp" -ge 52 ]; then
echo -e "\033[48;5;214m"
elif [ "$temp" -ge 50 ]; then
echo -e "\033[48;5;215m"
elif [ "$temp" -ge 48 ]; then
echo -e "\033[48;5;220m"
elif [ "$temp" -ge 46 ]; then
echo -e "\033[48;5;221m"
elif [ "$temp" -ge 44 ]; then
echo -e "\033[48;5;226m"
elif [ "$temp" -ge 42 ]; then
echo -e "\033[48;5;227m"
elif [ "$temp" -ge 40 ]; then
echo -e "\033[48;5;228m"
elif [ "$temp" -ge 38 ]; then
echo -e "\033[48;5;192m"
elif [ "$temp" -ge 36 ]; then
echo -e "\033[48;5;228m"
elif [ "$temp" -ge 35 ]; then
echo -e "\033[48;5;193m"
elif [ "$temp" -ge 34 ]; then
echo -e "\033[48;5;157m"
elif [ "$temp" -ge 33 ]; then
echo -e "\033[48;5;120m"
elif [ "$temp" -ge 32 ]; then
echo -e "\033[48;5;82m"
elif [ "$temp" -ge 30 ]; then
echo -e "\033[48;5;46m"
elif [ "$temp" -ge 28 ]; then
echo -e "\033[48;5;47m"
elif [ "$temp" -ge 26 ]; then
echo -e "\033[48;5;48m"
elif [ "$temp" -ge 24 ]; then
echo -e "\033[48;5;49m"
elif [ "$temp" -ge 22 ]; then
echo -e "\033[48;5;50m"
elif [ "$temp" -ge 20 ]; then
echo -e "\033[48;5;51m"
elif [ "$temp" -ge 18 ]; then
echo -e "\033[48;5;45m"
elif [ "$temp" -ge 16 ]; then
echo -e "\033[48;5;39m"
elif [ "$temp" -ge 14 ]; then
echo -e "\033[48;5;33m"
else
echo -e "\033[48;5;93m"
fi
}
# Function to create bar graph starting at 28°C with dynamic width
create_bar_with_temp() {
local temp=$1
local temp_f=$2
local available_width=$3 # Total width available for the entire bar display
local min_temp=28
local max_temp=65
# Calculate how much space the temperature text will take
local temp_text_width
if [ "$TEMP_DISPLAY" = "celsius" ]; then
temp_text_width=7 # "35°C "
elif [ "$TEMP_DISPLAY" = "fahrenheit" ]; then
temp_text_width=8 # "95°F "
else
temp_text_width=13 # "35°C/95°F "
fi
# Available width for brackets + blocks = total - temp text - spacing
local blocks_area=$((available_width - temp_text_width - 2))
if [ $blocks_area -lt 10 ]; then
blocks_area=10 # Minimum blocks area
fi
# Scale the temperature range to fit available blocks
local bar_length=$blocks_area
# Build bar with CJK brackets
local bar="「"
# Determine how many blocks to fill
local filled=0
if [ "$temp" -le "$min_temp" ]; then
# Show only 1 block for temps at or below 28°C
filled=1
# Choose color based on temp
local block_color
if [ "$temp" -le 5 ]; then
# Bright purple for 5°C and below
block_color="\033[38;5;93m"
else
# Light blue for 6-28°C
block_color="\033[38;5;117m"
fi
bar+="${block_color}■${RESET}"
else
# Clamp max temperature
if [ "$temp" -gt "$max_temp" ]; then
temp=$max_temp
fi
# Calculate percentage of the range
local temp_range=$((max_temp - min_temp))
local temp_above_min=$((temp - min_temp))
# Scale to available blocks (filled = percentage * bar_length)
filled=$(( (temp_above_min * bar_length) / temp_range ))
if [ $filled -lt 1 ]; then
filled=1
fi
# Add blocks with color gradient
for ((i=0; i<filled; i++)); do
# Calculate which temperature this block represents
local block_temp=$(( min_temp + (i * temp_range / bar_length) ))
local pos_color
pos_color=$(get_color "$block_temp")
bar+="${pos_color}■${RESET}"
done
fi
# Add empty blocks for remaining space
local empty=$((bar_length - filled))
for ((i=0; i<empty; i++)); do
bar+="\033[48;5;232m\033[38;5;240m■\033[0m"
done
# Get the color for the actual current temperature for the display
local current_temp_color
current_temp_color=$(get_color "$temp")
# Build temperature display based on TEMP_DISPLAY setting
local temp_display
if [ "$TEMP_DISPLAY" = "celsius" ]; then
# Celsius only: "35°C "
temp_display="${current_temp_color}${temp}°C${RESET}"
local visible_len=${#temp}
local padding=$((5 - visible_len)) # Target 5 chars for C-only
if [ $padding -lt 0 ]; then padding=0; fi
temp_display="${temp_display}$(printf ' %.0s' $(seq 1 $padding))"
elif [ "$TEMP_DISPLAY" = "fahrenheit" ]; then
# Fahrenheit only: "95°F " or "104°F "
temp_display="${current_temp_color}$(printf "%3d" "$temp_f")°F${RESET}"
local visible_len=$((${#temp_f} + 2))
local padding=$((6 - visible_len)) # Target 6 chars for F-only
if [ $padding -lt 0 ]; then padding=0; fi
temp_display="${temp_display}$(printf ' %.0s' $(seq 1 $padding))"
else
# Both (default): "35°C/95°F " or "40°C/104°F "
local visible_len=$((${#temp} + 10))
local padding=$((9 - visible_len))
if [ $padding -lt 0 ]; then padding=0; fi
local spacing=$(printf ' %.0s' $(seq 1 $padding))
temp_display="${current_temp_color}${temp}°C${RESET}${BOLD}${WHITE}/${RESET}${current_temp_color}$(printf "%3d" "$temp_f")°F${RESET}${spacing}"
fi
bar+="\033[0m」 ${temp_display}"
echo -e "$bar"
}
# Reset color
RESET="\033[0m"
BOLD="\033[1m"
BLUE="\033[38;5;33m"
CYAN="\033[38;5;51m"
LIGHTBLUE="\033[38;5;117m"
GRAY="\033[38;5;240m"
YELLOW="\033[38;5;226m"
WHITE="\033[38;5;15m"
BLACK="\033[38;5;16m"
DARKBLUE_BG="\033[48;5;17m"
PURPLE_BG="\033[48;5;54m"
TEMPINFO_BG="\033[48;5;240m"
YELLOW_BG="\033[48;5;220m"
# In-memory tracking storage (associative array)
declare -A MEMORY_LOG
# Function to log temperature
log_temperature() {
local drive=$1
local temp=$2
local timestamp=$(date +%s)
if [ "$USE_MEMORY_TRACKING" = "true" ]; then
# Store in memory with format: timestamp|drive|temp
MEMORY_LOG["${timestamp}:${drive}"]="${timestamp}|${drive}|${temp}"
else
# Store in file
echo "${timestamp}|${drive}|${temp}" >> "$LOG_FILE"
fi
}
# Function to get min/max temps within the configured time window for a drive
get_stats() {
local drive=$1
local cutoff=$(($(date +%s) - (STATS_TIME_WINDOW * 3600))) # Convert hours to seconds
if [ "$USE_MEMORY_TRACKING" = "true" ]; then
# Get temps from memory
local temps=""
for key in "${!MEMORY_LOG[@]}"; do
local entry="${MEMORY_LOG[$key]}"
local timestamp
local entry_drive
local temp
timestamp=$(echo "$entry" | cut -d'|' -f1)
entry_drive=$(echo "$entry" | cut -d'|' -f2)
temp=$(echo "$entry" | cut -d'|' -f3)
if [ "$entry_drive" = "$drive" ] && [ "$timestamp" -ge "$cutoff" ]; then
temps="${temps}${temp}"$'\n'
fi
done
if [ -z "$temps" ]; then
echo "N/A|N/A"
return
fi
# Calculate min and max
local min
local max
min=$(echo "$temps" | grep -v '^$' | sort -n | head -1)
max=$(echo "$temps" | grep -v '^$' | sort -n | tail -1)
echo "${min}|${max}"
else
# Get temps from file
if [ ! -f "$LOG_FILE" ]; then
echo "N/A|N/A"
return
fi
# Get temps for this drive within the time window
local temps
temps=$(awk -F'|' -v d="$drive" -v c="$cutoff" '$1 >= c && $2 == d {print $3}' "$LOG_FILE")
if [ -z "$temps" ]; then
echo "N/A|N/A"
return
fi
# Calculate min and max
local min
local max
min=$(echo "$temps" | sort -n | head -1)
max=$(echo "$temps" | sort -n | tail -1)
echo "${min}|${max}"
fi
}
# Function to clean old log entries (older than configured time window)
clean_old_logs() {
local cutoff=$(($(date +%s) - (STATS_TIME_WINDOW * 3600))) # Convert hours to seconds
if [ "$USE_MEMORY_TRACKING" = "true" ]; then
# Clean old entries from memory
for key in "${!MEMORY_LOG[@]}"; do
local entry="${MEMORY_LOG[$key]}"
local timestamp
timestamp=$(echo "$entry" | cut -d'|' -f1)
if [ "$timestamp" -lt "$cutoff" ]; then
unset MEMORY_LOG["$key"]
fi
done
else
# Clean old entries from file
if [ ! -f "$LOG_FILE" ]; then
return
fi
local temp_file=$(mktemp)
# Keep only entries within the time window
awk -F'|' -v c="$cutoff" '$1 >= c' "$LOG_FILE" > "$temp_file"
# Only update the log file if the temp file was created successfully
if [ -s "$temp_file" ]; then
mv "$temp_file" "$LOG_FILE"
else
# If temp file is empty but log file exists, it means all entries are old
# Keep the file but make it empty
if [ -f "$LOG_FILE" ]; then
> "$LOG_FILE"
fi
rm -f "$temp_file"
fi
fi
}
# Trap Ctrl+C to exit cleanly
trap 'echo -e "\n${GRAY}Monitoring stopped.${RESET}"; exit 0' INT
# Main monitoring loop
while true; do
# Clean old log entries once per iteration
clean_old_logs
# Clear screen
clear
# Get terminal width
TERM_WIDTH=$(tput cols)
# Create border line
BORDER_LINE=$(printf '═%.0s' $(seq 1 $((TERM_WIDTH - 2))))
# Print header box with full terminal width
echo -e "${PURPLE_BG}${BOLD}${LIGHTBLUE}╔${BORDER_LINE}╗${RESET}"
# Title line
TITLE="Drive Temperature Monitor - Version 2 [ Truvis Thornton - http://truv.is ]"
TITLE_LEN=${#TITLE}
# Account for the space after ║ and before ║ (2 spaces total) plus the two ║ chars (2)
PAD_LEN=$((TERM_WIDTH - TITLE_LEN - 4))
if [ $PAD_LEN -lt 0 ]; then PAD_LEN=0; fi
PADDING=$(printf ' %.0s' $(seq 1 $PAD_LEN))
echo -e "${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}${PURPLE_BG} ${BOLD}${YELLOW}${TITLE}${PADDING} ${RESET}${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}"
# Refresh info line with time window
if [ "$USE_MEMORY_TRACKING" = "true" ]; then
REFRESH_TEXT="Refreshing every ${REFRESH_INTERVAL}s - Stats Window: ${STATS_TIME_WINDOW}h (In-Memory) - Press Ctrl+C to exit"
else
REFRESH_TEXT="Refreshing every ${REFRESH_INTERVAL}s - Stats Window: ${STATS_TIME_WINDOW}h - Press Ctrl+C to exit"
fi
REFRESH_LEN=${#REFRESH_TEXT}
REFRESH_PAD=$((TERM_WIDTH - REFRESH_LEN - 4))
if [ $REFRESH_PAD -lt 0 ]; then REFRESH_PAD=0; fi
REFRESH_PADDING=$(printf ' %.0s' $(seq 1 $REFRESH_PAD))
echo -e "${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}${PURPLE_BG} ${WHITE}${REFRESH_TEXT}${RESET}${PURPLE_BG}${REFRESH_PADDING} ${RESET}${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}"
# Separator line after refresh info
echo -e "${PURPLE_BG}${BOLD}${LIGHTBLUE}╠${BORDER_LINE}╣${RESET}"
# Temperature range info based on color mapping (bars start at 28°C)
# Note: Emoji is counted as ~4 bytes but displays as 2 columns
# We need to calculate visible length separately from the colored string
INFO1="${LIGHTBLUE}🌡️${RESET} Cool: ≤28°C (≤82°F) - Light Blue (1 block)"
INFO1_VISIBLE="🌡️ Cool: ≤28°C (≤82°F) - Light Blue (1 block)"
INFO1_LEN=${#INFO1_VISIBLE}
INFO1_PAD=$((TERM_WIDTH - INFO1_LEN - 3)) # -3 to compensate for emoji display width
if [ $INFO1_PAD -lt 0 ]; then INFO1_PAD=0; fi
INFO1_PADDING=$(printf ' %.0s' $(seq 1 $INFO1_PAD))
echo -e "${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}${TEMPINFO_BG} ${LIGHTBLUE}🌡️${RESET}${TEMPINFO_BG}${WHITE} Cool: ≤28°C (≤82°F) - Light Blue (1 block)${RESET}${TEMPINFO_BG}${INFO1_PADDING} ${RESET}${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}"
INFO2="\033[38;5;46m🌡️${RESET} Optimal: 29°C to 45°C (84°F to 113°F) - Green to Yellow"
INFO2_VISIBLE="🌡️ Optimal: 29°C to 45°C (84°F to 113°F) - Green to Yellow"
INFO2_LEN=${#INFO2_VISIBLE}
INFO2_PAD=$((TERM_WIDTH - INFO2_LEN - 3))
if [ $INFO2_PAD -lt 0 ]; then INFO2_PAD=0; fi
INFO2_PADDING=$(printf ' %.0s' $(seq 1 $INFO2_PAD))
echo -e "${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}${TEMPINFO_BG} \033[38;5;46m🌡️${RESET}${TEMPINFO_BG}${WHITE} Optimal: 29°C to 45°C (84°F to 113°F) - Green to Yellow${RESET}${TEMPINFO_BG}${INFO2_PADDING} ${RESET}${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}"
INFO3="\033[38;5;208m🌡️${RESET} Warm: 46°C to 55°C (115°F to 131°F) - Orange to Brown/Red"
INFO3_VISIBLE="🌡️ Warm: 46°C to 55°C (115°F to 131°F) - Orange to Brown/Red"
INFO3_LEN=${#INFO3_VISIBLE}
INFO3_PAD=$((TERM_WIDTH - INFO3_LEN - 3))
if [ $INFO3_PAD -lt 0 ]; then INFO3_PAD=0; fi
INFO3_PADDING=$(printf ' %.0s' $(seq 1 $INFO3_PAD))
echo -e "${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}${TEMPINFO_BG} \033[38;5;208m🌡️${RESET}${TEMPINFO_BG}${WHITE} Warm: 46°C to 55°C (115°F to 131°F) - Orange to Brown/Red${RESET}${TEMPINFO_BG}${INFO3_PADDING} ${RESET}${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}"
INFO4="\033[38;5;196m🌡️${RESET} Critical: 56°C to 65°C (133°F to 149°F) - Red to Bright Magenta"
INFO4_VISIBLE="🌡️ Critical: 56°C to 65°C (133°F to 149°F) - Red to Bright Magenta"
INFO4_LEN=${#INFO4_VISIBLE}
INFO4_PAD=$((TERM_WIDTH - INFO4_LEN - 3))
if [ $INFO4_PAD -lt 0 ]; then INFO4_PAD=0; fi
INFO4_PADDING=$(printf ' %.0s' $(seq 1 $INFO4_PAD))
echo -e "${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}${TEMPINFO_BG} \033[38;5;196m🌡️${RESET}${TEMPINFO_BG}${WHITE} Critical: 56°C to 65°C (133°F to 149°F) - Red to Bright Magenta${RESET}${TEMPINFO_BG}${INFO4_PADDING} ${RESET}${PURPLE_BG}${BOLD}${LIGHTBLUE}║${RESET}"
echo -e "${PURPLE_BG}${BOLD}${LIGHTBLUE}╚${BORDER_LINE}╝${RESET}"
# Get hostname and IP address
HOSTNAME=$(hostname)
IP_ADDRESS=$(hostname -I | awk '{print $1}')
if [ -z "$IP_ADDRESS" ]; then
IP_ADDRESS="No IP"
fi
# Hostname section border (top)
echo -e "${YELLOW_BG}${BLACK}╔${BORDER_LINE}╗${RESET}"
# Hostname and IP bar with borders
HOST_INFO="🖥️ ${HOSTNAME} • 🖧 ${IP_ADDRESS}"
HOST_LEN=$((${#HOSTNAME} + ${#IP_ADDRESS} + 11)) # Account for symbols, spaces, and emojis
HOST_PAD=$((TERM_WIDTH - HOST_LEN)) # Account for borders and spaces
if [ $HOST_PAD -lt 0 ]; then
HOST_PAD=0
# Truncate hostname if too long
MAX_HOST_LEN=$((TERM_WIDTH - ${#IP_ADDRESS} - 14))
if [ $MAX_HOST_LEN -gt 0 ]; then
HOSTNAME="${HOSTNAME:0:$MAX_HOST_LEN}"
HOST_INFO="🖥️ ${HOSTNAME} • 🖧 ${IP_ADDRESS}"
HOST_PAD=1
fi
fi
HOST_PADDING=$(printf ' %.0s' $(seq 1 $HOST_PAD))
echo -e "${YELLOW_BG}${BLACK}║${RESET}${YELLOW_BG}${BLACK} ${HOST_INFO}${HOST_PADDING} ${RESET}${YELLOW_BG}${BLACK}║${RESET}"
# Drive monitoring section border (top with T-connection)
echo -e "${YELLOW_BG}${BLACK}╠${BORDER_LINE}╣${RESET}"
# T-border line
echo -e "${BOLD}${LIGHTBLUE}╠${BORDER_LINE}╣${RESET}"
# Calculate dynamic column widths based on terminal width and what's enabled
# Fixed/minimum column widths:
DRIVE_WIDTH=10 # "[ DRIVE ]"
# Calculate stats width based on TEMP_DISPLAY (only if shown)
if [ "$SHOW_STATS" = "true" ]; then
if [ "$TEMP_DISPLAY" = "celsius" ]; then
STATS_WIDTH=20 # For C-only display
elif [ "$TEMP_DISPLAY" = "fahrenheit" ]; then
STATS_WIDTH=22 # For F-only display
else
STATS_WIDTH=29 # For both C/F display
fi
else
STATS_WIDTH=0
fi
# Serial width (only if shown)
if [ "$SHOW_SERIAL" = "true" ]; then
SERIAL_WIDTH=26 # "[ SERIAL ]"
else
SERIAL_WIDTH=0
fi
# Model width (only if shown)
if [ "$SHOW_MODEL" = "true" ]; then
MODEL_WIDTH=25 # Minimum width for model column
else
MODEL_WIDTH=0
fi
# Calculate spacing based on which columns are shown
SPACING=0
[ "$SHOW_STATS" = "true" ] && SPACING=$((SPACING + 2))
[ "$SHOW_SERIAL" = "true" ] && SPACING=$((SPACING + 2))
[ "$SHOW_MODEL" = "true" ] && SPACING=$((SPACING + 1))
# Base spacing
BASE_SPACING=7 # Minimum spacing for drive and temp bar
TOTAL_SPACING=$((BASE_SPACING + SPACING))
# Calculate remaining space for TEMP_BAR (dynamic)
# Account for: borders (4) + drive (10) + stats + serial + model + spacing
FIXED_WIDTHS=$((4 + DRIVE_WIDTH + STATS_WIDTH + SERIAL_WIDTH + MODEL_WIDTH + TOTAL_SPACING))
TEMP_BAR_WIDTH=$((TERM_WIDTH - FIXED_WIDTHS))
# Ensure minimum width for temperature bar (brackets + at least a few blocks + temp text)
MIN_TEMP_BAR_WIDTH=30
if [ $TEMP_BAR_WIDTH -lt $MIN_TEMP_BAR_WIDTH ]; then
TEMP_BAR_WIDTH=$MIN_TEMP_BAR_WIDTH
# Reduce model width if needed
if [ "$SHOW_MODEL" = "true" ]; then
MODEL_WIDTH=$((TERM_WIDTH - 4 - DRIVE_WIDTH - TEMP_BAR_WIDTH - STATS_WIDTH - SERIAL_WIDTH - TOTAL_SPACING))
if [ $MODEL_WIDTH -lt 15 ]; then
MODEL_WIDTH=15
fi
fi
fi
# Header row values
HEADER_DRIVE="DRIVE"
HEADER_GRAPH="TEMPERATURE GRAPH"
HEADER_SERIAL="SERIAL"
HEADER_MODEL="MODEL"
# Set stats header based on temperature display format
if [ "$TEMP_DISPLAY" = "celsius" ]; then
HEADER_STATS="${STATS_TIME_WINDOW}H MIN/MAX (C)"
elif [ "$TEMP_DISPLAY" = "fahrenheit" ]; then
HEADER_STATS="${STATS_TIME_WINDOW}H MIN/MAX (F)"
else
HEADER_STATS="${STATS_TIME_WINDOW}H MIN/MAX (C/F)"
fi
# Calculate padding for "TEMPERATURE GRAPH" header
GRAPH_HEADER_LEN=${#HEADER_GRAPH}
GRAPH_PAD_LEFT=$(( (TEMP_BAR_WIDTH - GRAPH_HEADER_LEN) / 2 ))
GRAPH_PAD_RIGHT=$(( TEMP_BAR_WIDTH - GRAPH_HEADER_LEN - GRAPH_PAD_LEFT ))
# Ensure non-negative padding
if [ $GRAPH_PAD_LEFT -lt 0 ]; then GRAPH_PAD_LEFT=0; fi
if [ $GRAPH_PAD_RIGHT -lt 0 ]; then GRAPH_PAD_RIGHT=0; fi
GRAPH_LEFT_PAD=$(printf ' %.0s' $(seq 1 $GRAPH_PAD_LEFT))
GRAPH_RIGHT_PAD=$(printf ' %.0s' $(seq 1 $GRAPH_PAD_RIGHT))
# Build header row with conditional columns
HEADER_ROW="${LIGHTBLUE}║${RESET}${DARKBLUE_BG} ${LIGHTBLUE}[${RESET}${DARKBLUE_BG} ${BOLD}${CYAN}%-8s${RESET}${DARKBLUE_BG} ${LIGHTBLUE}]${RESET}${DARKBLUE_BG} %s${BOLD}${CYAN}%s${RESET}${DARKBLUE_BG}%s"
HEADER_VALUES=("$HEADER_DRIVE" "$GRAPH_LEFT_PAD" "$HEADER_GRAPH" "$GRAPH_RIGHT_PAD")
# Add stats column if enabled
if [ "$SHOW_STATS" = "true" ]; then
STATS_HEADER_LEN=${#HEADER_STATS}
STATS_PAD_NEEDED=$((STATS_WIDTH - STATS_HEADER_LEN - 4))
if [ $STATS_PAD_NEEDED -lt 0 ]; then STATS_PAD_NEEDED=0; fi
HEADER_ROW+=" ${LIGHTBLUE}[${RESET}${DARKBLUE_BG} ${BOLD}${CYAN}%s%-${STATS_PAD_NEEDED}s${RESET}${DARKBLUE_BG}${LIGHTBLUE}]${RESET}${DARKBLUE_BG}"
HEADER_VALUES+=("$HEADER_STATS" "")
fi
# Add serial column if enabled
if [ "$SHOW_SERIAL" = "true" ]; then
SERIAL_HEADER_LEN=${#HEADER_SERIAL}
SERIAL_PAD_NEEDED=$((SERIAL_WIDTH - SERIAL_HEADER_LEN - 4))
if [ $SERIAL_PAD_NEEDED -lt 0 ]; then SERIAL_PAD_NEEDED=0; fi
HEADER_ROW+=" ${LIGHTBLUE}[${RESET}${DARKBLUE_BG} ${BOLD}${CYAN}%s%-${SERIAL_PAD_NEEDED}s${RESET}${DARKBLUE_BG} ${LIGHTBLUE}]${RESET}${DARKBLUE_BG}"
HEADER_VALUES+=("$HEADER_SERIAL" "")
fi
# Add model column if enabled
if [ "$SHOW_MODEL" = "true" ]; then
MODEL_HEADER_LEN=${#HEADER_MODEL}
MODEL_PAD_NEEDED=$((MODEL_WIDTH - MODEL_HEADER_LEN - 2))
if [ $MODEL_PAD_NEEDED -lt 0 ]; then MODEL_PAD_NEEDED=0; fi
HEADER_ROW+=" ${LIGHTBLUE}[${RESET}${DARKBLUE_BG} ${BOLD}${CYAN}%s%-${MODEL_PAD_NEEDED}s ${RESET}${DARKBLUE_BG}${LIGHTBLUE}]${RESET}"
HEADER_VALUES+=("$HEADER_MODEL" "")
fi
HEADER_ROW+="${LIGHTBLUE}║${RESET}\n"
# Print header
printf "$HEADER_ROW" "${HEADER_VALUES[@]}"
# Separator line with dashes (with left border)
SEP_WIDTH=$((TERM_WIDTH - 4))
SEPARATOR=$(printf -- '-%.0s' $(seq 1 $SEP_WIDTH))
printf "${LIGHTBLUE}║${RESET} ${GRAY}${SEPARATOR}${RESET} ${LIGHTBLUE}║${RESET}\n"
# Get all block devices (excluding loop, ram, etc.)
drives=$(lsblk -ndo NAME,TYPE | grep disk | awk '{print $1}')
if [ -z "$drives" ]; then
echo -e "${LIGHTBLUE}║${RESET} ${WHITE}No drives found${RESET}"
# Drive monitoring section border (bottom)
echo -e "${BOLD}${LIGHTBLUE}╚${BORDER_LINE}╝${RESET}"
echo ""
sleep $REFRESH_INTERVAL
continue
fi
# Process each drive
for drive in $drives; do
device="/dev/$drive"
# Get drive model (only if needed)
if [ "$SHOW_MODEL" = "true" ]; then
model=$(smartctl -i "$device" 2>/dev/null | grep "Device Model:" | cut -d: -f2 | xargs)
if [ -z "$model" ]; then
model=$(smartctl -i "$device" 2>/dev/null | grep "Model Number:" | cut -d: -f2 | xargs)
fi
if [ -z "$model" ]; then
model=$(smartctl -i "$device" 2>/dev/null | grep "Product:" | cut -d: -f2 | xargs)
fi
if [ -z "$model" ]; then
model="Unknown Model"
fi
# Truncate model to fit in dynamic column width
model_display_width=$((MODEL_WIDTH - 4))
model_display="${model:0:$model_display_width}"
fi
# Get drive serial number (only if needed)
if [ "$SHOW_SERIAL" = "true" ]; then
serial=$(smartctl -i "$device" 2>/dev/null | grep "Serial Number:" | cut -d: -f2 | xargs)
if [ -z "$serial" ]; then
serial=$(smartctl -i "$device" 2>/dev/null | grep "Serial number:" | cut -d: -f2 | xargs)
fi
if [ -z "$serial" ]; then
serial="N/A"
fi
# Truncate serial to fit in column
serial_display_width=$((SERIAL_WIDTH - 4))
serial="${serial:0:$serial_display_width}"
fi
# Get temperature
temp=$(smartctl -A "$device" 2>/dev/null | grep -i "Temperature_Celsius" | awk '{print $10}')
if [ -z "$temp" ]; then
temp=$(smartctl -A "$device" 2>/dev/null | grep -i "Temperature" | head -n1 | awk '{print $10}')
fi
if [ -z "$temp" ]; then
temp=$(smartctl -a "$device" 2>/dev/null | grep -i "Current Drive Temperature:" | awk '{print $4}')
fi
# Get stats for this drive (only if needed)
if [ "$SHOW_STATS" = "true" ]; then
stats=$(get_stats "$drive")
min_temp=$(echo "$stats" | cut -d'|' -f1)
max_temp=$(echo "$stats" | cut -d'|' -f2)
# Format stats display with colors based on TEMP_DISPLAY setting
if [ "$min_temp" = "N/A" ]; then
if [ "$TEMP_DISPLAY" = "celsius" ]; then
stats_display="${GRAY}N/A${RESET}"
stats_pad=$((STATS_WIDTH - 7))
elif [ "$TEMP_DISPLAY" = "fahrenheit" ]; then
stats_display="${GRAY}N/A${RESET}"
stats_pad=$((STATS_WIDTH - 7))
else
stats_display="${GRAY}N/A${RESET}"
stats_pad=$((STATS_WIDTH - 7))
fi
else
min_color=$(get_color "$min_temp")
max_color=$(get_color "$max_temp")
min_temp_f=$(( (min_temp * 9 / 5) + 32 ))
max_temp_f=$(( (max_temp * 9 / 5) + 32 ))
if [ "$TEMP_DISPLAY" = "celsius" ]; then
stats_display="${min_color}${min_temp}°C${RESET} - ${max_color}${max_temp}°C${RESET}"
visible_len=$((${#min_temp} + ${#max_temp} + 5))
stats_pad=$((STATS_WIDTH - visible_len - 4))
elif [ "$TEMP_DISPLAY" = "fahrenheit" ]; then
min_temp_f_formatted=$(printf "%3d" "$min_temp_f")
max_temp_f_formatted=$(printf "%3d" "$max_temp_f")
stats_display="${min_color}${min_temp_f_formatted}°F${RESET} - ${max_color}${max_temp_f_formatted}°F${RESET}"
visible_len=$((3 + 3 + 7))
stats_pad=$((STATS_WIDTH - visible_len - 4))
else
min_temp_f_formatted=$(printf "%3d" "$min_temp_f")
max_temp_f_formatted=$(printf "%3d" "$max_temp_f")
stats_display="${min_color}${min_temp}°C${RESET}${BOLD}${WHITE}/${RESET}${min_color}${min_temp_f_formatted}°F${RESET} - ${max_color}${max_temp}°C${RESET}${BOLD}${WHITE}/${RESET}${max_color}${max_temp_f_formatted}°F${RESET}"
visible_len=$((${#min_temp} + 3 + ${#max_temp} + 3 + 13))
stats_pad=$((STATS_WIDTH - visible_len - 4))
fi
fi
if [ $stats_pad -lt 0 ]; then stats_pad=0; fi
fi
# Display drive information
if [ -n "$temp" ] && [ "$temp" -eq "$temp" ] 2>/dev/null; then
temp_f=$(( (temp * 9 / 5) + 32 ))
bar=$(create_bar_with_temp "$temp" "$temp_f" "$TEMP_BAR_WIDTH")
# Log the temperature
log_temperature "$drive" "$temp"
# Build the line with conditional columns
printf "${LIGHTBLUE}║${RESET} ${LIGHTBLUE}[${RESET} ${BOLD}${WHITE}%-8s${RESET} ${LIGHTBLUE}]${RESET} %s " "$drive" "$bar"
# Calculate remaining space for padding
# Base used: 2 (borders) + 2 (spaces) + 10 (drive) + TEMP_BAR_WIDTH (dynamic) + 4 (spacing after bar)
used_width=$((18 + TEMP_BAR_WIDTH))
# Add stats column if enabled
if [ "$SHOW_STATS" = "true" ]; then
printf "${LIGHTBLUE}[${RESET} "
echo -ne "$stats_display"
printf "%-${stats_pad}s${LIGHTBLUE}]${RESET}" ""
used_width=$((used_width + STATS_WIDTH + 1))
fi
# Add serial column if enabled
if [ "$SHOW_SERIAL" = "true" ]; then
printf " ${LIGHTBLUE}[${RESET} ${WHITE}%-${serial_display_width}s${RESET} ${LIGHTBLUE}]${RESET}" "$serial"
used_width=$((used_width + SERIAL_WIDTH + 2))
fi
# Add model column if enabled
if [ "$SHOW_MODEL" = "true" ]; then
printf " ${LIGHTBLUE}[${RESET} ${WHITE}%-${model_display_width}s ${RESET}${LIGHTBLUE}]${RESET}" "$model_display"
used_width=$((used_width + MODEL_WIDTH + 1))
fi
# Calculate and add padding to reach the right border
remaining_space=$((TERM_WIDTH - used_width - 1))
if [ $remaining_space -lt 0 ]; then remaining_space=0; fi
printf "%-${remaining_space}s${LIGHTBLUE}║${RESET}\n" ""
else
# Skip this drive if HIDE_NA_DRIVES is enabled
if [ "$HIDE_NA_DRIVES" = "true" ]; then
continue
fi
# Calculate how much space for blocks in the N/A bar
temp_text_width=0
if [ "$TEMP_DISPLAY" = "celsius" ]; then
temp_text_width=7
elif [ "$TEMP_DISPLAY" = "fahrenheit" ]; then
temp_text_width=8
else
temp_text_width=13
fi
na_blocks_area=$((TEMP_BAR_WIDTH - temp_text_width - 2))
if [ $na_blocks_area -lt 10 ]; then
na_blocks_area=10
fi
# Create a "No temperature data" bar matching the dynamic width
no_temp_bar="「"
for ((i=0; i<na_blocks_area; i++)); do
no_temp_bar+="\033[48;5;0m\033[38;5;240m■\033[0m"
done
no_temp_bar+="\033[0m」 ${GRAY}N/A${RESET} "
printf "${LIGHTBLUE}║${RESET} ${LIGHTBLUE}[${RESET} ${BOLD}${WHITE}%-8s${RESET} ${LIGHTBLUE}]${RESET} " "$drive"
echo -ne "$no_temp_bar"
# Calculate remaining space for padding
# Base used: 2 (borders) + 2 (spaces) + 10 (drive) + TEMP_BAR_WIDTH (dynamic) + 4 (spacing after bar)
used_width=$((18 + TEMP_BAR_WIDTH))
# Add stats column if enabled
if [ "$SHOW_STATS" = "true" ]; then
printf " ${LIGHTBLUE}[${RESET} ${GRAY}%-3s${RESET}%-${stats_pad}s${LIGHTBLUE}]${RESET}" "N/A" ""
used_width=$((used_width + STATS_WIDTH + 2))
fi
# Add serial column if enabled
if [ "$SHOW_SERIAL" = "true" ]; then
printf " ${LIGHTBLUE}[${RESET} ${WHITE}%-${serial_display_width}s${RESET} ${LIGHTBLUE}]${RESET}" "$serial"
used_width=$((used_width + SERIAL_WIDTH + 2))
fi
# Add model column if enabled
if [ "$SHOW_MODEL" = "true" ]; then
printf " ${LIGHTBLUE}[${RESET} ${WHITE}%-${model_display_width}s ${RESET}${LIGHTBLUE}]${RESET}" "$model_display"
used_width=$((used_width + MODEL_WIDTH + 1))
fi
# Calculate and add padding to reach the right border
remaining_space=$((TERM_WIDTH - used_width - 1))
if [ $remaining_space -lt 0 ]; then remaining_space=0; fi
printf "%-${remaining_space}s${LIGHTBLUE}║${RESET}\n" ""
fi
done
# Drive monitoring section border (bottom)
echo -e "${BOLD}${LIGHTBLUE}╚${BORDER_LINE}╝${RESET}"
echo ""
# Wait before next refresh
sleep $REFRESH_INTERVAL
done