mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-15 10:43:55 +00:00
Let Chronometer query all data from FTL (no need for having the API). Fixes #1305
This commit is contained in:
parent
ae9182c92e
commit
b1a9793d94
1 changed files with 39 additions and 29 deletions
|
@ -8,30 +8,49 @@
|
||||||
# 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() {
|
||||||
|
@ -49,23 +68,14 @@ 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)
|
get_summary_data
|
||||||
|
echo "Blocking: ${domains_being_blocked}"
|
||||||
domains=$(printf "%'.f" $(GetJSONValue ${json} "domains_being_blocked")) #add commas in
|
echo "Queries: ${dns_queries_today}"
|
||||||
queries=$(printf "%'.f" $(GetJSONValue ${json} "dns_queries_today"))
|
echo "Pi-holed: ${ads_blocked_today} (${ads_percentage_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
|
||||||
|
|
Loading…
Reference in a new issue