1
0
mirror of https://github.com/pi-hole/pi-hole synced 2024-11-18 14:18:15 +00:00

Merge pull request #1308 from pi-hole/new/FTLChronometer

Let Chronometer query all data from FTL directly
This commit is contained in:
DL6ER 2017-03-07 19:41:23 +01:00 committed by GitHub
commit 6e6259975e

View File

@ -8,34 +8,55 @@
# This file is copyright under the latest version of the EUPL. # This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license. # Please see LICENSE file for your rights under this license.
#Functions############################################################################################################## #Functions##############################################################################################################
piLog="/var/log/pihole.log" piLog="/var/log/pihole.log"
gravity="/etc/pihole/gravity.list" gravity="/etc/pihole/gravity.list"
. /etc/pihole/setupVars.conf . /etc/pihole/setupVars.conf
# Borrowed/modified from https://gist.github.com/cjus/1047794 function GetFTLData {
function GetJSONValue { # Open connection to FTL
retVal=$(echo $1 | sed 's/\\\\\//\//g' | \ exec 3<>/dev/tcp/localhost/"$(cat /var/run/pihole-FTL.port)"
sed 's/[{}]//g' | \
awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | \ # Test if connection is open
sed 's/\"\:/\|/g' | \ if { >&3; } 2> /dev/null; then
sed 's/[\,]/ /g' | \ # Send command to FTL
sed 's/\"//g' | \ echo -e ">$1" >&3
grep -w $2)
echo ${retVal##*|} # 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
} }
outputJSON() { outputJSON() {
json=$(curl -s -X GET http://127.0.0.1/admin/api.php?summaryRaw) get_summary_data
echo ${json} 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}}"
}
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})
} }
normalChrono() { normalChrono() {
for (( ; ; )); do for (( ; ; )); do
get_summary_data
domain=$(GetFTLData recentBlocked)
clear clear
# Displays a colorful Pi-hole logo # Displays a colorful Pi-hole logo
echo " ___ _ _ _" echo " ___ _ _ _"
@ -49,23 +70,13 @@ normalChrono() {
#uptime -p #Doesn't work on all versions of uptime #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."}' 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."}'
echo "-------------------------------" echo "-------------------------------"
domain=$(curl -s -X GET http://127.0.0.1/admin/api.php?recentBlocked) domain=$(GetFTLData recentBlocked)
echo "Recently blocked:" echo "Recently blocked:"
echo " $domain" echo " $domain"
# 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;}'
json=$(curl -s -X GET http://127.0.0.1/admin/api.php?summaryRaw) echo "Blocking: ${domains_being_blocked}"
echo "Queries: ${dns_queries_today}"
domains=$(printf "%'.f" $(GetJSONValue ${json} "domains_being_blocked")) #add commas in echo "Pi-holed: ${ads_blocked_today} (${ads_percentage_today}%)"
queries=$(printf "%'.f" $(GetJSONValue ${json} "dns_queries_today"))
blocked=$(printf "%'.f" $(GetJSONValue ${json} "ads_blocked_today"))
LC_NUMERIC=C percentage=$(printf "%0.2f\n" $(GetJSONValue ${json} "ads_percentage_today")) #2 decimal places
echo "Blocking: ${domains}"
echo "Queries: ${queries}"
echo "Pi-holed: ${blocked} (${percentage}%)"
sleep 5 sleep 5
done done