Skip to content

Instantly share code, notes, and snippets.

@notsobad
Created August 4, 2016 03:04
Show Gist options
  • Save notsobad/499066efa7cd71f1fbd34df26aa7a798 to your computer and use it in GitHub Desktop.
Save notsobad/499066efa7cd71f1fbd34df26aa7a798 to your computer and use it in GitHub Desktop.
count 50x error in nginx error log
#!/bin/bash
# By notsobad
f=$1
if [ "$f" = "" ]; then
echo "Usage: $0 LOG_FILE" 1>&2
echo "like: $0 /var/log/nginx/www.xxx.com_log-20160313"
echo "or: $0 /var/log/nginx/www.xxx.com_log-20160313.gz"
exit 1
fi
echo "$f" | grep -q ".gz$" && {
cmd="zcat $f"
} || {
cmd="cat $f"
}
$cmd | awk '{
i = 0
if($9 ~ "^50"){
url = $7
i = index(url, "?")
if(i >= 1){
url = substr(url, 0, i - 1)
}
arr[url] += 1
}
}
END{
for(i in arr){
print(i, arr[i])
}
}' | sort -n -k2 -r
@alvesoaj
Copy link

Wow, nice job! Thanks!

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