Display output of PiHole api

From Domoticz
Jump to: navigation, search

Purpose

If you don't know PiHole. In short, PiHole is a DNS based ad blocked that runs smoothly on a Raspberry Pi. PiHole will block advertisements for all device connected to your network. It is free and it work pretty much out of the box. The admin panel gives your more possibilities to finetune the settings and to see more in depth information on your devices and the ads that were being blocked during the day. It is being actively developed.

Additional PiHole information

Find more info on:

PiHole homepage [1]

GitHub of PiHole [2]

Domoticz setup - needed hardware and devices

Under hardware create a new dummy switch named PiHole

On this hardware create 6 new virtual sensors:

Domains blocked - Text

DNS queries today - Text

Ads blocked today - Text

Ads percentage today - Percentage

Unique domains - Text

Queries forwarded - Text

For all new sensors write down the IDX.

Script

Create a file in ~/domoticz/scripts/ called PiHole_read.sh with the following content:

#!/bin/bash

# Settings
PIHOLE_IP="YOUR PIHOLE IP"  # PiHole IP
DOMO_IP="YOUR DOMOTICZ IP"    # Domoticz IP
DOMO_PORT="YOUR DOMOTICZ PORT" 	  # Domo port
DOMAINSBLOCKED_IDX='IDX FROM DOMOTICZ'
DNSQUERIESTODAY_IDX='IDX FROM DOMOTICZ'
ADSBLOCKEDTODAY_IDX='IDX FROM DOMOTICZ'
ADSPERCENTTODAY_IDX='IDX FROM DOMOTICZ'
UNIQUEDOMAINS_IDX='IDX FROM DOMOTICZ'
QUERIESFORWARDED_IDX='IDX FROM DOMOTICZ'

# 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"

You can run domotica and PiHole next to each other on one Pi, but you still need to fill twice the IP address.

Make sure it is executable

chmod +x PiHole_read.sh

Let crontab execute it every five mintues:

crontab -e
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
*\5 * * * * ~/domoticz/scripts/PiHole_read.sh


--Tapnl (talk) 23:50, 11 June 2017