#!/usr/bin/env bash # Setup Log Rotation # Configures logrotate for monitoring logs set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; } log_error() { echo -e "${RED}[✗]${NC} $1"; } create_logrotate_config() { log_info "Creating logrotate configuration..." local logrotate_file="/etc/logrotate.d/blockchain-monitoring" local log_dir="${PROJECT_ROOT}/logs/monitoring" # Create log directory if it doesn't exist mkdir -p "$log_dir" # Create logrotate config cat > "$logrotate_file" </dev/null || true endscript } # Validator health logs (on each validator) /var/log/validator-health.log { daily rotate 14 compress delaycompress missingok notifempty create 0644 root root } EOF log_success "Logrotate configuration created: $logrotate_file" # Test configuration if command -v logrotate &> /dev/null; then log_info "Testing logrotate configuration..." if sudo logrotate -d "$logrotate_file" &>/dev/null; then log_success "Logrotate configuration is valid" else log_warn "Logrotate configuration may have issues (run with sudo)" fi else log_warn "logrotate command not found - install logrotate package" fi } create_manual_rotation_script() { log_info "Creating manual log rotation script..." cat > "$SCRIPT_DIR/rotate-logs.sh" <<'ROTATESCRIPT' #!/usr/bin/env bash # Manual Log Rotation Script # Rotates monitoring logs manually set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" LOG_DIR="${PROJECT_ROOT}/logs/monitoring" # Colors GREEN='\033[0;32m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } rotate_log() { local log_file="$1" if [ ! -f "$log_file" ]; then return 0 fi local size=$(stat -f%z "$log_file" 2>/dev/null || stat -c%s "$log_file" 2>/dev/null || echo "0") local max_size=$((100 * 1024 * 1024)) # 100MB if [ "$size" -gt "$max_size" ]; then local timestamp=$(date +%Y%m%d_%H%M%S) local rotated_file="${log_file}.${timestamp}" mv "$log_file" "$rotated_file" log_info "Rotated $log_file -> $rotated_file" # Compress old logs older than 7 days find "$LOG_DIR" -name "*.${timestamp%.*}" -mtime +7 -exec gzip {} \; 2>/dev/null || true # Remove logs older than 30 days find "$LOG_DIR" -name "*.log.*" -mtime +30 -delete 2>/dev/null || true return 0 fi return 1 } main() { log_info "Rotating monitoring logs..." mkdir -p "$LOG_DIR" local rotated=0 for log_file in "$LOG_DIR"/*.log; do if [ -f "$log_file" ]; then if rotate_log "$log_file"; then ((rotated++)) fi fi done if [ "$rotated" -gt 0 ]; then log_success "Rotated $rotated log file(s)" else log_info "No logs needed rotation" fi } main "$@" ROTATESCRIPT chmod +x "$SCRIPT_DIR/rotate-logs.sh" log_success "rotate-logs.sh script created" } setup_cron_rotation() { log_info "Setting up automatic log rotation..." # Add to crontab if not already present local cron_job="0 0 * * * ${SCRIPT_DIR}/rotate-logs.sh >> ${PROJECT_ROOT}/logs/monitoring/rotation.log 2>&1" if crontab -l 2>/dev/null | grep -q "rotate-logs.sh"; then log_warn "Log rotation cron job already exists" else log_info "Adding log rotation cron job..." (crontab -l 2>/dev/null; echo "$cron_job") | crontab - log_success "Log rotation cron job added (daily at midnight)" fi } main() { log_info "Setting up log rotation..." echo "" create_logrotate_config echo "" create_manual_rotation_script echo "" setup_cron_rotation echo "" log_success "Log rotation setup complete!" log_info "Logs will be rotated:" log_info " - Automatically via logrotate (daily, keep 30 days)" log_info " - Manually via rotate-logs.sh (when logs > 100MB)" log_info " - Via cron (daily at midnight)" } main "$@"