Pi-hole Chronometer web page

Summary

This post explains how to get the Pi-hole Chronometer stats displayed on a web page via the Pi-hole web server. There is no need to be logged in. The stats page refreshes every minute.

This approach may be useful for those with their Pi-hole on a secure network and who just want a quick way to see the chrono stats without needing to be logged in or open a terminal. This approach is NOT recommended for a Pi-hole which can be accessed by unknown users or which is internet facing.

It is assumed that it is the latest Pi-hole (as of Sep 2022) running on the latest Pi OS or a similar derivative, that the user is pi and that the user is in their home directory and that the editor is nano.

Instructions

Make a directory called stats to store the script and stats

mkdir stats

Create a script in there called chronostats

nano stats/chronostats

Paste in the bash script code below and save. This script runs Pi-hole Chronometer once, strips out the colour and movement codes and saves to a staging file. Then the date and time are added and the finished file copied to a stats file.

#!/bin/bash

pihole -c -e | sed 's/\x1B\[[0-9;]*[a-zA-Z]//g' > /home/pi/stats/stagestats.txt
echo -e '' >> /home/pi/stats/stagestats.txt
echo -n "      Date: " >> /home/pi/stats/stagestats.txt
date >> /home/pi/stats/stagestats.txt
cp /home/pi/stats/stagestats.txt /home/pi/stats/stats.txt

Create a stats web page

sudo nano /var/www/html/pihole/stats.html

Paste in the html code below and save. This code shows a title and the reload comment and displays the contents of the aforementioned stats file in a small text window. The page auto-refreshes itself every minute, thus automatically displaying the most recent version of the stats file every minute.

<!doctype html>
<html>
  <head>
    <title>Pi-hole stats</title>
    <meta http-equiv="refresh" content="60">
  </head>
  <body>
    <h1>Pi-hole stats</h1>
    <p>Reloads every minute</p>
    <p><embed style="border-style:solid" src="stats.txt" width="640" height="480"></p>
  </body>
</html>

Create a symbolic link to the stats file so the web server can access it

sudo ln -s /home/pi/stats/stats.txt /var/www/html/pihole/stats.txt

Testing

Run the script

bash -l /home/pi/stats/chronostats

Go the stats page in your browser, either logged out or logged in, doesn't matter, and you should see the Pi-hole Chronometer output from just now.

http://<your pi address>/pihole/stats.html

Auto-run

If your stats are present then it's working. The web page will auto-refresh every minute but the chronostats script needs to be run every minute to update the text file that the web page displays. You can achieve this in cron. Edit your crontab.

crontab -e

Select nano if you've not used it before. Go to the end of the comments and on the first blank line you get to, paste in the entry shown below and save the file. It should report that it is installing the new crontab.

* * * * * bash -l /home/pi/stats/chronostats

Finished

You should now be seeing the Pi-hole Chronometer stats updating every minute.

Notes

It's worth repeating that this approach is NOT recommended for a Pi-hole which can be accessed by unknown users or which is internet facing. It's more just for hobbyist and casual home users, and was a bit of an exercise for me in playing with some of Pi-hole's great features.

The current stats.txt file can also be accessed directly via

http://<your pi address>/pihole/stats.txt

The script is writing the stats and staging files to the SD card every minute. It would be much better to use a small ram disk and avoid the SD card altogether. I did try this and ended up with tension between how ram disk persistence is handled between ssh logged-in bash users versus cron-initiated bash users, and the good security of the platform which I did not want to compromise to work around it. Feel free to comment if you know a simple approach that doesn't involve copying environment variables system-wide, etc

I daresay the page could be extended to include more stats, and probably a cleaner method of persistence entirely, maybe using the API, but then it's getting into realms of a serious mod and would need proper and serious consideration over security implications, and this was really just a mini-project for the evening. I did look at the json output but there is not as much useful info shown.

To uninstall, edit crontab and remove the entry, delete the stats directory and the files within from the home directory, and delete the stats.html and the stats.txt symbolic link from the web directory.

1 Like