BASH

Extracting and Analyzing Web Server Logs

Learn to process web server access logs using `grep` and `awk` to extract common errors, unique IP addresses, and request statistics for debugging.

#!/bin/bash
LOG_FILE="/var/log/nginx/access.log" # Or /var/log/apache2/access.log

echo "--- Top 10 IP Addresses Visiting ---"
grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' "$LOG_FILE" | sort | uniq -c | sort -nr | head -10

echo -e "
--- Count of HTTP Status Codes ---"
awk '{print $9}' "$LOG_FILE" | sort | uniq -c | sort -nr

echo -e "
--- Count of 404 Not Found Errors ---"
grep " 404 " "$LOG_FILE" | wc -l

echo -e "
--- URIs with 500 Internal Server Errors ---"
grep " 500 " "$LOG_FILE" | awk '{print $7}' | sort | uniq -c | sort -nr | head -10

# You can also filter by date range, e.g., for today's errors
# grep "23/Dec/2023" "$LOG_FILE" | grep " 500 " | awk '{print $7}'
How it works: This script provides examples of how to parse common web server access logs using `grep` and `awk`. It demonstrates how to find the top visiting IP addresses, count HTTP status codes, specifically count 404 errors, and identify URIs frequently causing 500 internal server errors. These techniques are invaluable for quick debugging and monitoring web application health directly from the server's logs.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs