From 99c3c57968fc36c048f47b888644dff5c053ae7f Mon Sep 17 00:00:00 2001 From: Tommy Huff Date: Mon, 24 Oct 2016 17:41:14 -0400 Subject: [PATCH] update chronometer --- advanced/Scripts/chronometer.sh | 226 +++++++++++++++++--------------- 1 file changed, 121 insertions(+), 105 deletions(-) diff --git a/advanced/Scripts/chronometer.sh b/advanced/Scripts/chronometer.sh index c0b93e78..b6fb6a49 100755 --- a/advanced/Scripts/chronometer.sh +++ b/advanced/Scripts/chronometer.sh @@ -12,137 +12,153 @@ #Functions############################################################################################################## -piLog="/var/log/pihole.log" -gravity="/etc/pihole/gravity.list" -today=$(date "+%b %e") - -CalcBlockedDomains() { - CheckIPv6 - 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 - else - blockedDomainsTotal="Err." - fi +#move to pihole +statsUpdateJSON() { + if [[ -z "${AdminLink}" ]] ; then + AdminLink="http://127.0.0.1/admin" + fi + local x=$(curl -s ${AdminLink}/api.php?summaryRaw) + #check if json is valid + if echo "${x}" | python -m json.tool > /dev/null ; then + echo "${x}" + else + echo "Error" + fi } -CalcQueriesToday() { - if [ -e "$piLog" ];then - queriesToday=$(cat "$piLog" | grep "$today" | awk '/query/ {print $6}' | wc -l) - else - queriesToday="Err." - fi +#move to pihole +statsBlockedDomains() { + if [[ -z "${json}" ]] ; then + json=$(statsUpdateJSON) + fi + if [[ "${json}" != "Error" ]] ; then + local x=$(echo "${json}" | python -c "import sys, json; print json.load(sys.stdin)['domains_being_blocked']") + echo ${x} + else + echo "Error" + fi } -CalcblockedToday() { - if [ -e "$piLog" ] && [ -e "$gravity" ];then - blockedToday=$(cat ${piLog} | awk '/\/etc\/pihole\/gravity.list/ && !/address/ {print $6}' | wc -l) - else - blockedToday="Err." - fi +#move to pihole +statsQueriesToday() { + if [[ -z "${json}" ]] ; then + json=$(statsUpdateJSON) + fi + if [[ "${json}" != "Error" ]] ; then + local x=$(echo "${json}" | python -c "import sys, json; print json.load(sys.stdin)['dns_queries_today']") + echo ${x} + else + echo "Error" + fi } -CalcPercentBlockedToday() { - if [ "$queriesToday" != "Err." ] && [ "$blockedToday" != "Err." ]; then - if [ "$queriesToday" != 0 ]; then #Fixes divide by zero error :) - #scale 2 rounds the number down, so we'll do scale 4 and then trim the last 2 zeros - percentBlockedToday=$(echo "scale=4; $blockedToday/$queriesToday*100" | bc) - percentBlockedToday=$(sed 's/.\{2\}$//' <<< "$percentBlockedToday") - else - percentBlockedToday=0 - fi - fi +#move to pihole +statsBlockedToday() { + if [[ -z "${json}" ]] ; then + json=$(statsUpdateJSON) + fi + if [[ "${json}" != "Error" ]] ; then + local x=$(echo "${json}" | python -c "import sys, json; print json.load(sys.stdin)['ads_blocked_today']") + echo ${x} + else + echo "Error" + fi } -CheckIPv6() { - piholeIPv6file="/etc/pihole/.useIPv6" - if [[ -f ${piholeIPv6file} ]];then - # If the file exists, then the user previously chose to use IPv6 in the automated installer - piholeIPv6=$(ip -6 route get 2001:4860:4860::8888 | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }') - fi +#move to pihole +statsPercentBlockedToday() { + if [[ -z "${json}" ]] ; then + json=$(statsUpdateJSON) + fi + if [[ "${json}" != "Error" ]] ; then + local x=$(echo "${json}" | python -c "import sys, json; print round(float(json.load(sys.stdin)['ads_percentage_today']), 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 + +#remove CIDR from IPv4 +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() { - for (( ; ; )) - do - clear - # Displays a colorful Pi-hole logo - echo " ___ _ _ _" - echo "| _ (_)___| |_ ___| |___" - echo "| _/ |___| ' \/ _ \ / -_)" - echo "|_| |_| |_||_\___/_\___|" - echo "" - echo " $(ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -d':' -f2)" - echo "" - uptime | cut -d' ' -f11- - #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."}' - echo "-------------------------------" - # 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;}' - - #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 - done + 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 + # Displays a colorful Pi-hole logo + echo " ___ _ _ _" + echo "| _ (_)___| |_ ___| |___" + echo "| _/ |___| ' \/ _ \ / -_)" + echo "|_| |_| |_||_\___/_\___|" + echo "" + center ${IPv4_address} + center ${IPv6_address} + echo "${load}" + echo "${uptime}" + echo "-------------------------------" + echo "Blocking: ${list}" + echo "Queries: ${hits}" + echo "Pi-holed: ${blocked} (${percent})%" + sleep 5 + done } -displayHelp() { - 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 - exit 1 + +function displayHelp(){ + echo "::: Displays stats about your piHole!" + echo ":::" + echo "::: Usage: sudo pihole -c [optional:-j]" + echo "::: Note: If no option is passed, then stats are displayed on screen, updated every 5 seconds" + echo ":::" + echo "::: Options:" + echo "::: -j, --json output stats as JSON formatted string" + echo "::: -h, --help display this help text" + exit 1 } if [[ $# = 0 ]]; then - normalChrono + normalChrono fi for var in "$@" do case "$var" in - "-j" | "--json" ) outputJSON;; + "-j" | "--json" ) statsUpdateJSON;; "-h" | "--help" ) displayHelp;; * ) exit 1;; esac