feat: arguments and file aggregation

Early work, going to wait a few days for my new box to get enough data
to run this properly.
This commit is contained in:
Josh Sherman 2025-06-19 20:29:00 -05:00
parent b9a27f343d
commit 8919943cc5
No known key found for this signature in database
GPG key ID: 55B058A80530EF22

65
nginx-popular-paths.sh Normal file
View file

@ -0,0 +1,65 @@
#!/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