BASH
Analyze Web Server Logs for Errors
Automate analysis of web server logs to quickly identify and count common error types like 404s and 500s, streamlining debugging and monitoring.
#!/bin/bash
# Path to your Nginx/Apache access log file
LOG_FILE="/var/log/nginx/access.log"
# Check if log file exists
if [ ! -f "$LOG_FILE" ]; then
echo "Error: Log file not found at $LOG_FILE"
exit 1
fi
echo "Analyzing log file: $LOG_FILE"
# Count 500 Internal Server Errors
ERROR_500_COUNT=$(grep -c ' " 500 ' "$LOG_FILE")
echo "Total 500 Internal Server Errors: $ERROR_500_COUNT"
# Count 404 Not Found Errors
ERROR_404_COUNT=$(grep -c ' " 404 ' "$LOG_FILE")
echo "Total 404 Not Found Errors: $ERROR_404_COUNT"
# List top 10 most frequent 404 URLs
echo -e "
Top 10 most frequent 404 Not Found URLs:"
grep ' " 404 ' "$LOG_FILE" | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10
# Find requests from a specific IP address (example: 192.168.1.1)
# IP_TO_FIND="192.168.1.1"
# echo -e "
Requests from IP $IP_TO_FIND:"
# grep "^$IP_TO_FIND" "$LOG_FILE"
How it works: This bash script helps web developers quickly analyze web server access logs (e.g., Nginx, Apache) for common issues. It uses 'grep' to count occurrences of specific HTTP status codes like 500 (Internal Server Error) and 404 (Not Found). Additionally, it demonstrates how to extract and rank the most frequently requested 404 URLs using 'awk', 'sort', and 'uniq', providing insights into broken links or missing assets.