Last active
August 29, 2015 14:09
-
-
Save sahal/d77bd25be45c1865ad3b to your computer and use it in GitHub Desktop.
Quick Bash script to geolocate IPs causing lines in your apache error logs
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
#!/bin/bash | |
# desc: find out where misfits (those causing apache errors) are located | |
# by: Sahal Ansari (github@sahal.info) | |
# use: ./misfits.sh [stats|top10] | |
# requires: geoip-bin package in Debian (and derivatives?) | |
DIR="$( cd "$( dirname "$0" )" && pwd )" | |
misfitlist="$DIR/list" | |
apache_error_log_location="/var/log/apache2/error.log" | |
#local copy of error.log exists | |
#apache_error_log_location="$DIR/error.log" | |
ips=( $(grep "File does not exist: " "$apache_error_log_location" | sed -e 's/^.*\[client\ //' -e 's/\].*//'| sort | uniq | tr '\n' '\ ' | tee "$misfitlist") ) | |
function do_lookup { | |
for ((element = 0 ; element <= ("${#ips[@]}" - 1); element++)) | |
do | |
# echo "${ips[$element]}" | |
geoiplookup "${ips[$element]}" | |
done | |
} | |
function do_stats { | |
do_lookup | sort | uniq -c | sort -rn | sed "s/\GeoIP\ Country\ Edition\:\ //" | |
} | |
function do_top10 { | |
do_stats | head -10 | |
} | |
case "$1" in | |
top10) | |
do_top10 | |
;; | |
stats) | |
do_stats | |
;; | |
*) | |
do_lookup | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment