From cb8bfa06e7f2d2b866e793ebe2637ba63c701749 Mon Sep 17 00:00:00 2001 From: Tommy Huff Date: Fri, 21 Oct 2016 20:05:37 -0400 Subject: [PATCH] move functions to pihole as they could be useful other places --- advanced/Scripts/chronometer.sh | 6 ++-- pihole | 59 ++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/advanced/Scripts/chronometer.sh b/advanced/Scripts/chronometer.sh index 04b4ac17..cd669f2a 100755 --- a/advanced/Scripts/chronometer.sh +++ b/advanced/Scripts/chronometer.sh @@ -33,7 +33,7 @@ IPv4_address=$(echo "${IPv4_address}" | cut -d"/" -f1) CalcBlockedDomains() { if [ -e "${gravity}" ]; then - blockedDomainsTotal=$(wc -l "${gravityraw}" | awk '{print $1}') + blockedDomainsTotal=$(pihole stats list) else blockedDomainsTotal="Err." fi @@ -41,7 +41,7 @@ CalcBlockedDomains() { CalcQueriesToday() { if [ -e "${piLog}" ];then - queriesToday=$(cat "${piLog}" | grep "${today}" | cut -d" " -f5 | grep query | wc -l) + queriesToday=$(pihole stats hits) else queriesToday="Err." fi @@ -49,7 +49,7 @@ CalcQueriesToday() { CalcblockedToday() { if [ -e "${piLog}" ] && [ -e "${gravity}" ];then - blockedToday=$(grep "${gravity}" "${piLog}" | grep "${today}" | grep -v read | wc -l) + blockedToday=$(pihole stats blocked) else blockedToday="Err." fi diff --git a/pihole b/pihole index fa5a95f0..cc1dba77 100755 --- a/pihole +++ b/pihole @@ -154,6 +154,35 @@ piholeStatus() { fi } +statsBlockedDomains() { + local blockedDomainsTotal=$(wc -l /etc/pihole/list.preEventHorizon | awk '{print $1}') + echo ${blockedDomainsTotal} +} + +statsQueriesToday() { + local queriesToday=$(cat /var/log/pihole.log | cut -d" " -f5 | grep query | wc -l) + echo ${queriesToday} +} + +statsBlockedToday() { + local blockedToday=$(grep /etc/pihole/gravity.list /var/log/pihole.log | grep -v read | wc -l) + echo ${blockedToday} +} + +stats() { + shift + if [[ $1 == "list" ]] ; then + statsBlockedDomains + elif [[ $1 == "hits" ]] ; then + statsQueriesToday + elif [[ $1 == "blocked" ]] ; then + statsBlockedToday + fi +} + + + + helpFunc() { echo "::: Control all PiHole specific functions!" @@ -187,23 +216,23 @@ fi # Handle redirecting to specific functions based on arguments case "$1" in -"-w" | "whitelist" ) whitelistFunc "$@";; -"-b" | "blacklist" ) blacklistFunc "$@";; -"-d" | "debug" ) debugFunc;; -"-f" | "flush" ) flushFunc;; -"-up" | "updatePihole" ) updatePiholeFunc;; -"-r" | "reconfigure" ) reconfigurePiholeFunc;; -"-g" | "updateGravity" ) updateGravityFunc "$@";; -"-s" | "setupLCD" ) setupLCDFunction;; -"-c" | "chronometer" ) chronometerFunc "$@";; -"-h" | "help" ) helpFunc;; -"-v" | "version" ) versionFunc "$@";; -"-q" | "query" ) queryFunc "$@";; -"uninstall" ) uninstallFunc;; +"-w" | "whitelist" ) whitelistFunc "$@";; +"-b" | "blacklist" ) blacklistFunc "$@";; +"-d" | "debug" ) debugFunc;; +"-f" | "flush" ) flushFunc;; +"-up" | "updatePihole" ) updatePiholeFunc;; +"-r" | "reconfigure" ) reconfigurePiholeFunc;; +"-g" | "updateGravity" ) updateGravityFunc "$@";; +"-s" | "setupLCD" ) setupLCDFunction;; +"-c" | "chronometer" ) chronometerFunc "$@";; +"-h" | "help" ) helpFunc;; +"-v" | "version" ) versionFunc "$@";; +"-q" | "query" ) queryFunc "$@";; +"uninstall" ) uninstallFunc;; "enable" ) piholeEnable 1;; "disable" ) piholeEnable 0;; "status" ) piholeStatus "$2";; "restartdns" ) restartDNS;; - -* ) helpFunc;; +"stats" ) stats "$@";; +* ) helpFunc;; esac