├── README.md └── piholestats.sh /README.md: -------------------------------------------------------------------------------- 1 | # piholestatus 2 | A script to display Pi-Hole stats in Grafana via InfluxDB 3 | 4 | 5 | Script originally created by JON HAYWARD: https://fattylewis.com/Graphing-pi-hole-stats/ 6 | Adapted fo work with InfluxDB by /u/tollsjo in December 2016 7 | 8 | To install and run the script as a service under SystemD. See: https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux 9 | 10 | You can set this up on pretty much any host that has access to the Pi-Hole API and the InfluxDB API. I opted to run it as a service on the Pi-Hole Host. It requires the http API to be enabled on Influx. It also requires a "pip install influxdb" to work. Tested on Ubuntu 16.04 11 | 12 | Dashboard Example: 13 | ![Grafana Dashboard](http://i.imgur.com/4bitvQt.png) 14 | -------------------------------------------------------------------------------- /piholestats.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | 3 | # Script originally created by JON HAYWARD: https://fattylewis.com/Graphing-pi-hole-stats/ 4 | # Adapted to work with InfluxDB by /u/tollsjo in December 2016 5 | # Updated by Cludch December 2016 6 | 7 | # To install and run the script as a service under SystemD. See: https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux 8 | 9 | import requests 10 | import time 11 | from influxdb import InfluxDBClient 12 | 13 | HOSTNAME = "pihole" # Pi-hole hostname to report in InfluxDB for each measurement 14 | PIHOLE_API = "http://127.0.0.1/admin/api.php" 15 | INFLUXDB_SERVER = "dashboard" # IP or hostname to InfluxDB server 16 | INFLUXDB_PORT = 8086 # Port on InfluxDB server 17 | INFLUXDB_USERNAME = "telegraf" 18 | INFLUXDB_PASSWORD = "telegraf" 19 | INFLUXDB_DATABASE = "telegraf" 20 | DELAY = 10 # seconds 21 | 22 | def send_msg(domains_blocked, dns_queries_today, ads_percentage_today, ads_blocked_today): 23 | 24 | json_body = [ 25 | { 26 | "measurement": "piholestats." + HOSTNAME.replace(".", "_"), 27 | "tags": { 28 | "host": HOSTNAME 29 | }, 30 | "fields": { 31 | "domains_blocked": int(domains_blocked), 32 | "dns_queries_today": int(dns_queries_today), 33 | "ads_percentage_today": float(ads_percentage_today), 34 | "ads_blocked_today": int(ads_blocked_today) 35 | } 36 | } 37 | ] 38 | 39 | client = InfluxDBClient(INFLUXDB_SERVER, INFLUXDB_PORT, INFLUXDB_USERNAME, INFLUXDB_PASSWORD, INFLUXDB_DATABASE) # InfluxDB host, InfluxDB port, Username, Password, database 40 | # client.create_database(INFLUXDB_DATABASE) # Uncomment to create the database (expected to exist prior to feeding it data) 41 | client.write_points(json_body) 42 | 43 | if __name__ == '__main__': 44 | while True: 45 | api = requests.get(PIHOLE_API) # URI to pihole server api 46 | API_out = api.json() 47 | domains_blocked = (API_out['domains_being_blocked']).replace(',', '') 48 | dns_queries_today = (API_out['dns_queries_today']).replace(',', '') 49 | ads_percentage_today = (API_out['ads_percentage_today']) 50 | ads_blocked_today = (API_out['ads_blocked_today']).replace(',', '') 51 | 52 | send_msg(domains_blocked, dns_queries_today, ads_percentage_today, ads_blocked_today) 53 | time.sleep(DELAY) 54 | --------------------------------------------------------------------------------