Files
2025-10-30 16:16:50 +01:00

242 lines
7.6 KiB
Bash
Executable File

#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/../share/logging.sh"
. "${SCRIPT_DIR}/../share/os-detection.sh"
LOGROTATE_DIR="${SCRIPT_DIR}/../logrotate"
LOGROTATE_INSTALL_DIR=$(GetLogRotateConfigDir)
OS_TYPE=$(DetectOS)
# List of logrotate config files to manage (without .conf extension)
LOGROTATE_CONFIGS=(
"server-metrics"
)
CheckRoot() {
if [[ $EUID -ne 0 ]]; then
LogError "This script must be run as root (use sudo)"
exit 1
fi
}
CheckLogrotateInstalled() {
local log_cmd=$(GetLogRotateCommand)
if ! IsLogRotateAvailable; then
LogError "$log_cmd is not installed. Please install it first:"
if [[ "$OS_TYPE" == "macos" ]]; then
LogError " macOS uses newsyslog (built-in) but may need configuration at $LOGROTATE_INSTALL_DIR"
LogError " If directory doesn't exist: sudo mkdir -p $LOGROTATE_INSTALL_DIR"
else
LogError " Ubuntu/Debian: sudo apt-get install logrotate"
LogError " CentOS/RHEL: sudo yum install logrotate"
fi
exit 1
fi
LogInformation "$log_cmd is available"
# Ensure the config directory exists (especially for macOS newsyslog.d)
if [[ ! -d "$LOGROTATE_INSTALL_DIR" ]]; then
LogInformation "Creating config directory: $LOGROTATE_INSTALL_DIR"
mkdir -p "$LOGROTATE_INSTALL_DIR"
chmod 755 "$LOGROTATE_INSTALL_DIR"
fi
}
InstallLogrotateConfigs() {
local installed_count=0
local failed_count=0
for config_name in "${LOGROTATE_CONFIGS[@]}"; do
local source_file="${LOGROTATE_DIR}/${config_name}.conf"
local target_file="${LOGROTATE_INSTALL_DIR}/${config_name}"
if [[ ! -f "$source_file" ]]; then
LogWarning "Config file not found: $source_file, skipping..."
((failed_count++))
continue
fi
LogInformation "Installing $config_name configuration..."
if cp "$source_file" "$target_file" && chmod 644 "$target_file"; then
LogInformation "Installed: $target_file"
((installed_count++))
else
LogError "Failed to install: $config_name"
((failed_count++))
fi
done
LogInformation "Installation summary: $installed_count installed, $failed_count failed"
if [[ $failed_count -gt 0 ]]; then
exit 1
fi
}
TestLogrotateConfigs() {
LogInformation "Testing log rotation configurations..."
local test_passed=0
local test_failed=0
local log_cmd=$(GetLogRotateCommand)
for config_name in "${LOGROTATE_CONFIGS[@]}"; do
local config_file="${LOGROTATE_INSTALL_DIR}/${config_name}"
if [[ ! -f "$config_file" ]]; then
LogWarning "Config file not installed: $config_file, skipping test..."
continue
fi
LogDebug "Testing configuration: $config_name"
if [[ "$OS_TYPE" == "macos" ]]; then
# newsyslog uses different syntax, just check file readability
if [[ -r "$config_file" ]]; then
LogDebug "Test passed: $config_name (file readable)"
((test_passed++))
else
LogError "Test failed: $config_name (file not readable)"
((test_failed++))
fi
else
if logrotate -d "$config_file" &> /dev/null; then
LogDebug "Test passed: $config_name"
((test_passed++))
else
LogError "Test failed: $config_name"
LogError "Run for details: logrotate -d $config_file"
((test_failed++))
fi
fi
done
if [[ $test_failed -gt 0 ]]; then
LogError "Configuration test failed for $test_failed configs"
exit 1
else
LogInformation "All $test_passed configuration tests passed"
fi
}
ShowStatus() {
local log_cmd=$(GetLogRotateCommand)
LogInformation "Log rotation configurations:"
LogInformation "OS: $OS_TYPE"
LogInformation "Tool: $log_cmd"
LogInformation "Install directory: $LOGROTATE_INSTALL_DIR"
LogInformation ""
local found_configs=0
for config_name in "${LOGROTATE_CONFIGS[@]}"; do
local config_file="${LOGROTATE_INSTALL_DIR}/${config_name}"
local source_file="${LOGROTATE_DIR}/${config_name}.conf"
if [[ -f "$config_file" ]]; then
LogInformation "$config_name - INSTALLED"
((found_configs++))
elif [[ -f "$source_file" ]]; then
LogInformation "$config_name - NOT INSTALLED (source available)"
else
LogInformation "$config_name - SOURCE MISSING"
fi
done
LogInformation ""
if [[ $found_configs -gt 0 ]]; then
LogInformation "Commands:"
if [[ "$OS_TYPE" == "macos" ]]; then
LogInformation " Manual run: sudo newsyslog -v"
LogInformation " Check config: cat /etc/newsyslog.conf"
LogInformation " Force rotation: sudo newsyslog -F"
else
LogInformation " Test rotation: sudo logrotate -f /etc/logrotate.d/<config-name>"
LogInformation " Check status: sudo cat /var/lib/logrotate/status"
LogInformation " Manual run: sudo logrotate /etc/logrotate.conf"
fi
else
LogWarning "No configurations are currently installed"
fi
}
RemoveLogrotateConfigs() {
local removed_count=0
for config_name in "${LOGROTATE_CONFIGS[@]}"; do
local config_file="${LOGROTATE_INSTALL_DIR}/${config_name}"
if [[ -f "$config_file" ]]; then
LogInformation "Removing $config_name configuration..."
rm -f "$config_file"
LogInformation "Removed: $config_file"
((removed_count++))
else
LogDebug "Config not found: $config_file"
fi
done
if [[ $removed_count -gt 0 ]]; then
LogInformation "Removed $removed_count logrotate configurations"
else
LogWarning "No configurations were found to remove"
fi
}
ShowUsage() {
echo "Usage: $0 [install|remove|test|status]"
echo ""
echo "Commands:"
echo " install - Install all logrotate configurations (default)"
echo " remove - Remove all logrotate configurations"
echo " test - Test all logrotate configurations"
echo " status - Show status of all configurations"
echo ""
echo "This script manages logrotate configs from: $LOGROTATE_DIR/"
echo "Configurations defined: ${LOGROTATE_CONFIGS[*]}"
echo ""
echo "Examples:"
echo " sudo $0 install"
echo " sudo $0 test"
}
Main() {
local action="${1:-install}"
case "$action" in
install)
LogInformation "Installing logrotate configurations"
CheckRoot
CheckLogrotateInstalled
InstallLogrotateConfigs
TestLogrotateConfigs
LogInformation "Logrotate setup completed successfully!"
;;
remove)
LogInformation "Removing logrotate configurations"
CheckRoot
RemoveLogrotateConfigs
;;
test)
LogInformation "Testing logrotate configurations"
CheckRoot
CheckLogrotateInstalled
TestLogrotateConfigs
;;
status)
ShowStatus
;;
help|--help|-h)
ShowUsage
;;
*)
LogError "Unknown action: $action"
ShowUsage
exit 1
;;
esac
}
Main "$@"