-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpihole_data.sh
191 lines (177 loc) · 6.38 KB
/
pihole_data.sh
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env bash
set -e
pihole_server=localhost
CACHE_FILE=/var/log/zabbix/pihole-data.log
LOCK_FILE=/tmp/pihole-data.lock
run_pihole_query() {
cd "$(readlink -f "$(dirname "$0")")" || exit 9
# Lock
if [[ -e "$LOCK_FILE" ]]
then
echo "Already getting data from pihole" >&2
exit 2
fi
touch "$LOCK_FILE"
trap "rm -rf $LOCK_FILE" EXIT HUP INT QUIT PIPE TERM
local domains_blocked dns_queries_today ads_blocked_today ads_percentage_today
get_domainsblocked(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.domains_being_blocked')
domains_blocked=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_dnsqueriestoday(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.dns_queries_today')
dns_queries_today=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_adsblockedtoday(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.ads_blocked_today')
ads_blocked_today=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_adspercentagetoday(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.ads_percentage_today')
ads_percentage_today=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_uniquedomains(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.unique_domains')
unique_domains=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_queriesforwarded(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.queries_forwarded')
queries_forwarded=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_queriescached(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.queries_cached')
queries_cached=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_clientseverseen(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.clients_ever_seen')
clients_ever_seen=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_uniqueclients(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.unique_clients')
unique_clients=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_dnsqueriesalltypes(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.dns_queries_all_types')
dns_queries_all_types=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_replyNODATA(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.reply_NODATA')
reply_NODATA=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_replyNXDOMAIN(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.reply_NXDOMAIN')
reply_NXDOMAIN=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_replyCNAME(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.reply_CNAME')
reply_CNAME=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_replyIP(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.reply_IP')
reply_IP=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_privacylevel(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.privacy_level')
privacy_level=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_status(){
rawjson=$(curl -s http://$pihole_server/admin/api.php | jq '.status')
status=$(echo $rawjson | sed -e 's/\"//g' -e 's/\,//g')
}
get_domainsblocked
get_dnsqueriestoday
get_adsblockedtoday
get_adspercentagetoday
get_uniquedomains
get_queriesforwarded
get_queriescached
get_clientseverseen
get_uniqueclients
get_dnsqueriesalltypes
get_replyNODATA
get_replyNXDOMAIN
get_replyCNAME
get_replyIP
get_privacylevel
get_status
{
echo "Domains Blocked: $domains_blocked"
echo "DNS Queries Today: $dns_queries_today"
echo "Ads Blocked Today: $ads_blocked_today"
echo "Ads Percentage Today: $ads_percentage_today"
echo "Unique Domains: $unique_domains"
echo "Queries Forwarded: $queries_forwarded"
echo "Queries Cached: $queries_cached"
echo "Clients Ever Seen: $clients_ever_seen"
echo "Unique Clients: $unique_clients"
echo "DNS Queries All Types: $dns_queries_all_types"
echo "Reply NODATA: $reply_NODATA"
echo "Reply NXDOMAIN: $reply_NXDOMAIN"
echo "Reply CNAME: $reply_CNAME"
echo "Reply IP: $reply_IP"
echo "Privacy Level: $privacy_level"
echo "Status: $status"
} > "$CACHE_FILE"
# Make sure to remove the lock file (may be redundant)
rm -rf "$LOCK_FILE"
}
case "$1" in
-c|--cached)
cat "$CACHE_FILE"
;;
-do|--domains)
awk '/Domains Blocked/ { print $3 }' "$CACHE_FILE"
;;
-dn|--dnsQ)
awk '/DNS Queries Today/ { print $4 }' "$CACHE_FILE"
;;
-ab|--ads-blocked)
awk '/Ads Blocked Today/ { print $4 }' "$CACHE_FILE"
;;
-ap|--ads-percentage)
awk '/Ads Percentage Today/ { print $4 }' "$CACHE_FILE"
;;
-ud|--unique_domains)
awk '/Unique Domains/ { print $3 }' "$CACHE_FILE"
;;
-qf|--queries_forwarded)
awk '/Queries Forwarded/ { print $3 }' "$CACHE_FILE"
;;
-qc|--queries_cached)
awk '/Queries Cached/ { print $3 }' "$CACHE_FILE"
;;
-ces|--clients_ever_seen)
awk '/Clients Ever Seen/ { print $4 }' "$CACHE_FILE"
;;
-uc|--unique_clients)
awk '/Unique Clients/ { print $3 }' "$CACHE_FILE"
;;
-dna|--dns_queries_all_types)
awk '/DNS Queries All Types/ { print $5 }' "$CACHE_FILE"
;;
-rna|--reply_NODATA)
awk '/Reply NODATA/ { print $3 }' "$CACHE_FILE"
;;
-rnx|--reply_NXDOMAIN)
awk '/Reply NXDOMAIN/ { print $3 }' "$CACHE_FILE"
;;
-rcn|--reply_CNAME)
awk '/Reply CNAME/ { print $3 }' "$CACHE_FILE"
;;
-rip|--reply_IP)
awk '/Reply IP/ { print $3 }' "$CACHE_FILE"
;;
-pl|--privacy_level)
awk '/Privacy Level/ { print $3 }' "$CACHE_FILE"
;;
-s|--status)
awk '/Status/ { print $2 }' "$CACHE_FILE"
;;
-f|--force)
rm -rf "$LOCK_FILE"
run_pihole_query
;;
*)
run_pihole_query
;;
esac