Page 1 of 1

PiHole Script

Posted: Tuesday 13 June 2017 19:35
by TapNL
I created a script to add output of the PiHole api to Domoticz

Link to the script on the wiki: https://www.domoticz.com/wiki/Display_o ... PiHole_api

Link to the website of the PiHole project: https://pi-hole.net

Re: PiHole Script

Posted: Tuesday 13 June 2017 20:02
by EddyG
Hi,

Nice interface.
The output is already JSON, so a little more readable solution might be:
INPUT=`curl -s 'http://192.168.2.159/admin/api.php'`
DOMAINSBLOCKED=`echo $INPUT | jq -r '.domains_being_blocked'`
.....

or even better switch to lua.

In the crontab full path should be used, so /home/pi/......

Re: PiHole Script

Posted: Tuesday 13 June 2017 20:55
by Derik
mmm
My pihole fucku.. my xu4....
Something with rights...
My hdd is going straid to hell....
ScreenShot040.jpg
ScreenShot040.jpg (71.29 KiB) Viewed 8901 times
from the time i did install pihole....
Now i try to get my space back... :-)

Run pihole on a old rpi... works great

Re: PiHole Script

Posted: Wednesday 14 June 2017 6:53
by jjnj
Nice script, thanks a lot for sharing!

Verstuurd vanaf mijn LG-H870 met Tapatalk

Re: PiHole Script

Posted: Thursday 15 June 2017 10:28
by EdwinK
Thanks, too bad Pi-Hole was making my system unresponsive. :(

Re: PiHole Script

Posted: Thursday 15 June 2017 14:25
by qwerk
EdKo66 wrote:Thanks, too bad Pi-Hole was making my system unresponsive. :(
pi-hole did the same to my raspberry. where I run domoticz.
I now have a docker version on my synology, that runs great, stable and on low resources.

Re: PiHole Script

Posted: Thursday 15 June 2017 16:21
by MsbS
I keep PiHole, Domoticz and Squeezebox (Logitech Media Server) on one Pi2 and it works just fine. Though my Domoticz only has a few switches so far.

Re: PiHole Script

Posted: Sunday 18 June 2017 20:25
by simonrg
@TapNL thanks for introducing me to Pi-Hole - really useful tool.

I like to keep things inside Domoticz, so I have created a Lua script instead and used Customer Sensors, so Domoticz will create graphs similar to the Pi-Hole dashboard display.

Lua is quite good for this sort of task as the combination of a json library and tables, means that the real work is done in 4 lines:

Code: Select all

for JsonField,DeviceName in pairs(PiHoleFields) do
    i=i+1
    commandArray[i] = {['UpdateDevice'] = otherdevices_idx[DeviceName] .. "|0|" .. decoded_response[JsonField]}
  end
Needless to say the whole code is a bit longer:

Code: Select all

--~/domoticz/scripts/lua/script_time_pihole.lua
commandArray = {}
-- =============================
-- Query pi-hole and make data available in Domoticz
-- =============================
--Uservariables
PiHoleIP="192.168.89.251" -- ip of your Pi-Hole machine
-- PiHoleFields is a table of the json keys and Domoticz device names, which is used to retrieve the data
-- from the json returned by Pi-Hole and upload it to the named appropriate Domoticz device.
-- Basically in PiHoleFields table - json_field = "Domoticz Device Name"
-- Before installing the script create 7 Domoticz dummy device virtual sensors, all customer sensors, 
-- except ads_percentage_today which is percentage custom sensor.
PiHoleFields={domains_being_blocked="Domains Blocked",dns_queries_today="DNS Queries",ads_blocked_today="Ads Blocked",ads_percentage_today="Ads Percent",unique_domains="Unique Domains",queries_forwarded="Queries Forwarded",queries_cached="Queries Cached"}
-- Set how often the date is checked by:
minutesbetween = 5
time = os.date("*t")
-- Check every hour
--if time.min == ReportMinute then
--if time.hour == ReportHour and time.min == ReportMinute then
if (((time.min)%minutesbetween) == 0) then
  -- Only load libaries now
  io = require('io')
  http = require('socket.http')
  https = require "ssl.https";
  JSON = require('JSON')
  -- Retreive Pi-Hole Information
  PiHoleURL = "http://"..PiHoleIP.."/admin/api.php"
  jresponse, status = http.request(PiHoleURL)
  decoded_response = JSON:decode(jresponse)
  i = 0
  for JsonField,DeviceName in pairs(PiHoleFields) do
    i=i+1
    commandArray[i] = {['UpdateDevice'] = otherdevices_idx[DeviceName] .. "|0|" .. decoded_response[JsonField]}
  end
end
    
--LUA default
return commandArray
And you need to have socket and json libraries installed as described here for a Raspberry Pi - http://www.domoticz.com/wiki/Upload_ene ... et_library and http://dkolf.de/src/dkjson-lua.fsl/home.

Re: PiHole Script

Posted: Monday 19 June 2017 23:34
by TapNL
@simonrg: Thx for lua script - also a possibility, not a LUA jedi - not even a bash jedi.
Nice to see, that it is interesting for people.

Re: PiHole Script

Posted: Wednesday 21 June 2017 17:15
by jjnj
The script is running, however nothing changes in Domoticz:

Code: Select all

pi@raspberrypi:~ $ sudo bash ~/Scripts/pihole.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   204    0   204    0     0   5020      0 --:--:-- --:--:-- --:--:--  5100
: invalid number/pihole.sh: line 21: printf: 110311
: invalid number/pihole.sh: line 22: printf: 29201
: invalid number/pihole.sh: line 23: printf: 6832
: invalid number/pihole.sh: line 24: printf: 1563
: invalid number/pihole.sh: line 25: printf: 14593
My script is:

Code: Select all

#!/bin/bash

# Settings
PIHOLE_IP="192.168.178.97"  # PiHole IP
DOMO_IP="192.168.xx.xx"    # Domoticz IP
DOMO_PORT="xx" 	  # Domo port
DOMAINSBLOCKED_IDX='1278'
DNSQUERIESTODAY_IDX='1279'
ADSBLOCKEDTODAY_IDX='1280'
ADSPERCENTTODAY_IDX='1283'
UNIQUEDOMAINS_IDX='1281'
QUERIESFORWARDED_IDX='1282'

# Get data from Pi_Hole API
INPUT=$(curl "http://$PIHOLE_IP/admin/api.php")
DOMAINSBLOCKED=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $2}')
DNSQUERIESTODAY=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $4}')
ADSBLOCKEDTODAY=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $6}')
ADSPERCENTTODAY=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $8}')
UNIQUEDOMAINS=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $10}')
QUERIESFORWARDED=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $12}')

#Make data more readable
DOMAINSBLOCKED=$(printf "%'d" "$DOMAINSBLOCKED")
DNSQUERIESTODAY=$(printf "%'d" "$DNSQUERIESTODAY")
ADSBLOCKEDTODAY=$(printf "%'d" "$ADSBLOCKEDTODAY")
UNIQUEDOMAINS=$(printf "%'d" "$UNIQUEDOMAINS")
QUERIESFORWARDED=$(printf "%'d" "$QUERIESFORWARDED")

# Load data in domoticz
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$DOMAINSBLOCKED_IDX&nvalue=0&svalue=$DOMAINSBLOCKED"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$DNSQUERIESTODAY_IDX&nvalue=0&svalue=$DNSQUERIESTODAY"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$ADSBLOCKEDTODAY_IDX&nvalue=0&svalue=$ADSBLOCKEDTODAY"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$ADSPERCENTTODAY_IDX&nvalue=0&svalue=$ADSPERCENTTODAY"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UNIQUEDOMAINS_IDX&nvalue=0&svalue=$UNIQUEDOMAINS"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$QUERIESFORWARDED_IDX&nvalue=0&svalue=$QUERIESFORWARDED"
This is the output of the pihole api call

Code: Select all

{"domains_being_blocked":110311,"dns_queries_today":29339,"ads_blocked_today":6871,"ads_percentage_today":23.41934,"unique_domains":1564,"queries_forwarded":14673,"queries_cached":7795,"unique_clients":5}
What is going wrong?
Just did an update to the latest pihole version 3.1, maybe that has some effect?

Re: PiHole Script

Posted: Monday 03 July 2017 8:53
by simonrg
jjnj wrote:What is going wrong?
Just did an update to the latest pihole version 3.1, maybe that has some effect?
Did you find what your problem was?
There appears to be nothing wrong with the script it runs fine on my machine.
However, I would suggest removing the printf statements and changing your Domoticz devices to Custom Sensors, so that Domoticz will do the formatting for you.
So script would be simply:

Code: Select all

#!/bin/bash

# Settings
PIHOLE_IP="192.168.89.251"  # PiHole IP
#DOMO_IP="192.168.xx.xx"    # Domoticz IP
#DOMO_PORT="xx"      # Domo port
DOMAINSBLOCKED_IDX='1278'
DNSQUERIESTODAY_IDX='1279'
ADSBLOCKEDTODAY_IDX='1280'
ADSPERCENTTODAY_IDX='1283'
UNIQUEDOMAINS_IDX='1281'
QUERIESFORWARDED_IDX='1282'

# Get data from Pi_Hole API
INPUT=$(curl "http://$PIHOLE_IP/admin/api.php")
INPUT='{"domains_being_blocked":110311,"dns_queries_today":29339,"ads_blocked_today":6871,"ads_percentage_today":23.41934,"unique_domains":1564,"queries_forwarded":14673,"queries_cached":7795,"unique_clients":5}'
DOMAINSBLOCKED=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $2}')
DNSQUERIESTODAY=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $4}')
ADSBLOCKEDTODAY=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $6}')
ADSPERCENTTODAY=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $8}')
UNIQUEDOMAINS=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $10}')
QUERIESFORWARDED=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $12}')

#Make data more readable
DOMAINSBLOCKED=$(printf "%'d" "$DOMAINSBLOCKED")
DNSQUERIESTODAY=$(printf "%'d" "$DNSQUERIESTODAY")
ADSBLOCKEDTODAY=$(printf "%'d" "$ADSBLOCKEDTODAY")
UNIQUEDOMAINS=$(printf "%'d" "$UNIQUEDOMAINS")
QUERIESFORWARDED=$(printf "%'d" "$QUERIESFORWARDED")

# Load data in domoticz
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$DOMAINSBLOCKED_IDX&nvalue=0&svalue=$DOMAINSBLOCKED"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$DNSQUERIESTODAY_IDX&nvalue=0&svalue=$DNSQUERIESTODAY"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$ADSBLOCKEDTODAY_IDX&nvalue=0&svalue=$ADSBLOCKEDTODAY"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$ADSPERCENTTODAY_IDX&nvalue=0&svalue=$ADSPERCENTTODAY"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UNIQUEDOMAINS_IDX&nvalue=0&svalue=$UNIQUEDOMAINS"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$QUERIESFORWARDED_IDX&nvalue=0&svalue=$QUERIESFORWARDED"


Re: PiHole Script

Posted: Monday 03 July 2017 11:08
by jjnj
simonrg wrote:
jjnj wrote:What is going wrong?
Just did an update to the latest pihole version 3.1, maybe that has some effect?
Did you find what your problem was?
There appears to be nothing wrong with the script it runs fine on my machine.
However, I would suggest removing the printf statements and changing your Domoticz devices to Custom Sensors, so that Domoticz will do the formatting for you.
So script would be simply:

Code: Select all

#!/bin/bash

# Settings
PIHOLE_IP="192.168.89.251"  # PiHole IP
#DOMO_IP="192.168.xx.xx"    # Domoticz IP
#DOMO_PORT="xx"      # Domo port
DOMAINSBLOCKED_IDX='1278'
DNSQUERIESTODAY_IDX='1279'
ADSBLOCKEDTODAY_IDX='1280'
ADSPERCENTTODAY_IDX='1283'
UNIQUEDOMAINS_IDX='1281'
QUERIESFORWARDED_IDX='1282'

# Get data from Pi_Hole API
INPUT=$(curl "http://$PIHOLE_IP/admin/api.php")
INPUT='{"domains_being_blocked":110311,"dns_queries_today":29339,"ads_blocked_today":6871,"ads_percentage_today":23.41934,"unique_domains":1564,"queries_forwarded":14673,"queries_cached":7795,"unique_clients":5}'
DOMAINSBLOCKED=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $2}')
DNSQUERIESTODAY=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $4}')
ADSBLOCKEDTODAY=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $6}')
ADSPERCENTTODAY=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $8}')
UNIQUEDOMAINS=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $10}')
QUERIESFORWARDED=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $12}')

#Make data more readable
DOMAINSBLOCKED=$(printf "%'d" "$DOMAINSBLOCKED")
DNSQUERIESTODAY=$(printf "%'d" "$DNSQUERIESTODAY")
ADSBLOCKEDTODAY=$(printf "%'d" "$ADSBLOCKEDTODAY")
UNIQUEDOMAINS=$(printf "%'d" "$UNIQUEDOMAINS")
QUERIESFORWARDED=$(printf "%'d" "$QUERIESFORWARDED")

# Load data in domoticz
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$DOMAINSBLOCKED_IDX&nvalue=0&svalue=$DOMAINSBLOCKED"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$DNSQUERIESTODAY_IDX&nvalue=0&svalue=$DNSQUERIESTODAY"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$ADSBLOCKEDTODAY_IDX&nvalue=0&svalue=$ADSBLOCKEDTODAY"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$ADSPERCENTTODAY_IDX&nvalue=0&svalue=$ADSPERCENTTODAY"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UNIQUEDOMAINS_IDX&nvalue=0&svalue=$UNIQUEDOMAINS"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$QUERIESFORWARDED_IDX&nvalue=0&svalue=$QUERIESFORWARDED"

Thanks, i will try when back home

Verstuurd vanaf mijn LG-H870 met Tapatalk

Re: PiHole Script

Posted: Monday 17 July 2017 20:38
by jjnj
Sorry for the late reply, the updated script worked for me!

Re: PiHole Script

Posted: Wednesday 27 September 2023 19:31
by Chris12
I updated the script so it works with the latest Pi-Hole version (incl API Token), I thaught maybe somebody else needs it.
Below the script with some example data (fake API token).

I added the script into my synology, so it can run every 5min.

Code: Select all

#!/bin/bash

# Settings
PIHOLE_IP="192.168.1.xx"  # PiHole IP
DOMO_IP="192.168.1.xx"    # Domoticz IP
DOMO_PORT="80xx"          # Domo port
API_TOKEN="e0922ed316ff63fa00304e6aa8547d43e2d1ff55ab7a07caa2662f34b6222f0e"  # PiHole API Token, found here: pihole->settings->API->show token
DOMAINSBLOCKED_IDX='1444'
DNSQUERIESTODAY_IDX='1445'
ADSBLOCKEDTODAY_IDX='1446'
ADSPERCENTTODAY_IDX='1447'
UNIQUEDOMAINS_IDX='1449'
QUERIESFORWARDED_IDX='1450'
QUERIESCACHED_IDX='1451'
CLIENTSEVERSEEN_IDX='1452'
UNIQUECLIENTS_IDX='1453'

# Get data from Pi_Hole API
INPUT=$(curl "http://$PIHOLE_IP/admin/api.php?summaryRaw&auth=$API_TOKEN")
domains_being_blocked=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $2}')
dns_queries_today=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $4}')
ads_blocked_today=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $6}')
ads_percentage_today=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $8}')
unique_domains=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $10}')
queries_forwarded=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $12}')
queries_cached=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $14}')
clients_ever_seen=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $16}')
unique_clients=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $18}')
dns_queries_all_types=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $20}')


#Make data more readable
domains_being_blocked=$(printf "%'d" "$domains_being_blocked")
dns_queries_today=$(printf "%'d" "$dns_queries_today")
ads_blocked_today=$(printf "%'d" "$ads_blocked_today")
ads_percentage_today$(printf "%'d" "$ads_percentage_today")
unique_domains=$(printf "%'d" "$unique_domains")
queries_forwarded=$(printf "%'d" "$queries_forwarded")
queries_cached=$(printf "%'d" "$queries_cached")
clients_ever_seen=$(printf "%'d" "$clients_ever_seen")
unique_clients=$(printf "%'d" "$unique_clients")
dns_queries_all_types=$(printf "%'d" "$dns_queries_all_types")


# Load data in domoticz
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$DOMAINSBLOCKED_IDX&nvalue=0&svalue=$domains_being_blocked"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$DNSQUERIESTODAY_IDX&nvalue=0&svalue=$dns_queries_today"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$ADSBLOCKEDTODAY_IDX&nvalue=0&svalue=$ads_blocked_today"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$ADSPERCENTTODAY_IDX&nvalue=0&svalue=$ads_percentage_today"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UNIQUEDOMAINS_IDX&nvalue=0&svalue=$unique_domains"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$QUERIESFORWARDED_IDX&nvalue=0&svalue=$queries_forwarded"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$QUERIESCACHED_IDX&nvalue=0&svalue=$queries_cached"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$CLIENTSEVERSEEN_IDX&nvalue=0&svalue=$clients_ever_seen"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=udevice&idx=$UNIQUECLIENTS_IDX&nvalue=0&svalue=$unique_clients"

Re: PiHole Script

Posted: Wednesday 27 September 2023 19:39
by sincze
Tnx Chris, just noticed your post on the Pi-Hole plugin not working anymore.

Re: PiHole Script

Posted: Wednesday 27 September 2023 19:53
by Chris12
sincze wrote: Wednesday 27 September 2023 19:39 Tnx Chris, just noticed your post on the Pi-Hole plugin not working anymore.
Yeah, I let it wait for a while hoping some-one would fix it, not sure bc it's been a while. Then I found this post, and worked on that to get it working!

Re: PiHole Script

Posted: Wednesday 27 September 2023 20:04
by sincze
Need to check but it could be this Plugin faces the same issues I experienced before with a different version of Domoticz with the onConnect issue as the URL used is the same as you are using..

Re: PiHole Script

Posted: Wednesday 29 November 2023 18:52
by tarmacalastair
Thanks for this.

The wiki is still dated 2017 with the old script. Chris12's script above works nicely, thanks.

I thought I would log in and change it but my forum login doesn't work for the wiki and I can't see anywhere to create a new user.

Anyone know what to do?