Welcome to mirror list, hosted at ThFree Co, Russian Federation.

chronometer.sh « Scripts « advanced - github.com/pi-hole/pi-hole.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a28bb8689db1c2947d166ea49ddc9187b33c1a8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bash
# Pi-hole: A black hole for Internet advertisements
# (c) 2015, 2016 by Jacob Salmela
# Network-wide ad blocking via your Raspberry Pi
# http://pi-hole.net
# Calculates stats and displays to an LCD
#
# Pi-hole is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.


#Functions##############################################################################################################
piLog="/var/log/pihole.log"
gravity="/etc/pihole/gravity.list"

. /etc/pihole/setupVars.conf

CalcBlockedDomains() {
	if [ -e "${gravity}" ]; then
		# if BOTH IPV4 and IPV6 are in use, then we need to divide total domains by 2.
		if [[ -n "${IPV4_ADDRESS}" && -n "${IPV6_ADDRESS}" ]]; then
			blockedDomainsTotal=$(wc -l /etc/pihole/gravity.list | awk '{print $1/2}')
		else
			# only one is set.
			blockedDomainsTotal=$(wc -l /etc/pihole/gravity.list | awk '{print $1}')
		fi
	else
		blockedDomainsTotal="Err."
	fi
}

CalcQueriesToday() {
	if [ -e "${piLog}" ]; then
		queriesToday=$(awk '/query\[/ {print $6}' < "${piLog}" | wc -l)
	else
		queriesToday="Err."
	fi
}

CalcblockedToday() {
	if [ -e "${piLog}" ] && [ -e "${gravity}" ];then
		blockedToday=$(awk '/\/etc\/pihole\/gravity.list/ && !/address/ {print $6}' < "${piLog}" | wc -l)
	else
		blockedToday="Err."
	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
}

outputJSON() {
	CalcQueriesToday
	CalcblockedToday
	CalcPercentBlockedToday

	CalcBlockedDomains

	printf '{"domains_being_blocked":"%s","dns_queries_today":"%s","ads_blocked_today":"%s","ads_percentage_today":"%s"}\n' "$blockedDomainsTotal" "$queriesToday" "$blockedToday" "$percentBlockedToday"
}

normalChrono() {
	for (( ; ; )); do
		clear
		# Displays a colorful Pi-hole logo
		echo " ___ _     _        _"
		echo "| _ (_)___| |_  ___| |___"
		echo "|  _/ |___| ' \/ _ \ / -_)"
		echo "|_| |_|   |_||_\___/_\___|"
		echo ""
		echo "        ${IPV4_ADDRESS}"
		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}"
		echo "Queries:       ${queriesToday}" #same total calculation as dashboard
		echo "Pi-holed:      ${blockedToday} (${percentBlockedToday}%)"

		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 0
}

if [[ $# = 0 ]]; then
	normalChrono
fi

for var in "$@"; do
	case "$var" in
		"-j" | "--json"  ) outputJSON;;
		"-h" | "--help"  ) displayHelp;;
		*                ) exit 1;;
	esac
done