Use Python to parse JSON

This commit is contained in:
Tommy Huff 2016-10-25 18:41:02 -04:00
parent 4839953328
commit 189c87dfb5
2 changed files with 127 additions and 110 deletions

View file

@ -12,75 +12,112 @@
#Functions############################################################################################################## #Functions##############################################################################################################
piLog="/var/log/pihole.log"
gravity="/etc/pihole/gravity.list"
today=$(date "+%b %e") #move to pihole
statsUpdateJSON() {
CalcBlockedDomains() { if [[ -z "${AdminLink}" ]] ; then
CheckIPv6 AdminLink="http://127.0.0.1/admin"
if [ -e "${gravity}" ]; then
#Are we IPV6 or IPV4?
if [[ -n ${piholeIPv6} ]]; then
#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 fi
local x=$(curl -s ${AdminLink}/api.php?summaryRaw)
#check if json is valid
if python -c "import sys, json;json.loads('${x}')" ; then
echo "${x}"
else else
blockedDomainsTotal="Err." echo "Error"
fi fi
} }
CalcQueriesToday() { #move to pihole
if [ -e "${piLog}" ]; then statsBlockedDomains() {
queriesToday=$(cat "${piLog}" | grep "${today}" | awk '/query/ {print $6}' | wc -l) if [[ -z "${json}" ]] ; then
json=$(statsUpdateJSON)
fi
if [[ "${json}" != "Error" ]] ; then
local x=$(python -c "import sys, json;print(json.loads('${json}')['domains_being_blocked'])")
echo ${x}
else else
queriesToday="Err." echo "Error"
fi fi
} }
CalcblockedToday() { #move to pihole
if [ -e "${piLog}" ] && [ -e "${gravity}" ];then statsQueriesToday() {
blockedToday=$(cat ${piLog} | awk '/\/etc\/pihole\/gravity.list/ && !/address/ {print $6}' | wc -l) if [[ -z "${json}" ]] ; then
json=$(statsUpdateJSON)
fi
if [[ "${json}" != "Error" ]] ; then
local x=$(python -c "import sys, json;print(json.loads('${json}')['dns_queries_today'])")
echo ${x}
else else
blockedToday="Err." echo "Error"
fi fi
} }
CalcPercentBlockedToday() { #move to pihole
if [ "${queriesToday}" != "Err." ] && [ "${blockedToday}" != "Err." ]; then statsBlockedToday() {
if [ "${queriesToday}" != 0 ]; then #Fixes divide by zero error :) if [[ -z "${json}" ]] ; then
#scale 2 rounds the number down, so we'll do scale 4 and then trim the last 2 zeros json=$(statsUpdateJSON)
percentBlockedToday=$(echo "scale=4; ${blockedToday}/${queriesToday}*100" | bc) fi
percentBlockedToday=$(sed 's/.\{2\}$//' <<< "${percentBlockedToday}") if [[ "${json}" != "Error" ]] ; then
local x=$(python -c "import sys, json;print(json.loads('${json}')['ads_blocked_today'])")
echo ${x}
else else
percentBlockedToday=0 echo "Error"
fi
fi fi
} }
CheckIPv6() { #move to pihole
piholeIPv6file="/etc/pihole/.useIPv6" statsPercentBlockedToday() {
if [[ -f ${piholeIPv6file} ]];then if [[ -z "${json}" ]] ; then
# If the file exists, then the user previously chose to use IPv6 in the automated installer json=$(statsUpdateJSON)
piholeIPv6=$(ip -6 route get 2001:4860:4860::8888 | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }') fi
if [[ "${json}" != "Error" ]] ; then
local x=$(python -c "import sys, json;print(round(float(json.loads('${json}')['ads_percentage_today']), 2))")
echo ${x}
else
echo "Error"
fi fi
} }
outputJSON() {
CalcQueriesToday
CalcblockedToday
CalcPercentBlockedToday
CalcBlockedDomains #start script
printf '{"domains_being_blocked":"%s","dns_queries_today":"%s","ads_blocked_today":"%s","ads_percentage_today":"%s"}\n' "$blockedDomainsTotal" "$queriesToday" "$blockedToday" "$percentBlockedToday" setupVars="/etc/pihole/setupVars.conf"
if [[ -f "${setupVars}" ]] ; then
. "${setupVars}"
else
echo "::: WARNING: /etc/pihole/setupVars.conf missing. Possible installation failure."
echo "::: Please run 'pihole -r', and choose the 'reconfigure' option to reconfigure."
exit 1
fi
IPv4_address=${IPv4_address%/*}
center(){
cols=$(tput cols)
length=${#1}
center=$(expr $cols / 2)
halfstring=$(expr $length / 2 )
pad=$(expr $center + $halfstring)
printf "%${pad}s\n" "$1"
} }
normalChrono() { normalChrono() {
for (( ; ; )); do for (( ; ; ))
do
## prepare all lines before clear to remove flashing
json=$(statsUpdateJSON)
load=$(uptime | cut -d' ' -f11-)
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."}')
list=$(statsBlockedDomains)
hits=$(statsQueriesToday)
blocked=$(statsBlockedToday)
percent=$(statsPercentBlockedToday)
#for moving functions to pihole
# list=$(pihole stats list)
# hits=$(pihole stats hits)
# blocked=$(pihole stats blocked)
# percent=$(pihole stats percent)
clear clear
# Displays a colorful Pi-hole logo # Displays a colorful Pi-hole logo
echo " ___ _ _ _" echo " ___ _ _ _"
@ -88,49 +125,28 @@ normalChrono() {
echo "| _/ |___| ' \/ _ \ / -_)" echo "| _/ |___| ' \/ _ \ / -_)"
echo "|_| |_| |_||_\___/_\___|" echo "|_| |_| |_||_\___/_\___|"
echo "" echo ""
echo " $(ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -d':' -f2)" center ${IPv4_address}
echo "" center ${IPv6_address}
uptime | cut -d' ' -f11- echo "${load}"
#uptime -p #Doesn't work on all versions of uptime echo "${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."}'
echo "-------------------------------" echo "-------------------------------"
# Uncomment to continually read the log file and display the current domain being blocked echo "Blocking: ${list}"
#tail -f /var/log/pihole.log | awk '/\/etc\/pihole\/gravity.list/ {if ($7 != "address" && $7 != "name" && $7 != "/etc/pihole/gravity.list") print $7; else;}' echo "Queries: ${hits}"
echo "Pi-holed: ${blocked} (${percent})%"
#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)
CalcQueriesToday
CalcblockedToday
CalcPercentBlockedToday
CalcBlockedDomains
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}%)"
sleep 5 sleep 5
done done
} }
displayHelp() {
cat << EOM function displayHelp(){
::: Displays stats about your piHole! echo "::: Displays stats about your piHole!"
::: echo ":::"
::: Usage: sudo pihole -c [optional:-j] echo "::: Usage: sudo pihole -c [optional:-j]"
::: Note: If no option is passed, then stats are displayed on screen, updated every 5 seconds echo "::: Note: If no option is passed, then stats are displayed on screen, updated every 5 seconds"
::: echo ":::"
::: Options: echo "::: Options:"
::: -j, --json output stats as JSON formatted string echo "::: -j, --json output stats as JSON formatted string"
::: -h, --help display this help text echo "::: -h, --help display this help text"
EOM
exit 1 exit 1
} }
@ -138,9 +154,10 @@ if [[ $# = 0 ]]; then
normalChrono normalChrono
fi fi
for var in "$@"; do for var in "$@"
do
case "$var" in case "$var" in
"-j" | "--json" ) outputJSON;; "-j" | "--json" ) statsUpdateJSON;;
"-h" | "--help" ) displayHelp;; "-h" | "--help" ) displayHelp;;
* ) exit 1;; * ) exit 1;;
esac esac

View file

@ -83,7 +83,7 @@ if [ -x "$(command -v apt-get)" ]; then
# grep -c will return 1 retVal on 0 matches, block this throwing the set -e with an OR TRUE # grep -c will return 1 retVal on 0 matches, block this throwing the set -e with an OR TRUE
PKG_COUNT="${PKG_MANAGER} -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true" PKG_COUNT="${PKG_MANAGER} -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true"
INSTALLER_DEPS=( apt-utils whiptail git dhcpcd5) INSTALLER_DEPS=( apt-utils whiptail git dhcpcd5)
PIHOLE_DEPS=( dnsutils bc dnsmasq lighttpd ${phpVer}-common ${phpVer}-cgi curl unzip wget sudo netcat cron iproute2 ) PIHOLE_DEPS=( dnsutils bc dnsmasq lighttpd ${phpVer}-common ${phpVer}-cgi curl unzip wget sudo netcat cron iproute2 python)
LIGHTTPD_USER="www-data" LIGHTTPD_USER="www-data"
LIGHTTPD_GROUP="www-data" LIGHTTPD_GROUP="www-data"
LIGHTTPD_CFG="lighttpd.conf.debian" LIGHTTPD_CFG="lighttpd.conf.debian"