2015-12-06 13:55:50 +00:00
|
|
|
|
#!/usr/bin/env bash
|
2016-01-30 20:12:40 +00:00
|
|
|
|
# Pi-hole: A black hole for Internet advertisements
|
|
|
|
|
# (c) 2015, 2016 by Jacob Salmela
|
|
|
|
|
# Network-wide ad blocking via your Raspberry Pi
|
|
|
|
|
# http://pi-hole.net
|
|
|
|
|
# Calculates stats and displays to an LCD
|
2015-12-06 13:55:50 +00:00
|
|
|
|
#
|
|
|
|
|
# Pi-hole is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 2 of the License, or
|
2016-01-18 22:24:02 +00:00
|
|
|
|
# (at your option) any later version.
|
2016-01-18 22:03:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Functions##############################################################################################################
|
|
|
|
|
piLog="/var/log/pihole.log"
|
|
|
|
|
gravity="/etc/pihole/gravity.list"
|
|
|
|
|
|
|
|
|
|
today=$(date "+%b %e")
|
|
|
|
|
|
|
|
|
|
function CalcBlockedDomains(){
|
2016-01-19 21:14:01 +00:00
|
|
|
|
CheckIPv6
|
2016-01-18 22:03:53 +00:00
|
|
|
|
if [ -e "$gravity" ]; then
|
|
|
|
|
#Are we IPV6 or IPV4?
|
2016-08-21 01:12:02 +00:00
|
|
|
|
if [[ -n ${piholeIPv6} ]];then
|
2016-01-18 22:03:53 +00:00
|
|
|
|
#We are IPV6
|
|
|
|
|
blockedDomainsTotal=$(wc -l /etc/pihole/gravity.list | awk '{print $1/2}')
|
|
|
|
|
else
|
|
|
|
|
#We are IPV4
|
|
|
|
|
blockedDomainsTotal=$(wc -l /etc/pihole/gravity.list | awk '{print $1}')
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
blockedDomainsTotal="Err."
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function CalcQueriesToday(){
|
|
|
|
|
if [ -e "$piLog" ];then
|
2016-04-03 00:20:54 +00:00
|
|
|
|
queriesToday=$(cat "$piLog" | grep "$today" | awk '/query/ {print $6}' | wc -l)
|
2016-01-18 22:03:53 +00:00
|
|
|
|
else
|
|
|
|
|
queriesToday="Err."
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function CalcblockedToday(){
|
|
|
|
|
if [ -e "$piLog" ] && [ -e "$gravity" ];then
|
2016-08-21 01:12:02 +00:00
|
|
|
|
blockedToday=$(cat ${piLog} | awk '/\/etc\/pihole\/gravity.list/ && !/address/ {print $6}' | wc -l)
|
2016-01-18 22:03:53 +00:00
|
|
|
|
else
|
|
|
|
|
blockedToday="Err."
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function CalcPercentBlockedToday(){
|
|
|
|
|
if [ "$queriesToday" != "Err." ] && [ "$blockedToday" != "Err." ]; then
|
2016-02-14 02:58:09 +00:00
|
|
|
|
if [ "$queriesToday" != 0 ]; then #Fixes divide by zero error :)
|
|
|
|
|
#scale 2 rounds the number down, so we'll do scale 4 and then trim the last 2 zeros
|
|
|
|
|
percentBlockedToday=$(echo "scale=4; $blockedToday/$queriesToday*100" | bc)
|
|
|
|
|
percentBlockedToday=$(sed 's/.\{2\}$//' <<< "$percentBlockedToday")
|
|
|
|
|
else
|
|
|
|
|
percentBlockedToday=0
|
|
|
|
|
fi
|
2016-01-18 22:03:53 +00:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function CheckIPv6(){
|
|
|
|
|
piholeIPv6file="/etc/pihole/.useIPv6"
|
2016-08-21 01:12:02 +00:00
|
|
|
|
if [[ -f ${piholeIPv6file} ]];then
|
2016-01-18 22:03:53 +00:00
|
|
|
|
# If the file exists, then the user previously chose to use IPv6 in the automated installer
|
|
|
|
|
piholeIPv6=$(ip -6 route get 2001:4860:4860::8888 | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }')
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function outputJSON(){
|
|
|
|
|
CalcQueriesToday
|
|
|
|
|
CalcblockedToday
|
|
|
|
|
CalcPercentBlockedToday
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2016-01-18 22:03:53 +00:00
|
|
|
|
CalcBlockedDomains
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2016-01-18 22:03:53 +00:00
|
|
|
|
printf '{"domains_being_blocked":"%s","dns_queries_today":"%s","ads_blocked_today":"%s","ads_percentage_today":"%s"}\n' "$blockedDomainsTotal" "$queriesToday" "$blockedToday" "$percentBlockedToday"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function normalChrono(){
|
|
|
|
|
for (( ; ; ))
|
|
|
|
|
do
|
|
|
|
|
clear
|
|
|
|
|
# Displays a colorful Pi-hole logo
|
2016-05-07 18:44:18 +00:00
|
|
|
|
echo " [0;1;35;95m_[0;1;31;91m__[0m [0;1;33;93m_[0m [0;1;34;94m_[0m [0;1;36;96m_[0m"
|
|
|
|
|
echo "[0;1;31;91m|[0m [0;1;33;93m_[0m [0;1;32;92m(_[0;1;36;96m)_[0;1;34;94m__[0;1;35;95m|[0m [0;1;31;91m|_[0m [0;1;32;92m__[0;1;36;96m_|[0m [0;1;34;94m|[0;1;35;95m__[0;1;31;91m_[0m"
|
|
|
|
|
echo "[0;1;33;93m|[0m [0;1;32;92m_[0;1;36;96m/[0m [0;1;34;94m|_[0;1;35;95m__[0;1;31;91m|[0m [0;1;33;93m'[0m [0;1;32;92m\/[0m [0;1;36;96m_[0m [0;1;34;94m\[0m [0;1;35;95m/[0m [0;1;31;91m-[0;1;33;93m_)[0m"
|
|
|
|
|
echo "[0;1;32;92m|_[0;1;36;96m|[0m [0;1;34;94m|_[0;1;35;95m|[0m [0;1;33;93m|_[0;1;32;92m||[0;1;36;96m_\[0;1;34;94m__[0;1;35;95m_/[0;1;31;91m_\[0;1;33;93m__[0;1;32;92m_|[0m"
|
|
|
|
|
echo ""
|
2016-01-18 22:03:53 +00:00
|
|
|
|
echo " $(ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -d':' -f2)"
|
|
|
|
|
echo ""
|
|
|
|
|
uptime | cut -d' ' -f11-
|
2016-03-08 18:19:01 +00:00
|
|
|
|
#uptime -p #Doesn't work on all versions of uptime
|
|
|
|
|
uptime | awk -F'( |,|:)+' '{if ($7=="min") m=$6; else {if ($7~/^day/) {d=$6;h=$8;m=$9} else {h=$6;m=$7}}} {print d+0,"days,",h+0,"hours,",m+0,"minutes."}'
|
2016-01-18 22:03:53 +00:00
|
|
|
|
echo "-------------------------------"
|
|
|
|
|
# Uncomment to continually read the log file and display the current domain being blocked
|
|
|
|
|
#tail -f /var/log/pihole.log | awk '/\/etc\/pihole\/gravity.list/ {if ($7 != "address" && $7 != "name" && $7 != "/etc/pihole/gravity.list") print $7; else;}'
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2016-01-18 22:03:53 +00:00
|
|
|
|
#uncomment next 4 lines to use original query count calculation
|
|
|
|
|
#today=$(date "+%b %e")
|
|
|
|
|
#todaysQueryCount=$(cat /var/log/pihole.log | grep "$today" | awk '/query/ {print $7}' | wc -l)
|
|
|
|
|
#todaysQueryCountV4=$(cat /var/log/pihole.log | grep "$today" | awk '/query/ && /\[A\]/ {print $7}' | wc -l)
|
|
|
|
|
#todaysQueryCountV6=$(cat /var/log/pihole.log | grep "$today" | awk '/query/ && /\[AAAA\]/ {print $7}' | wc -l)
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
|
|
|
|
|
2016-01-18 22:03:53 +00:00
|
|
|
|
CalcQueriesToday
|
|
|
|
|
CalcblockedToday
|
|
|
|
|
CalcPercentBlockedToday
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2016-01-18 22:03:53 +00:00
|
|
|
|
CalcBlockedDomains
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2016-01-18 22:03:53 +00:00
|
|
|
|
echo "Blocking: $blockedDomainsTotal"
|
|
|
|
|
#below commented line does not add up to todaysQueryCount
|
|
|
|
|
#echo "Queries: $todaysQueryCountV4 / $todaysQueryCountV6"
|
|
|
|
|
echo "Queries: $queriesToday" #same total calculation as dashboard
|
|
|
|
|
echo "Pi-holed: $blockedToday ($percentBlockedToday%)"
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2016-01-18 22:03:53 +00:00
|
|
|
|
sleep 5
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function displayHelp(){
|
2016-03-28 23:31:21 +00:00
|
|
|
|
echo "::: Displays stats about your piHole!"
|
|
|
|
|
echo ":::"
|
2016-04-10 20:17:58 +00:00
|
|
|
|
echo "::: Usage: sudo pihole -c [optional:-j]"
|
2016-03-28 23:31:21 +00:00
|
|
|
|
echo "::: Note: If no option is passed, then stats are displayed on screen, updated every 5 seconds"
|
|
|
|
|
echo ":::"
|
|
|
|
|
echo "::: Options:"
|
|
|
|
|
echo "::: -j, --json output stats as JSON formatted string"
|
|
|
|
|
echo "::: -h, --help display this help text"
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2016-01-18 22:03:53 +00:00
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if [[ $# = 0 ]]; then
|
|
|
|
|
normalChrono
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
for var in "$@"
|
|
|
|
|
do
|
|
|
|
|
case "$var" in
|
|
|
|
|
"-j" | "--json" ) outputJSON;;
|
2016-03-06 05:15:11 +00:00
|
|
|
|
"-h" | "--help" ) displayHelp;;
|
2016-01-18 22:03:53 +00:00
|
|
|
|
* ) exit 1;;
|
|
|
|
|
esac
|
2016-01-18 22:15:39 +00:00
|
|
|
|
done
|