etc~ scripts and random things |
| git clone https://github.com/djnnvx/etc.git |
| log | files | refs | archive |
logs-summary.sh (2154B)
1 #!/bin/bash 2 3 # --- Nginx Log Summary Script --- 4 # 5 # This script scans all Nginx logs (compressed and uncompressed) 6 # in /var/log/nginx and prints the top N most common 7 # visited links, client IPs, user agents, and errors. 8 # 9 # --- USAGE --- 10 # 11 # Run with sudo and an optional number: 12 # sudo ./log-summary.sh [NUMBER] 13 # 14 # [NUMBER]: The number of top results to show (e.g., 10, 50, 100). 15 # Defaults to 50 if not provided. 16 # 17 # Get help: 18 # ./log-summary.sh -h 19 # 20 # --- 21 22 # --- Argument Parsing --- 23 24 if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then 25 head -n 20 "$0" 26 exit 0 27 fi 28 29 DEFAULT_LIMIT=50 30 LIMIT="${1:-$DEFAULT_LIMIT}" # Use $1 if provided, else use the default 31 32 if ! [[ "$LIMIT" =~ ^[0-9]+$ ]]; then 33 echo "Error: Argument must be a positive number." >&2 34 echo "Usage: sudo $0 [NUMBER]" >&2 35 echo " or: $0 -h" >&2 36 exit 1 37 fi 38 39 set -e 40 41 # --- Script Body --- 42 43 echo "Navigating to /var/log/nginx..." 44 cd /var/log/nginx 45 46 print_header() { 47 echo "" 48 echo "=============================================" 49 echo " $1" 50 echo "=============================================" 51 echo "" 52 } 53 54 echo "Showing Top $LIMIT results for all categories..." 55 56 # --- Access Log Analysis --- 57 58 print_header "Top $LIMIT Most Visited Links" 59 (cat access.log access.log.1 2>/dev/null ; zcat access.log.*.gz 2>/dev/null) | \ 60 awk -F'"' '{ print $2 }' | awk '{ print $2 }' | \ 61 sort | uniq -c | sort -nr | head -n "$LIMIT" 62 63 print_header "Top $LIMIT Client IPs" 64 (cat access.log access.log.1 2>/dev/null ; zcat access.log.*.gz 2>/dev/null) | \ 65 awk -F'"' 'NF >= 8 { print $8 }' | \ 66 tr -d ' ' | \ 67 sort | uniq -c | sort -nr | head -n "$LIMIT" 68 69 print_header "Top $LIMIT User Agents" 70 (cat access.log access.log.1 2>/dev/null ; zcat access.log.*.gz 2>/dev/null) | \ 71 awk -F'"' 'NF >= 6 { print $6 }' | \ 72 sort | uniq -c | sort -nr | head -n "$LIMIT" 73 74 # --- Error Log Analysis --- 75 76 print_header "Top $LIMIT Nginx Errors" 77 (cat error.log error.log.1 2>/dev/null ; zcat error.log.*.gz 2>/dev/null) | \ 78 awk -F'] ' '{print $2}' | awk -F',' '{print $1}' | \ 79 sort | uniq -c | sort -nr | head -n "$LIMIT" 80 81 echo "" 82 echo "Done."