refactor script install packages

This commit is contained in:
Meliox 2026-06-07 03:18:27 +02:00
parent 823af45d38
commit d98916a2a5

View File

@ -90,34 +90,51 @@ function check_root_privileges() {
info "Root privileges verified."
}
# Define a function to install packages
function install_packages {
# Check if the 'sensors' command is available on the system
if (! command -v sensors &>/dev/null); then
# If the 'sensors' command is not available, prompt the user to install lm-sensors
local choiceInstallLmSensors=$(ask "lm-sensors is not installed. Would you like to install it? (y/n)")
case "$choiceInstallLmSensors" in
[yY])
# If the user chooses to install lm-sensors, update the package list and install the package
apt-get update
apt-get install lm-sensors
;;
[nN])
# If the user chooses not to install lm-sensors, exit the script with a zero status code
msgb "Decided to not install lm-sensors. The mod cannot run without it. Exiting..."
err "lm-sensors is required. Exiting..."
;;
*)
# If the user enters an invalid input, print an error message and exit the script with a non-zero status code
err "Invalid input. Exiting..."
;;
esac
# Check if a tool is installed; if not, offer to install it via apt.
# Usage: _check_or_install_tool <cmd> <pkg> <description>
# Returns 0 if the tool is available (or successfully installed), 1 otherwise.
function _check_or_install_tool {
local cmd="$1"
local pkg="$2"
local description="$3"
if command -v "$cmd" &>/dev/null; then
info "$description is installed."
return 0
fi
# Check if lm-sensors is installed correctly and exit if not
if (! command -v sensors &>/dev/null); then
err "lm-sensors installation failed or 'sensors' command is not available. Please install lm-sensors manually and re-run the script."
local choice
choice=$(ask "$description is not installed. Would you like to install it? (y/N)")
case "$choice" in
[yY])
apt-get update
apt-get install -y "$pkg"
if command -v "$cmd" &>/dev/null; then
info "$description installed successfully."
return 0
else
warn "$description installation failed. The section will be skipped."
return 1
fi
;;
*)
info "Skipping $description installation."
return 1
;;
esac
}
# Check if nvidia-smi is available. NVIDIA GPU monitoring requires the NVIDIA drivers
# to be installed manually; this function does not attempt installation via apt.
# Returns 0 if nvidia-smi is available, 1 otherwise.
function _check_nvidia_tool {
if command -v nvidia-smi &>/dev/null; then
info "nvidia-smi is installed."
return 0
fi
warn "nvidia-smi not found. NVIDIA GPU monitoring requires the NVIDIA drivers to be installed."
info "Refer to your system or Proxmox documentation for NVIDIA driver installation."
return 1
}
# Main configuration function to detect sensors and set up parameters
@ -129,8 +146,10 @@ function configure {
local modelName
local upsConnection
install_packages
local lm_sensors_ok=false
_check_or_install_tool sensors lm-sensors "lm-sensors" && lm_sensors_ok=true
if [[ "$lm_sensors_ok" == true ]]; then
#### Collect sensor data ####
#region sensors collection
if [ "$DEBUG_REMOTE" = true ]; then
@ -200,116 +219,6 @@ function configure {
fi
#endregion cpu setup
#### Graphics ####
#region Graphics setup
msgb "\n=== Detecting Graphics information ==="
#region intel GPU setup
# Check for Intel GPU - ensure intel_gpu_top is installed
if command -v intel_gpu_top &>/dev/null; then
# detect all intel cards using intel_gpu_top -L. Show them line by line
local intelCards
# Get the output from intel_gpu_top -L, skip empty lines
intelCards=$(intel_gpu_top -L 2>/dev/null | grep -E '^card[0-9]+' || true)
if [[ -n "$intelCards" ]]; then
local cardCount=$(echo "$intelCards" | wc -l)
echo "Intel GPU(s) detected ($cardCount):"
echo "$intelCards" | while IFS= read -r line; do
# Extract card name, GPU model, and PCI info
if [[ $line =~ ^(card[0-9]+)[[:space:]]+(.+)[[:space:]]+pci:(.+)$ ]]; then
local cardName="${BASH_REMATCH[1]}"
local gpuModel="${BASH_REMATCH[2]}"
local pciInfo="${BASH_REMATCH[3]}"
echo " - Card: $cardName"
echo " Model: $gpuModel"
echo " PCI: $pciInfo"
else
# Fallback: just show the line as-is
echo " $line"
fi
done
ENABLE_INTEL_GPU_INFO=true
ENABLE_GPU_INFO=true
else
warn "No Intel GPUs detected by intel_gpu_top."
ENABLE_INTEL_GPU_INFO=false
fi
else
warn "intel_gpu_top command not found. Skipping Intel GPU information detection."
ENABLE_INTEL_GPU_INFO=false
fi
#endregion intel GPU setup
#region NVIDIA GPU setup
# Check for NVIDIA GPU - ensure nvidia-smi is installed
if command -v nvidia-smi &>/dev/null; then
# detect all NVIDIA cards using nvidia-smi -L
local nvidiaCards
# Get the output from nvidia-smi -L (lists GPUs)
nvidiaCards=$(nvidia-smi -L 2>/dev/null || true)
if [[ -n "$nvidiaCards" ]]; then
local cardCount=$(echo "$nvidiaCards" | wc -l)
echo "NVIDIA GPU(s) detected ($cardCount):"
echo "$nvidiaCards" | while IFS= read -r line; do
# Expected format: GPU 0: NVIDIA GeForce RTX 3080 (UUID: GPU-xxxxx)
if [[ $line =~ ^GPU\ ([0-9]+):\ (.+)\ \(UUID:\ (.+)\)$ ]]; then
local gpuIndex="${BASH_REMATCH[1]}"
local gpuModel="${BASH_REMATCH[2]}"
local gpuUUID="${BASH_REMATCH[3]}"
echo " - GPU $gpuIndex"
echo " Model: $gpuModel"
echo " UUID: $gpuUUID"
else
# Fallback: just show the line as-is
echo " $line"
fi
done
ENABLE_NVIDIA_GPU_INFO=true
ENABLE_GPU_INFO=true
else
warn "No NVIDIA GPUs detected by nvidia-smi."
ENABLE_NVIDIA_GPU_INFO=false
fi
else
warn "nvidia-smi command not found. Skipping NVIDIA GPU information detection."
ENABLE_NVIDIA_GPU_INFO=false
fi
#endregion NVIDIA GPU setup
#region AMD GPU setup
# not implemented yet
#endregion AMD GPU setup
#endregion Graphics setup
#### GPU Historical Data ####
#region gpu history setup
ENABLE_GPU_HISTORY=false
if [[ "$ENABLE_GPU_INFO" == true ]]; then
msgb "\n=== GPU Historical Data ==="
local choiceGpuHistory
choiceGpuHistory=$(ask "Store historical GPU data for graphs? (y/N)")
case "$choiceGpuHistory" in
[yY])
ENABLE_GPU_HISTORY=true
info "Historical GPU data will be stored."
;;
[nN]|"")
info "Historical GPU data will not be stored."
;;
*)
warn "Invalid selection. Historical GPU data will not be stored."
;;
esac
fi
#endregion gpu history setup
#### RAM ####
#region ram setup
local ramList ramCount
@ -415,6 +324,119 @@ function configure {
fi
#endregion temp unit setup
fi #end lm-sensors block 1
#### Graphics ####
#region Graphics setup
msgb "\n=== Detecting Graphics information ==="
#region intel GPU setup
if _check_or_install_tool intel_gpu_top igt-gpu-tools "Intel GPU tools (igt-gpu-tools)"; then
# detect all intel cards using intel_gpu_top -L. Show them line by line
local intelCards
# Get the output from intel_gpu_top -L, skip empty lines
intelCards=$(intel_gpu_top -L 2>/dev/null | grep -E '^card[0-9]+' || true)
if [[ -n "$intelCards" ]]; then
local cardCount=$(echo "$intelCards" | wc -l)
echo "Intel GPU(s) detected ($cardCount):"
echo "$intelCards" | while IFS= read -r line; do
# Extract card name, GPU model, and PCI info
if [[ $line =~ ^(card[0-9]+)[[:space:]]+(.+)[[:space:]]+pci:(.+)$ ]]; then
local cardName="${BASH_REMATCH[1]}"
local gpuModel="${BASH_REMATCH[2]}"
local pciInfo="${BASH_REMATCH[3]}"
echo " - Card: $cardName"
echo " Model: $gpuModel"
echo " PCI: $pciInfo"
else
# Fallback: just show the line as-is
echo " $line"
fi
done
ENABLE_INTEL_GPU_INFO=true
ENABLE_GPU_INFO=true
else
warn "No Intel GPUs detected by intel_gpu_top."
ENABLE_INTEL_GPU_INFO=false
fi
else
ENABLE_INTEL_GPU_INFO=false
fi
#endregion intel GPU setup
#region NVIDIA GPU setup
if _check_nvidia_tool; then
# detect all NVIDIA cards using nvidia-smi -L
local nvidiaCards
# Get the output from nvidia-smi -L (lists GPUs)
nvidiaCards=$(nvidia-smi -L 2>/dev/null || true)
if [[ -n "$nvidiaCards" ]]; then
local cardCount=$(echo "$nvidiaCards" | wc -l)
echo "NVIDIA GPU(s) detected ($cardCount):"
echo "$nvidiaCards" | while IFS= read -r line; do
# Expected format: GPU 0: NVIDIA GeForce RTX 3080 (UUID: GPU-xxxxx)
if [[ $line =~ ^GPU\ ([0-9]+):\ (.+)\ \(UUID:\ (.+)\)$ ]]; then
local gpuIndex="${BASH_REMATCH[1]}"
local gpuModel="${BASH_REMATCH[2]}"
local gpuUUID="${BASH_REMATCH[3]}"
echo " - GPU $gpuIndex"
echo " Model: $gpuModel"
echo " UUID: $gpuUUID"
else
# Fallback: just show the line as-is
echo " $line"
fi
done
ENABLE_NVIDIA_GPU_INFO=true
ENABLE_GPU_INFO=true
else
warn "No NVIDIA GPUs detected by nvidia-smi."
ENABLE_NVIDIA_GPU_INFO=false
fi
else
ENABLE_NVIDIA_GPU_INFO=false
fi
#endregion NVIDIA GPU setup
#region AMD GPU setup
if _check_or_install_tool radeontop radeontop "AMD GPU tools (radeontop)"; then
# AMD GPU detection is not yet implemented
ENABLE_AMD_GPU_INFO=false
else
ENABLE_AMD_GPU_INFO=false
fi
#endregion AMD GPU setup
#endregion Graphics setup
#### GPU Historical Data ####
#region gpu history setup
ENABLE_GPU_HISTORY=false
if [[ "$ENABLE_GPU_INFO" == true ]]; then
msgb "\n=== GPU Historical Data ==="
local choiceGpuHistory
choiceGpuHistory=$(ask "Store historical GPU data for graphs? (y/N)")
case "$choiceGpuHistory" in
[yY])
ENABLE_GPU_HISTORY=true
info "Historical GPU data will be stored."
;;
[nN]|"")
info "Historical GPU data will not be stored."
;;
*)
warn "Invalid selection. Historical GPU data will not be stored."
;;
esac
fi
#endregion gpu history setup
#### UPS ####
#region ups setup
local choiceUPS=$(ask "Enable UPS information? (y/N)")
@ -485,8 +507,8 @@ function configure {
#### Final Check ####
#region final check
if [ "$SENSORS_DETECTED" = false ] && [ "$ENABLE_UPS" = false ] && [ "$ENABLE_SYSTEM_INFO" = false ]; then
err "No sensors detected, UPS or system info enabled. Exiting."
if [ "$SENSORS_DETECTED" = false ] && [ "$ENABLE_GPU_INFO" != true ] && [ "$ENABLE_UPS" = false ] && [ "$ENABLE_SYSTEM_INFO" = false ]; then
err "No sensors detected, no GPU info, no UPS and no system info enabled. Exiting."
fi
#endregion final check
}