Cache disk list to avoid multiple lsblk calls

Add get_disk_list() function that caches the output of lsblk
on first call. Subsequent calls return the cached value,
reducing overhead when multiple functions need to iterate
over disk devices.

#8

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-05 10:51:13 -05:00
parent 7514e2ba7c
commit a491ae4592

View File

@@ -13,6 +13,12 @@ readonly CMD_TIMEOUT=30 # Default timeout in seconds for external commands
# Optional log file - set via environment variable PROXDOC_LOGFILE # Optional log file - set via environment variable PROXDOC_LOGFILE
LOGFILE="${PROXDOC_LOGFILE:-}" LOGFILE="${PROXDOC_LOGFILE:-}"
###################
# Cached Data
###################
# Disk list cache - populated on first use
DISK_LIST=""
################### ###################
# Pattern Constants # Pattern Constants
################### ###################
@@ -114,6 +120,14 @@ checkIfOnHypervisor() {
command -v pveversion >/dev/null 2>&1 command -v pveversion >/dev/null 2>&1
} }
# Get disk list with caching to avoid multiple lsblk calls
get_disk_list() {
if [[ -z "$DISK_LIST" ]]; then
DISK_LIST=$(lsblk -d -o name 2>/dev/null | grep -E "$DISK_DEVICE_PATTERN")
fi
echo "$DISK_LIST"
}
################### ###################
# System Information Functions # System Information Functions
################### ###################
@@ -155,7 +169,7 @@ get_disk_health() {
if ! timeout $CMD_TIMEOUT smartctl -H "/dev/$disk"; then if ! timeout $CMD_TIMEOUT smartctl -H "/dev/$disk"; then
log_message warn "smartctl timed out or failed for /dev/$disk" log_message warn "smartctl timed out or failed for /dev/$disk"
fi fi
done < <(lsblk -d -o name | grep -E "$DISK_DEVICE_PATTERN") done <<< "$(get_disk_list)"
else else
log_message warn "smartctl not found. Install smartmontools for disk health monitoring" log_message warn "smartctl not found. Install smartmontools for disk health monitoring"
fi fi
@@ -588,7 +602,7 @@ quick_health_check() {
else else
echo -e "/dev/$disk: ${YELLOW}check timed out or unavailable${NC}" echo -e "/dev/$disk: ${YELLOW}check timed out or unavailable${NC}"
fi fi
done < <(lsblk -d -o name | grep -E "$DISK_DEVICE_PATTERN") done <<< "$(get_disk_list)"
fi fi
# Node Exporter # Node Exporter