Last active
December 21, 2015 14:29
-
-
Save metalrufflez/6320329 to your computer and use it in GitHub Desktop.
Count the number of ip requests on an nginx logfile during one minuteYou can use regex filtering for specific requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function recent_ip() { | |
helper_file="/tmp/ips" | |
log_file=$1 | |
pattern=$2 | |
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 | |
echo "Counting IPs" | |
tail -f $log_file | egrep "${pattern:-.}" > $helper_file & | |
sleep 60 | |
kill %1 | |
echo "-------------------------------------" | |
awk '! /^-/ { print $1 }' $helper_file | tr -d , | sort | uniq -c | sort -k1,1nr -t. -k2,2n -k3,3n -k4,4n -k5,5n | head | |
rm $helper_file | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment