update chronometer.sh

This commit is contained in:
Tommy Huff 2016-10-21 16:28:31 -04:00
parent 8f9ff930b3
commit caf65bb54c

View file

@ -12,71 +12,69 @@
#Functions############################################################################################################## #Functions##############################################################################################################
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
piLog="/var/log/pihole.log" piLog="/var/log/pihole.log"
gravityraw="/etc/pihole/list.preEventHorizon"
gravity="/etc/pihole/gravity.list" gravity="/etc/pihole/gravity.list"
today=$(date "+%b %e") today=$(date "+%b %e")
IPv4_address=$(echo "${IPv4_address}" | cut -d"/" -f1)
CalcBlockedDomains() { CalcBlockedDomains() {
CheckIPv6 if [ -e "${gravity}" ]; then
if [ -e "$gravity" ]; then blockedDomainsTotal=$(wc -l "${gravityraw}" | awk '{print $1}')
#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 else
blockedDomainsTotal="Err." blockedDomainsTotal="Err."
fi fi
} }
CalcQueriesToday() { CalcQueriesToday() {
if [ -e "$piLog" ];then if [ -e "${piLog}" ];then
queriesToday=$(cat "$piLog" | grep "$today" | awk '/query/ {print $6}' | wc -l) queriesToday=$(cat "${piLog}" | grep "${today}" | cut -d" " -f5 | grep query | wc -l)
else else
queriesToday="Err." queriesToday="Err."
fi fi
} }
CalcblockedToday() { CalcblockedToday() {
if [ -e "$piLog" ] && [ -e "$gravity" ];then if [ -e "${piLog}" ] && [ -e "${gravity}" ];then
blockedToday=$(cat ${piLog} | awk '/\/etc\/pihole\/gravity.list/ && !/address/ {print $6}' | wc -l) blockedToday=$(grep "${gravity}" "${piLog}" | grep "${today}" | grep -v read | wc -l)
else else
blockedToday="Err." blockedToday="Err."
fi fi
} }
CalcPercentBlockedToday() { CalcPercentBlockedToday() {
if [ "$queriesToday" != "Err." ] && [ "$blockedToday" != "Err." ]; then if [ "${queriesToday}" != "Err." ] && [ "${blockedToday}" != "Err." ]; then
if [ "$queriesToday" != 0 ]; then #Fixes divide by zero error :) 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 #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=$(echo "scale=4; ${blockedToday}/${queriesToday}*100" | bc)
percentBlockedToday=$(sed 's/.\{2\}$//' <<< "$percentBlockedToday") percentBlockedToday=$(sed 's/.\{2\}$//' <<< "${percentBlockedToday}")
else else
percentBlockedToday=0 percentBlockedToday=0
fi fi
fi 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
}
outputJSON() { outputJSON() {
CalcQueriesToday CalcQueriesToday
CalcblockedToday CalcblockedToday
CalcPercentBlockedToday CalcPercentBlockedToday
CalcBlockedDomains CalcBlockedDomains
printf '{"domains_being_blocked":"%s","dns_queries_today":"%s","ads_blocked_today":"%s","ads_percentage_today":"%s"}\n' "$blockedDomainsTotal" "$queriesToday" "$blockedToday" "$percentBlockedToday" printf '{"domains_being_blocked":"%s","dns_queries_today":"%s","ads_blocked_today":"%s","ads_percentage_today":"%s"}\n' "${blockedDomainsTotal}" "${queriesToday}" "${blockedToday}" "${percentBlockedToday}"
} }
normalChrono() { normalChrono() {
@ -89,7 +87,7 @@ normalChrono() {
echo "| _/ |___| ' \/ _ \ / -_)" echo "| _/ |___| ' \/ _ \ / -_)"
echo "|_| |_| |_||_\___/_\___|" echo "|_| |_| |_||_\___/_\___|"
echo "" echo ""
echo " $(ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -d':' -f2)" echo " ${IPv4_address}"
echo "" echo ""
uptime | cut -d' ' -f11- uptime | cut -d' ' -f11-
#uptime -p #Doesn't work on all versions of uptime #uptime -p #Doesn't work on all versions of uptime
@ -111,11 +109,9 @@ normalChrono() {
CalcBlockedDomains CalcBlockedDomains
echo "Blocking: $blockedDomainsTotal" echo "Blocking: ${blockedDomainsTotal}"
#below commented line does not add up to todaysQueryCount echo "Queries: ${queriesToday}" #same total calculation as dashboard
#echo "Queries: $todaysQueryCountV4 / $todaysQueryCountV6" echo "Pi-holed: $blockedToday ($percentBlockedToday%)"
echo "Queries: $queriesToday" #same total calculation as dashboard
echo "Pi-holed: $blockedToday ($percentBlockedToday%)"
sleep 5 sleep 5
done done