Skip to content

Instantly share code, notes, and snippets.

@metalrufflez
Last active December 21, 2015 14:29
Show Gist options
  • Save metalrufflez/6320312 to your computer and use it in GitHub Desktop.
Save metalrufflez/6320312 to your computer and use it in GitHub Desktop.
Count the number of ip requests in a nginx log.By default it greps an entire log, or you can use regex filtering for specific requests or timeframe
function count_ip() {
log_file=$1
pattern=$2
helper_file="/tmp/helperip"
if [[ $# -eq 0 ]]; then
echo "Usage: $FUNCNAME <log file> [pattern]"
return
fi
if [[ ! -a $log_file ]]; then
echo "$log_file doesn't exist"
return
fi
if [[ -z $pattern ]]; then
helper_file=$log_file
else
egrep "$pattern" $log_file > $helper_file
fi
awk '! /^-/ { print $1 }' $helper_file | tr -d , | sort | uniq -c | sort -k1,1nr -t. -k2,2n -k3,3n -k4,4n -k5,5n | head
if [[ ! -z $pattern ]]; then
rm -f $helper_file
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment