Skip to content

Instantly share code, notes, and snippets.

@capjamesg
Created October 7, 2020 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capjamesg/641fe600ebb58389a52ad4199eed2d4c to your computer and use it in GitHub Desktop.
Save capjamesg/641fe600ebb58389a52ad4199eed2d4c to your computer and use it in GitHub Desktop.
A bash script to parse my nginx access logs.
#!/bin/bash
rm /home/james/logs/*
log_files=/home/james/logs/
# Copy log files to temporary directory
cp /var/log/nginx/access*.gz $log_files
for f in $log_files/*;
do
gunzip -d "$f"
done
# Manually copy plain-text access files
cp /var/log/nginx/access.log $log_files
cp /var/log/nginx/access.log.1 $log_files
page_view_counter=`cat $log_files* | wc -l`
feed_counter=`cat $log_files* | grep "feed.xml" | wc -l`
# Show top five performing pages
echo "Top Five Pages"
awk '$7 !~ "/assets/*|robots.txt|favicon.ico|400|/2020/*|wp-*|vendor" {print $7}' $log_files/* | \
sort | \
uniq -c | \
sort -r | \
head -n 5
echo
echo "Top Five Posts"
awk '$7 ~ "/2020/*" {print $7}' $log_files/* | \
sort | \
uniq -c | \
sort -r | \
head -n 5
echo
echo "Status Codes"
awk '{print $9}' $log_files/* | \
sort | \
uniq -c | \
sort -r | \
head -n 5
echo
echo "Total Pageviews: $page_view_counter"
echo "Total Feed Hits: $feed_counter"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment