mirror of
https://github.com/AlexMacocian/ServerManagementUtils.git
synced 2026-07-15 15:19:58 +00:00
Setup logrotate
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
# Logrotate configuration for server monitoring scripts
|
||||
# This file should be placed in /etc/logrotate.d/
|
||||
|
||||
/var/log/disk_usage.log /var/log/power_usage.log /var/log/temperature.log {
|
||||
daily
|
||||
rotate 10
|
||||
compress
|
||||
delaycompress
|
||||
notifempty
|
||||
create 644 root root
|
||||
dateext
|
||||
nomail
|
||||
missingok
|
||||
copytruncate
|
||||
endscript
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
#!/bin/bash
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
. "${SCRIPT_DIR}/../share/logging.sh"
|
||||
|
||||
LOGROTATE_DIR="${SCRIPT_DIR}/../logrotate"
|
||||
LOGROTATE_INSTALL_DIR="/etc/logrotate.d"
|
||||
|
||||
# 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() {
|
||||
if ! command -v logrotate &> /dev/null; then
|
||||
LogError "logrotate is not installed. Please install it first:"
|
||||
LogError " Ubuntu/Debian: sudo apt-get install logrotate"
|
||||
LogError " CentOS/RHEL: sudo yum install logrotate"
|
||||
exit 1
|
||||
fi
|
||||
LogInformation "logrotate is installed"
|
||||
}
|
||||
|
||||
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 logrotate configurations..."
|
||||
|
||||
local test_passed=0
|
||||
local test_failed=0
|
||||
|
||||
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 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
|
||||
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() {
|
||||
LogInformation "Logrotate configurations:"
|
||||
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:"
|
||||
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"
|
||||
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 "$@"
|
||||
Reference in New Issue
Block a user