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
|
2017-02-22 17:55:20 +00:00
|
|
|
|
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
|
|
|
|
|
# Network-wide ad blocking via your own hardware.
|
|
|
|
|
#
|
2016-01-30 20:12:40 +00:00
|
|
|
|
# Calculates stats and displays to an LCD
|
2015-12-06 13:55:50 +00:00
|
|
|
|
#
|
2017-02-22 17:55:20 +00:00
|
|
|
|
# This file is copyright under the latest version of the EUPL.
|
|
|
|
|
# Please see LICENSE file for your rights under this license.
|
|
|
|
|
|
2016-01-18 22:03:53 +00:00
|
|
|
|
#Functions##############################################################################################################
|
|
|
|
|
piLog="/var/log/pihole.log"
|
|
|
|
|
gravity="/etc/pihole/gravity.list"
|
|
|
|
|
|
2016-11-02 00:01:48 +00:00
|
|
|
|
. /etc/pihole/setupVars.conf
|
|
|
|
|
|
2017-03-07 16:53:36 +00:00
|
|
|
|
function GetFTLData {
|
|
|
|
|
# Open connection to FTL
|
|
|
|
|
exec 3<>/dev/tcp/localhost/"$(cat /var/run/pihole-FTL.port)"
|
|
|
|
|
|
|
|
|
|
# Test if connection is open
|
|
|
|
|
if { >&3; } 2> /dev/null; then
|
|
|
|
|
# Send command to FTL
|
|
|
|
|
echo -e ">$1" >&3
|
|
|
|
|
|
|
|
|
|
# Read input
|
|
|
|
|
read -r -t 1 LINE <&3
|
|
|
|
|
until [ ! $? ] || [[ "$LINE" == *"EOM"* ]]; do
|
|
|
|
|
echo "$LINE" >&1
|
|
|
|
|
read -r -t 1 LINE <&3
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Close connection
|
|
|
|
|
exec 3>&-
|
|
|
|
|
exec 3<&-
|
|
|
|
|
fi
|
2016-01-18 22:03:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 02:47:45 +00:00
|
|
|
|
outputJSON() {
|
2017-03-07 16:53:36 +00:00
|
|
|
|
get_summary_data
|
2017-03-07 16:57:57 +00:00
|
|
|
|
echo "{\"domains_being_blocked\":${domains_being_blocked_raw},\"dns_queries_today\":${dns_queries_today_raw},\"ads_blocked_today\":${ads_blocked_today_raw},\"ads_percentage_today\":${ads_percentage_today_raw}}"
|
2017-03-07 16:53:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get_summary_data() {
|
|
|
|
|
local summary=$(GetFTLData "stats")
|
|
|
|
|
domains_being_blocked_raw=$(grep "domains_being_blocked" <<< "${summary}" | grep -Eo "[0-9]+$")
|
|
|
|
|
domains_being_blocked=$(printf "%'.f" ${domains_being_blocked_raw})
|
|
|
|
|
dns_queries_today_raw=$(grep "dns_queries_today" <<< "$summary" | grep -Eo "[0-9]+$")
|
|
|
|
|
dns_queries_today=$(printf "%'.f" ${dns_queries_today_raw})
|
|
|
|
|
ads_blocked_today_raw=$(grep "ads_blocked_today" <<< "$summary" | grep -Eo "[0-9]+$")
|
|
|
|
|
ads_blocked_today=$(printf "%'.f" ${ads_blocked_today_raw})
|
|
|
|
|
ads_percentage_today_raw=$(grep "ads_percentage_today" <<< "$summary" | grep -Eo "[0-9.]+$")
|
|
|
|
|
LC_NUMERIC=C ads_percentage_today=$(printf "%'.f" ${ads_percentage_today_raw})
|
2016-01-18 22:03:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 02:47:45 +00:00
|
|
|
|
normalChrono() {
|
2016-10-22 07:14:38 +00:00
|
|
|
|
for (( ; ; )); do
|
2017-03-07 17:10:05 +00:00
|
|
|
|
get_summary_data
|
|
|
|
|
domain=$(GetFTLData recentBlocked)
|
2016-01-18 22:03:53 +00:00
|
|
|
|
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-11-06 17:16:48 +00:00
|
|
|
|
echo " ${IPV4_ADDRESS}"
|
2016-01-18 22:03:53 +00:00
|
|
|
|
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 "-------------------------------"
|
2017-01-16 21:39:58 +00:00
|
|
|
|
echo "Recently blocked:"
|
|
|
|
|
echo " $domain"
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2017-03-07 16:53:36 +00:00
|
|
|
|
echo "Blocking: ${domains_being_blocked}"
|
|
|
|
|
echo "Queries: ${dns_queries_today}"
|
|
|
|
|
echo "Pi-holed: ${ads_blocked_today} (${ads_percentage_today}%)"
|
2016-03-06 05:15:11 +00:00
|
|
|
|
|
2016-01-18 22:03:53 +00:00
|
|
|
|
sleep 5
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 02:47:45 +00:00
|
|
|
|
displayHelp() {
|
2016-10-20 03:46:37 +00:00
|
|
|
|
cat << EOM
|
|
|
|
|
::: Displays stats about your piHole!
|
|
|
|
|
:::
|
|
|
|
|
::: Usage: sudo pihole -c [optional:-j]
|
|
|
|
|
::: Note: If no option is passed, then stats are displayed on screen, updated every 5 seconds
|
|
|
|
|
:::
|
|
|
|
|
::: Options:
|
|
|
|
|
::: -j, --json output stats as JSON formatted string
|
|
|
|
|
::: -h, --help display this help text
|
|
|
|
|
EOM
|
2016-12-01 23:10:06 +00:00
|
|
|
|
exit 0
|
2016-01-18 22:03:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if [[ $# = 0 ]]; then
|
|
|
|
|
normalChrono
|
|
|
|
|
fi
|
|
|
|
|
|
2016-10-22 07:14:38 +00:00
|
|
|
|
for var in "$@"; do
|
|
|
|
|
case "$var" in
|
|
|
|
|
"-j" | "--json" ) outputJSON;;
|
|
|
|
|
"-h" | "--help" ) displayHelp;;
|
|
|
|
|
* ) exit 1;;
|
|
|
|
|
esac
|
2016-01-18 22:15:39 +00:00
|
|
|
|
done
|