Parse with PHP

This commit is contained in:
Tommy Huff 2016-10-25 18:55:23 -04:00
parent 4839953328
commit 56bbf3b05c
3 changed files with 147 additions and 108 deletions

View file

@ -12,136 +12,154 @@
#Functions############################################################################################################## #Functions##############################################################################################################
piLog="/var/log/pihole.log"
gravity="/etc/pihole/gravity.list"
today=$(date "+%b %e") #move to pihole
CalcBlockedDomains() { if [ ! -f "/opt/pihole/jq" ] ; then
CheckIPv6 curl -s http://stedolan.github.io/jq/download/linux64/jq -o /opt/pihole/jq
if [ -e "${gravity}" ]; then chmod 755 /opt/pihole/jq
#Are we IPV6 or IPV4? fi
if [[ -n ${piholeIPv6} ]]; then
#We are IPV6 statsUpdateJSON() {
blockedDomainsTotal=$(wc -l /etc/pihole/gravity.list | awk '{print $1/2}') if [[ -z "${AdminLink}" ]] ; then
else AdminLink="http://127.0.0.1/admin"
#We are IPV4 fi
blockedDomainsTotal=$(wc -l /etc/pihole/gravity.list | awk '{print $1}') local x=$(curl -s ${AdminLink}/api.php?summaryRaw)
fi echo "${x}"
else
blockedDomainsTotal="Err."
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
else json=$(statsUpdateJSON)
queriesToday="Err." fi
fi if [[ "${json}" != "Error" ]] ; then
local x=$(php /opt/pihole/json.php "${json}" "domains_being_blocked")
echo ${x}
else
echo "Error"
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
else json=$(statsUpdateJSON)
blockedToday="Err." fi
fi if [[ "${json}" != "Error" ]] ; then
local x=$(php /opt/pihole/json.php "${json}" "dns_queries_today")
echo ${x}
else
echo "Error"
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
else local x=$(php /opt/pihole/json.php "${json}" "ads_blocked_today")
percentBlockedToday=0 echo ${x}
fi else
fi echo "Error"
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
fi if [[ "${json}" != "Error" ]] ; then
local x=$(php /opt/pihole/json.php "${json}" "ads_percentage_today"| xargs printf "%.*f\n" 2)
echo ${x}
else
echo "Error"
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 (( ; ; ))
clear do
# Displays a colorful Pi-hole logo ## prepare all lines before clear to remove flashing
echo " ___ _ _ _" json=$(statsUpdateJSON)
echo "| _ (_)___| |_ ___| |___" load=$(uptime | cut -d' ' -f11-)
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 "|_| |_| |_||_\___/_\___|" list=$(statsBlockedDomains)
echo "" hits=$(statsQueriesToday)
echo " $(ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -d':' -f2)" blocked=$(statsBlockedToday)
echo "" percent=$(statsPercentBlockedToday)
uptime | cut -d' ' -f11- #for moving functions to pihole
#uptime -p #Doesn't work on all versions of uptime # list=$(pihole stats list)
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."}' # hits=$(pihole stats hits)
echo "-------------------------------" # blocked=$(pihole stats blocked)
# Uncomment to continually read the log file and display the current domain being blocked # percent=$(pihole stats percent)
#tail -f /var/log/pihole.log | awk '/\/etc\/pihole\/gravity.list/ {if ($7 != "address" && $7 != "name" && $7 != "/etc/pihole/gravity.list") print $7; else;}' clear
# Displays a colorful Pi-hole logo
#uncomment next 4 lines to use original query count calculation echo " ___ _ _ _"
#today=$(date "+%b %e") echo "| _ (_)___| |_ ___| |___"
#todaysQueryCount=$(cat /var/log/pihole.log | grep "$today" | awk '/query/ {print $7}' | wc -l) echo "| _/ |___| ' \/ _ \ / -_)"
#todaysQueryCountV4=$(cat /var/log/pihole.log | grep "$today" | awk '/query/ && /\[A\]/ {print $7}' | wc -l) echo "|_| |_| |_||_\___/_\___|"
#todaysQueryCountV6=$(cat /var/log/pihole.log | grep "$today" | awk '/query/ && /\[AAAA\]/ {print $7}' | wc -l) echo ""
center ${IPv4_address}
center ${IPv6_address}
CalcQueriesToday echo "${load}"
CalcblockedToday echo "${uptime}"
CalcPercentBlockedToday echo "-------------------------------"
echo "Blocking: ${list}"
CalcBlockedDomains echo "Queries: ${hits}"
echo "Pi-holed: ${blocked} (${percent})%"
echo "Blocking: ${blockedDomainsTotal}" sleep 5
#below commented line does not add up to todaysQueryCount done
#echo "Queries: $todaysQueryCountV4 / $todaysQueryCountV6"
echo "Queries: ${queriesToday}" #same total calculation as dashboard
echo "Pi-holed: ${blockedToday} (${percentBlockedToday}%)"
sleep 5
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
} }
if [[ $# = 0 ]]; then if [[ $# = 0 ]]; then
normalChrono normalChrono
fi fi
for var in "$@"; do for var in "$@"
case "$var" in do
"-j" | "--json" ) outputJSON;; case "$var" in
"-h" | "--help" ) displayHelp;; "-j" | "--json" ) statsUpdateJSON;;
* ) exit 1;; "-h" | "--help" ) displayHelp;;
esac * ) exit 1;;
esac
done done

20
advanced/Scripts/json.php Normal file
View file

@ -0,0 +1,20 @@
<?php
if ((isset($argv[1])) && (isset($argv[2]))) {
$json="$argv[1]";
$key="$argv[2]";
$result=json_decode($json, true);
if (is_array($result)) {
if (array_key_exists($key, $result)) {
echo "$result[$key]";
} else {
//key dosnt exist
die("Error KEY");
}
} else {
//invalid json
die("Error JSON");
}
} else {
die("Usage: php json.php \"JSON DATA HERE\" \"Key\"");
}
?>

View file

@ -559,6 +559,7 @@ installScripts() {
install -o "${USER}" -Dm755 -t /opt/pihole/ gravity.sh install -o "${USER}" -Dm755 -t /opt/pihole/ gravity.sh
install -o "${USER}" -Dm755 -t /opt/pihole/ ./advanced/Scripts/*.sh install -o "${USER}" -Dm755 -t /opt/pihole/ ./advanced/Scripts/*.sh
install -o "${USER}" -Dm755 -t /opt/pihole/ ./advanced/Scripts/*.php
install -o "${USER}" -Dm755 -t /usr/local/bin/ pihole install -o "${USER}" -Dm755 -t /usr/local/bin/ pihole
install -Dm644 ./advanced/bash-completion/pihole /etc/bash_completion.d/pihole install -Dm644 ./advanced/bash-completion/pihole /etc/bash_completion.d/pihole