Skip to content

Instantly share code, notes, and snippets.

@arzzen
Last active Oct 4, 2019
Embed
What would you like to do?
accesslog.sh
#!/bin/bash
LOGFILE="access.log"
LIMIT=10
while getopts l:f: option
do
case "${option}"
in
l) LIMIT=${OPTARG};;
f) LOGFILE=${OPTARG};;
esac
done
LOGFILE_GZ="$LOGFILE.*"
RESPONSE_CODE="200"
filters() {
grep $RESPONSE_CODE
#\
# | grep -v "\/rss\/" \
# | grep -v robots.txt \
# | grep -v "\.css" \
# | grep -v "\.jss*" \
# | grep -v "\.png" \
# | grep -v "\.ico"
}
filters_404() {
grep "404"
}
request_ips() {
awk '{print $1}'
}
request_method() {
awk '{print $6}' | cut -d'"' -f2
}
wordcount() {
sort | uniq -c
}
sort_desc() {
sort -rn
}
return_kv() {
awk '{print $1, $2}'
}
request_pages() {
awk '{print $7}'
}
referer_pages() {
awk '{print $11}'
}
return_top_ten() {
head -$LIMIT
}
## actions
get_request_ips() {
echo ""
echo "Top $LIMIT Request IP's:"
echo "===================="
cat $LOGFILE \
| filters \
| request_ips \
| wordcount \
| sort_desc \
| return_kv \
| return_top_ten
echo ""
}
get_request_methods() {
echo "Top Request Methods:"
echo "===================="
cat $LOGFILE \
| filters \
| request_method \
| wordcount \
| return_kv
echo ""
}
get_request_pages_404() {
echo "Top $LIMIT: 404 Page Responses:"
echo "==========================="
zgrep '-' $LOGFILE $LOGFILE_GZ\
| filters_404 \
| request_pages \
| wordcount \
| sort_desc \
| return_kv \
| return_top_ten
echo ""
}
get_request_pages() {
echo "Top $LIMIT Request Pages:"
echo "====================="
cat $LOGFILE \
| filters \
| request_pages \
| wordcount \
| sort_desc \
| return_kv \
| return_top_ten
echo ""
}
get_referer_pages() {
echo "Top $LIMIT Referer Pages:"
echo "====================="
cat $LOGFILE \
| filters \
| referer_pages \
| wordcount \
| sort_desc \
| return_kv \
| return_top_ten
echo ""
}
get_request_pages_all() {
echo "Top $LIMIT Request Pages from All Logs:"
echo "==================================="
zgrep '-' --no-filename $LOGFILE $LOGFILE_GZ \
| filters \
| request_pages \
| wordcount \
| sort_desc \
| return_kv \
| return_top_ten
echo ""
}
get_referer_pages_all() {
echo "Top $LIMIT Referer Pages from All Logs:"
echo "==================================="
zgrep '-' --no-filename $LOGFILE $LOGFILE_GZ \
| filters \
| referer_pages \
| wordcount \
| sort_desc \
| return_kv \
| return_top_ten
echo ""
}
# executing
get_request_ips
get_request_methods
get_request_pages
get_referer_pages
get_request_pages_all
get_referer_pages_all
get_request_pages_404
@arzzen

This comment has been minimized.

Copy link
Owner Author

@arzzen arzzen commented Oct 4, 2019

usage ./accesslog.sh -l 10 -f /var/log/apache2/access.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment