#!/bin/sh
L=10
AL=1
LINE=--------------------------------
dir=/tmp/mnt/USB/adblocking/logs
printf "\nThe top $L requested domains were:\n$LINE\n"
awk '/query\[A]/ {print $6}' $dir/dnsmasq.log | awk '{for(i=1;i<=NF;i++)a[$i]++}END{for(o in a) printf "\n%s %s ",a[o],o}' | sort -nr | head -$L
printf "\nThe top $L blocked ad domains were:\n$LINE\n"
awk '/is 0.0.0.0/ {print $6}' $dir/dnsmasq.log | awk '{for(i=1;i<=NF;i++)a[$i]++}END{for(o in a) printf "\n%s %s ",a[o],o}' | sort -nr | head -$L
printf "\nThe top $L blocked custom domains were:\n$LINE\n"
awk '/is 10.0.0.1/ {print $6}' $dir/dnsmasq.log | awk '{for(i=1;i<=NF;i++)a[$i]++}END{for(o in a) printf "\n%s %s ",a[o],o}' | sort -nr | head -$L
rm -f temp*
printf "\nThe top $L noisiest name clients:\n$LINE\n"
awk '/from 192.168.1./ {print $8}' $dir/dnsmasq.log | awk '{for(i=1;i<=NF;i++)a[$i]++}END{for(o in a) printf "\n%s %s ",a[o],o}' | sort -nr | head -$L > temp1
for i in $(awk '{print $2}' temp1); do
grep -e "$i" $dir/dnsmasq.log | awk '{print $6}'| awk '{for(i=1;i<=NF;i++)a[$i]++}END{for(o in a) printf "\n %s %s",a[o],o}' | sort -nr | head -1 >> temp2
CH="$(awk 'END{print $1}' temp2)"
TH="$(awk -v AL="$AL" 'FNR==AL{print $1}' temp1)"
let AL=AL+1
awk -v CH="$CH" -v TH="$TH" 'BEGIN{print ((CH * 100)/TH)"%"}' >> temp3
done
awk 'NR==FNR{a[FNR]=$0 " -";next} {print a[FNR],$0}' temp2 temp3 > temp4
awk 'NR==FNR{a[FNR]=$0 " -";next} {print a[FNR],$0}' temp1 temp4
printf "\nTop $L domains for top $L clients:\n$LINE\n"
for i in $(awk '{print $2}' temp1); do
printf "$i:\n"
grep -e "$i" $dir/dnsmasq.log | awk '{print $6}'| awk '{for(i=1;i<=NF;i++)a[$i]++}END{for(o in a) printf "\n %s %s",a[o],o}' | sort -nr | head -$L
done
rm -f temp*