Pi-hole Dns Traffic Visualizer

Made by Sam Lindley

About the project

Visualize the performance of Pi-hole on the Raspberry Pi Sense-HAT.

Project info

Difficulty:

Categories:

Displays
Lights

Platforms:

Raspberry Pi

Estimated time:

1 hour

Published:

25th March 2018

Items used in this project


Hardware components

114990584 ARM Raspberry Pi 3 Model B x 1
2738 ARM Raspberry Pi Sense HAT - For the Pi 3 / 2 / B+ / A+ x 1

Software app and online services

Pi-hole

curl -sSL https://install.pi-hole.net | bash

Sense-HAT library

sudo apt-get install sense-hat

Story


After setting up Pi-hole on a Raspberry Pi 3, I wanted a way to conveniently  view its performance without having to open up a browser. I decided to emulate  the chart from the web interface on a spare Sense-HAT.

    

I first query the Pi-hole API to retrieve information on the number of DNS requests and ads blocked over time. The data is organized depending on the time interval selected and scaled to fit an 8x8 RGB grid . Column height is used to indicate the relative level of DNS traffic generated in the previous 24-hour timeframe.

I decided to use color in the bar chart to represent either the aforementioned traffic level or the percentage of ads blocked. I  added a slight time delay to produce a ripple effect and the option to randomize the order of pixel generation.

Contributer Zach Cross added options to change the orientation of the chart and enable low-light mode.

With the option list rapidly expanding, I thought that joystick controls would be perfect to conveniently cycle through and select choices without having to manually type them at the command line. The following list describes the mapping of functionality to each joystick direction:

  • UP - PUSH
    Cycle color mode.
  • RIGHT - PUSH
    Cycle interval selection.
  • DOWN - PUSH
    Toggle low-light mode.
  • LEFT - PUSH
    Cycle display orientation.
  • MIDDLE - PUSH
    Toggle randomization.
  • MIDDLE - HOLD
    Exit program.

After completing the bar chart, I wanted to display additional information about the Pi-hole ecosystem in a different visual style. The idea to produce a spiral pattern arose and I decided to use it to display the overall percentage of ads blocked for the day. I used the following algorithm to display it:

  1. def spiral_graph(block_percentage, orientation, lowlight, randomize, x=3, y=3):
  2. grid_size = 64
  3. grid_list = []
  4. dx = 0
  5. dy = 1
  6. pivot_index = 0
  7. pivot_point = 1
  8.  
  9. grid_units = int(grid_size * block_percentage)
  10.  
  11. if not randomize:
  12. SENSE.clear()
  13. SENSE.set_rotation(orientation)
  14. SENSE.low_light = lowlight
  15.  
  16. for i in range(grid_size):
  17. if i < grid_units:
  18. if randomize:
  19. grid_list.append((x, 7 - y, (255, 0, 0)))
  20. else:
  21. SENSE.set_pixel(x, 7 - y, (255, 0, 0))
  22. else:
  23. if randomize:
  24. grid_list.append((x, 7 - y, (0, 0, 255)))
  25. else:
  26. SENSE.set_pixel(x, 7 - y, (0, 0, 255))
  27.  
  28. time.sleep(RIPPLE_SPEED)
  29.  
  30. if pivot_index == pivot_point:
  31. if dx == 0:
  32. dx, dy = dy, dx
  33. pivot_index = 0
  34. elif dy == 0:
  35. dx, dy = dy, -dx
  36. pivot_index = 0
  37. pivot_point += 1
  38.  
  39. x += dx
  40. y += dy
  41. pivot_index += 1
  42.  
  43. if randomize:
  44. SENSE.clear()
  45.  
  46. for pixel in random.sample(grid_list, grid_size):
  47. SENSE.set_pixel(pixel[0], pixel[1], pixel[2])
  48. time.sleep(RIPPLE_SPEED)


In the spiral graph, the overall percentage of ads blocked in the previous 24-hour timeframe is represented by the number of red pixels displayed.

To enhace the aesthetic experience I decided to alternate between the bar chart and spiral graph at regular intervals.

Code


Credits


Leave your feedback...