scan

#!/bin/ksh
#
# fs_clone Log Scanner and Analyzer for Solaris
# Purpose: Automate scanning and analysis of fs_clone operation logs
#

# Configuration
LOG_DIR="/var/adm/fs_clone"
REPORT_DIR="/var/adm/reports/fs_clone"
ARCHIVE_DIR="/var/adm/archive/fs_clone"
ALERT_EMAIL="admin@example.com"
RETENTION_DAYS=90

# Create directories if they don't exist
mkdir -p "$REPORT_DIR" "$ARCHIVE_DIR"

# Timestamp for report
TIMESTAMP=$(date '+%Y%m%d_%H%M%S')
REPORT_FILE="$REPORT_DIR/fs_clone_analysis_$TIMESTAMP.txt"

# Initialize report
exec > >(tee -a "$REPORT_FILE")
exec 2>&1

echo "=========================================="
echo "fs_clone Log Analysis Report"
echo "Generated: $(date '+%Y-%m-%d %H:%M:%S')"
echo "=========================================="
echo

# Function to analyze log file
analyze_log() {
    local logfile="$1"
    echo "Analyzing: $logfile"
    echo "----------------------------------------"
    
    # Check if log file exists and is readable
    if [[ ! -r "$logfile" ]]; then
        echo "WARNING: Cannot read $logfile"
        return 1
    fi
    
    # Extract key metrics
    echo "Summary Statistics:"
    echo " Total lines: $(wc -l < "$logfile")"
    
    # Count success/failure patterns
    local success_count=$(grep -c -i "success\|completed\|finished" "$logfile" 2>/dev/null || echo 0)
    local error_count=$(grep -c -i "error\|failed\|failure" "$logfile" 2>/dev/null || echo 0)
    local warning_count=$(grep -c -i "warning\|warn" "$logfile" 2>/dev/null || echo 0)
    
    echo " Successful operations: $success_count"
    echo " Errors: $error_count"
    echo " Warnings: $warning_count"
    
    # Extract error messages
    if [[ $error_count -gt 0 ]]; then
        echo
        echo "Error Messages Found:"
        grep -i "error\|failed\|failure" "$logfile" | head -20
    fi
    
    # Extract warning messages
    if [[ $warning_count -gt 0 ]]; then
        echo
        echo "Warning Messages Found:"
        grep -i "warning\|warn" "$logfile" | head -20
    fi
    
    # Check for specific fs_clone issues
    echo
    echo "Specific Issues Detected:"
    
    # Disk space issues
    if grep -q -i "no space\|disk full\|insufficient space" "$logfile"; then
        echo " [CRITICAL] Disk space issues detected"
    fi
    
    # Permission issues
    if grep -q -i "permission denied\|not permitted" "$logfile"; then
        echo " [ERROR] Permission issues detected"
    fi
    
    # Timeout issues
    if grep -q -i "timeout\|timed out" "$logfile"; then
        echo " [ERROR] Timeout issues detected"
    fi
    
    # Corruption issues
    if grep -q -i "corrupt\|damaged\|inconsistent" "$logfile"; then
        echo " [CRITICAL] Filesystem corruption detected"
    fi
    
    echo
    echo "----------------------------------------"
    echo
}

# Function to check log age and archive old logs
archive_old_logs() {
    echo "Checking for old logs to archive..."
    find "$LOG_DIR" -name "*.log" -type f -mtime +$RETENTION_DAYS -exec sh -c '
        for file; do
            echo "Archiving: $file"
            gzip -c "$file" > "'"$ARCHIVE_DIR"'/$(basename "$file").$(date +%Y%m%d).gz"
            rm "$file"
        done
    ' sh {} +
    echo
}

# Function to generate summary statistics
generate_summary() {
    echo "=========================================="
    echo "Overall Summary"
    echo "=========================================="
    
    total_logs=$(find "$LOG_DIR" -name "*.log" -type f | wc -l)
    echo "Total log files scanned: $total_logs"
    
    # Calculate total size
    total_size=$(du -sh "$LOG_DIR" 2>/dev/null | awk '{print $1}')
    echo "Total log directory size: $total_size"
    
    # Recent activity (last 24 hours)
    recent_logs=$(find "$LOG_DIR" -name "*.log" -type f -mtime -1 | wc -l)
    echo "Log files modified in last 24h: $recent_logs"
    
    echo
}

# Function to send alert if critical issues found
send_alert() {
    local critical_count="$1"
    
    if [[ $critical_count -gt 0 ]]; then
        echo "ALERT: $critical_count critical issues found!" | \
            mailx -s "fs_clone Critical Issues Detected - $(hostname)" "$ALERT_EMAIL"
    fi
}

# Main execution
echo "Starting fs_clone log scan..."
echo

# Check if log directory exists
if [[ ! -d "$LOG_DIR" ]]; then
    echo "ERROR: Log directory $LOG_DIR does not exist"
    exit 1
fi

# Counter for critical issues
CRITICAL_COUNT=0

# Scan all log files
for logfile in "$LOG_DIR"/*.log; do
    if [[ -f "$logfile" ]]; then
        analyze_log "$logfile"
        
        # Check for critical issues
        if grep -q -i "critical\|corrupt\|no space" "$logfile"; then
            ((CRITICAL_COUNT++))
        fi
    fi
done

# Generate summary
generate_summary

# Archive old logs
archive_old_logs

# Send alert if needed
send_alert "$CRITICAL_COUNT"

echo "=========================================="
echo "Analysis complete. Report saved to:"
echo "$REPORT_FILE"
echo "=========================================="

# Exit with appropriate code
if [[ $CRITICAL_COUNT -gt 0 ]]; then
    exit 2
else
    exit 0
fi

Comments

Popular posts from this blog

Automating CPU Load Monitoring for Oracle EBS on Solaris

From Manual to Masterful: 25 Essential Prompts for Leading a Winning Oracle DBA Automation Project