Skip to content

Instantly share code, notes, and snippets.

@todmephis
Created July 12, 2018 03:48
Show Gist options
  • Save todmephis/5929a9e18717be9ae5601e2f6f53d062 to your computer and use it in GitHub Desktop.
Save todmephis/5929a9e18717be9ae5601e2f6f53d062 to your computer and use it in GitHub Desktop.
BulletProof plugin Log Analyzer. Takes security log and output all blocked requests showing them by date and blocked requests per month.
#!/bin/bash
#by @todmephis
#BulletProof WordPress Plugin Log Analyzer.
#Takes bulletproof's security log and output all blocked requests showing them by date and bloked requests per month.
usage() { echo "Usage: $0 [-f <log_file>] " 1>&2; exit 1; }
while getopts ":f:" o; do
case "${o}" in
f)
FILE=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z $FILE ]; then
echo "Error: Zero sized string" 1>&2; exit 1;
elif [ ! -e $FILE ]; then
echo "Error: File $FILE does not exist" 1>&2; exit 1;
elif [ ! -s $FILE ]; then
echo "Error: File $FILE is empty" 1>&2; exit 1;
elif [ ! -r $FILE ]; then
echo -n "Error: File $FILE not readable for user "; whoami; 1>&2; exit 1;
elif [[ ! "$(file "$FILE")" =~ ': ASCII text'$ ]]; then
echo "Error: Non ASCII file"; 1>&2; exit 1;
fi
r_months=($(cat $FILE | grep "^\[" | awk '{print $5}' | uniq -c | sed 's/,$//' | sed -e 1b -e '$!d' | awk '{print $2}'))
echo -e "[*]Log file [${FILE}] from [${r_months[0]}] to [${r_months[1]}]\n"
echo [*]Showing detailed info
echo -e "\nREQ TYPE DATE \t TIME\t Times Requested\n"
cat $FILE | grep "^\[" | uniq -c | awk '{$4="\b"; printf substr($0, index($0,$2)); print "\t" $1}'
echo -e "\n[*]Showing info by month"
echo -e "\nMONTH\t Times Requested"
cat $FILE | grep "^\[" | awk '{print $5}' | uniq -c | sed 's/,$//' | awk '{print $2"\t\t"$1}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment