PiHole Script

All kinds of 'OS' scripts
Post Reply
TapNL
Posts: 17
Joined: Monday 24 April 2017 21:52
Target OS: -
Domoticz version:
Contact:

PiHole Script

Post by TapNL » Tuesday 13 June 2017 19:35

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

User avatar
EddyG
Posts: 124
Joined: Monday 02 November 2015 6:54
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: Netherlands
Contact:

Re: PiHole Script

Post by EddyG » Tuesday 13 June 2017 20:02

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/......
Regards,
Eddy

User avatar
Derik
Posts: 1864
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: PiHole Script

Post by Derik » Tuesday 13 June 2017 20:55

mmm
My pihole fucku.. my xu4....
Something with rights...
My hdd is going straid to hell....
ScreenShot040.jpg
ScreenShot040.jpg (71.29 KiB) Viewed 1444 times
from the time i did install pihole....
Now i try to get my space back... :-)

Run pihole on a old rpi... works great
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

jjnj
Posts: 232
Joined: Thursday 31 July 2014 13:51
Target OS: Windows
Domoticz version:
Location: NL
Contact:

Re: PiHole Script

Post by jjnj » Wednesday 14 June 2017 6:53

Nice script, thanks a lot for sharing!

Verstuurd vanaf mijn LG-H870 met Tapatalk

User avatar
EdwinK
Posts: 859
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: PiHole Script

Post by EdwinK » Thursday 15 June 2017 10:28

Thanks, too bad Pi-Hole was making my system unresponsive. :(
Running latest BETA on a Pi-3 | Toon Thermostat | RFxcom | Dashticz V2 |

qwerk
Posts: 268
Joined: Tuesday 22 July 2014 7:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Netherlands
Contact:

Re: PiHole Script

Post by qwerk » Thursday 15 June 2017 14:25

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.

MsbS
Posts: 40
Joined: Friday 19 May 2017 14:00
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: PiHole Script

Post by MsbS » Thursday 15 June 2017 16:21

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.

simonrg
Posts: 1055
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi
Domoticz version:
Location: North East England
Contact:

Re: PiHole Script

Post by simonrg » Sunday 18 June 2017 20:25

@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.
Raspberry Pi 2 B - [email protected] PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266

TapNL
Posts: 17
Joined: Monday 24 April 2017 21:52
Target OS: -
Domoticz version:
Contact:

Re: PiHole Script

Post by TapNL » Monday 19 June 2017 23:34

@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.

jjnj
Posts: 232
Joined: Thursday 31 July 2014 13:51
Target OS: Windows
Domoticz version:
Location: NL
Contact:

Re: PiHole Script

Post by jjnj » Wednesday 21 June 2017 17:15

The script is running, however nothing changes in Domoticz:

Code: Select all

[email protected]:~ $ 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?

simonrg
Posts: 1055
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi
Domoticz version:
Location: North East England
Contact:

Re: PiHole Script

Post by simonrg » Monday 03 July 2017 8:53

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"

Raspberry Pi 2 B - [email protected] PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266

jjnj
Posts: 232
Joined: Thursday 31 July 2014 13:51
Target OS: Windows
Domoticz version:
Location: NL
Contact:

Re: PiHole Script

Post by jjnj » Monday 03 July 2017 11:08

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

jjnj
Posts: 232
Joined: Thursday 31 July 2014 13:51
Target OS: Windows
Domoticz version:
Location: NL
Contact:

Re: PiHole Script

Post by jjnj » Monday 17 July 2017 20:38

Sorry for the late reply, the updated script worked for me!

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests