Early work, going to wait a few days for my new box to get enough data to run this properly.
65 lines
1.2 KiB
Bash
65 lines
1.2 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Nginx Popular Paths
|
|
# Just a simple script to mine Nginx logs to find the most popular paths.
|
|
#
|
|
# Usage:
|
|
# ./nginx-popular-paths.sh [-L|--logfile <file>] [-D|--days <n>] [-C|--count <n>]
|
|
#
|
|
# Author: Josh Sherman (joshtronic.com)
|
|
# Link: https://git.sherver.org/joshtronic/nginx-popular-paths
|
|
# License: MIT
|
|
|
|
# Argument defaults
|
|
LOGFILE='/var/log/nginx/access.log'
|
|
DAYS=30
|
|
COUNT=10
|
|
|
|
# Parse any arguments
|
|
while [[ "$#" -gt 0 ]]; do
|
|
case "$1" in
|
|
-C|--count)
|
|
COUNT="$2"
|
|
shift 2
|
|
;;
|
|
-D|--days)
|
|
DAYS="$2"
|
|
shift 2
|
|
;;
|
|
-L|--logfile)
|
|
LOGFILE="$2"
|
|
shift 2
|
|
;;
|
|
-*)
|
|
echo "Unknown option: $1" >&2
|
|
exit 1
|
|
;;
|
|
*)
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Stand up and tear down our temp file
|
|
TMPFILE=$(mktemp)
|
|
trap 'rm -f "$TMPFILE"' EXIT
|
|
|
|
# Start with the base logfile
|
|
[[ -f "$LOGFILE" ]] && cat "$LOGFILE" >> "$TMPFILE"
|
|
|
|
# Look for additional logfiles and archives
|
|
for i in $(seq 1 99); do
|
|
RAWFILE="$LOGFILE.$i"
|
|
GZFILE="$LOGFILE.$i.gz"
|
|
|
|
if [[ -f "$RAWFILE" ]]; then
|
|
cat "$RAWFILE" >> "$TMPFILE"
|
|
elif [[ -f "$GZFILE" ]]; then
|
|
zcat "$GZFILE" >> "$TMPFILE"
|
|
else
|
|
break
|
|
fi
|
|
done
|
|
|
|
# DEBUG: Log the new file
|
|
cat $TMPFILE
|