· Alexander · Grafana  · 4 min read

Pi-Hole Stats in Grafana

Send Pi-Hole stats to InfluxDB with a Python script and build a Grafana dashboard from them.

Send Pi-Hole stats to InfluxDB with a Python script and build a Grafana dashboard from them.

If you do not know what Pi-hole is I definitely recommend you look into it. Especially if you want to block ads/telemetry on all your home network devices.

Now there are probably a few ways to do this but for my dashboard I ended up using a script to send pi-hole stats to InfluxDB. I currently have Pi-Hole running on a VM under Ubuntu Server 17.10.

First we need to install python-pip. So SSH to your Pi-Hole server and run:

Terminal window
sudo apt-get install python-pip -y

Now create a new directory for the script to live in.

Terminal window
mkdir /opt/pihole-influx

Clone the pi-hole-influx repo.

Terminal window
git clone https://github.com/janw/pi-hole-influx.git /opt/pihole-influx

Once that finishes, cd to /pihole-influx and run:

Terminal window
pip install -r requirements.txt

Now clone the config.example.ini to config.ini.

Terminal window
cp config.example.ini config.ini

Edit the config.ini file to match your environment.

Terminal window
nano config.ini
[InfluxDB]
port8086
hostname<if.of.influx.db>
usernamepihole
passwordpihole
databasepihole
[pihole]
api_locationaddress of the /admin/api.php of your pi-hole instance
instance_namehostname

Save and close the config.ini file.

Lastly, you need to create the InfluxDB database that your pi-hole stats will reside in. This will need to match what you put in the database = databasename section in your config.ini.

Terminal window
curl -XPOST "http://<ip.of.influx.db>:8086/query?u=<admin user>&p=<password>" --data-urlencode "q=CREATE DATABASE "pihole""
curl -i -XPOST "http://<ip.of.influx.db>:8086/query?u=<admin user>&p=<password>" --data-urlencode "q=CREATE USER "pihole" WITH PASSWORD "pihole""
curl -XPOST "http://<ip.of.influx.db>:8086/query?u=<admin user>&p=<password>" --data-urlencode "q=GRANT WRITE ON "pihole" TO "pihole""
curl -XPOST "http://<ip.of.influx.db>:8086/query?u=<admin user>&p=<password>" --data-urlencode "q=GRANT READ ON "pihole" TO "grafana""

Now launch piholeinflux.py.

Terminal window
./piholeinflux.py

Running piholeinflux.py as a service.

You can set up piholeinflux.py to run as a systemd service so that it will auto launch at boot time if you ever have to reboot your server.

Create the piholeinflux.service file.

Terminal window
nano piholeinflux.service

Paste the below info into your new .service file.

User=pi #YOUR USERNAME
ExecStart=/usr/bin/python /home/USERNAME/pihole-influx/piholeinflux.py

Save and close the .service file. Then run the following in order:

Terminal window
sudo ln -s /opt/pihole-influx/piholeinflux.service /etc/systemd/system
sudo systemctl enable piholeinflux.service
sudo systemctl daemon-reload
sudo systemctl start piholeinflux.service

Setting up the Grafana Dashboard

Pi-Hole Data Source

  1. Select the cog on the left hand side and click “data sources”
  2. Click “add data source”
  3. Click InfluxDB
  4. Enter the following information and hit “Save & Test”
SettingValue
Namepi-hole
URLhttp://<ip-of-influx.db>
Databasepihole
usergrafana
passwordgrafana

Dashboard Setup

Now you can import a basic dashboard using the ID from the script’s repo. This will give you some basic info from your Pi-Hole data source.

Dashboard ID: 6603

HOWEVER, there are a few issues with it. First, you will want to edit the Realtime Queries and add: non_negative_derivative or add math(* -1) to each of the queries, under “Metrics”, so the Y-Axis has no negative values.

Docker Container Version

You can deploy this script using the below Dockerfile and config.ini.

FROM alpine as builder
RUN apk add --no-cache git
WORKDIR /app
RUN git clone https://github.com/janw/pi-hole-influx.git
FROM python:3-alpine
WORKDIR /usr/src/app
COPY --from=builder /app/pi-hole-influx/requirements.txt /usr/src/app
RUN pip install --no-cache-dir -r requirements.txt
COPY --from=builder /app/pi-hole-influx/piholeinflux.py /usr/src/app
COPY config.ini .
CMD [ "python", "./piholeinflux.py" ]
[influxdb]
port = 8086
hostname = 10.9.9.120
username = pihole
password = allthosesweetstatistics
database = pihole
# Time between reports to InfluxDB (in seconds)
reporting_interval = 10
[pihole]
api_location = http://10.9.9.120/admin/api.php
instance_name = pihole
timeout = 10

Copy both of the above blocks into the same folder.

Terminal window
docker build -t your-name/of-image .

Thanks to my co-worker for throwing together this easy, lightweight Docker container version. Also since it doesn’t require a mounting volume, it should work in swarm mode!


Edit 4/29/19: Updated to match new Grafana guide settings.

Back to Blog

Comments


Related Posts

View All Posts »