Pi-hole a mqtt

Posílaní dat z Pi-hole na mqtt aby jsme to mohly využít třeba v Pimatic

Nainstalujeme python mqtt paho

sudo apt install python-pip

sudo pip install paho-mqtt

A napíšeme kod.

#!/usr/bin/env python

import json
import urllib2
import paho.mqtt.client as mqtt
import time klient = mqtt.Client("python_pub") #klient.username_pw_set("username","password") klient.connect("192.168.x.x", 1883) url = "http://localhost/admin/api.php" data = json.load(urllib2.urlopen(url)) parsed_json = data print data print(parsed_json['ads_blocked_today']) #pocet blokovanych domen print(parsed_json['unique_clients']) #pocet pripojenych zarizen print(parsed_json['dns_queries_today']) #pocet dotazovanzch domen print(parsed_json['ads_percentage_today']) #procent zablokovanych domen z celkoveho poctu print(parsed_json['domains_being_blocked']) #pocet domen na cerne listine print(parsed_json['status']) #stav Pi-Hole klient.publish("pihole/blokdns",(parsed_json['ads_blocked_today']))
time.sleep(1) klient.publish("pihole/klientu",(parsed_json['unique_clients'])) klient.publish("pihole/dotazudns",(parsed_json['dns_queries_today'])) klient.publish("pihole/procentblokdns",(parsed_json['ads_percentage_today'])) klient.publish("pihole/dnsbloklist",(parsed_json['domains_being_blocked'])) klient.publish("pihole/stav",(parsed_json['status']))

Přidáme do crontabu spuštění co 5minut

*/5 * * * * /home/pi/pimqtt.py

This article was updated on listopad 20, 2019