-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiHoleDisplay.py
More file actions
219 lines (176 loc) · 5.94 KB
/
PiHoleDisplay.py
File metadata and controls
219 lines (176 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import os
import platform
import time
import logging
from board import SCL, SDA
import busio
import adafruit_ssd1306
import humanize
import psutil
import requests
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from datetime import datetime
# Config
logging.basicConfig(filename='/home/pi/DisplayPiHole/PiDisp.log', level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
logger = logging.getLogger(__name__)
# Network interface to retrieve the IP address
interface = os.getenv('PIHOLE_OLED_INTERFACE', 'eth0')
# Mount point for disk usage info
mount_point = os.getenv('PIHOLE_OLED_MOUNT_POINT', '/')
# There is no reset pin on the SSD1306 0.96"
# Create the I2C interface.
i2c = busio.I2C(SCL, SDA)
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
width = disp.width
height = disp.height
disp.show()
image = Image.new('1', (width, height))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('/home/pi/DisplayPiHole/SF_Pixelate.ttf', 8)
font2 = ImageFont.truetype('/home/pi/DisplayPiHole/SF_Pixelate.ttf', 40)
font3 = ImageFont.truetype('/home/pi/DisplayPiHole/SF_Pixelate.ttf', 10)
# font = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf', 8)
# font2 = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf', 32)
# font3 = ImageFont.truetype('VCR_OSD_MONO_1.001.ttf', 10)
#font = ImageFont.load_default()
sleep = 1 # seconds
hostname = platform.node()
try:
elapsed_seconds = 0
while True:
draw.rectangle((0, 0, width, height), outline=0, fill=0)
addr = psutil.net_if_addrs()[interface][0]
draw.text(
(0, 0),
"Pi: %s" % addr.address.rjust(15),
font=font3,
fill=255
)
uptime = datetime.now() - datetime.fromtimestamp(
psutil.boot_time()
)
draw.text(
(0, 12),
"Up: %s" % humanize.naturaltime(uptime),
font=font3,
fill=255
)
temps = psutil.sensors_temperatures()
for name, entries in temps.items():
for entry in entries:
temp = entry.current
draw.text(
(0, 24),
"Temp: %s °C" % temp,
font=font3,
fill=255
)
disp.image(image)
disp.show()
time.sleep(5)
draw.rectangle((0, 0, width, height), outline=0, fill=0)
for x in range(0,10,1):
if x < 10:
draw.rectangle((0, 0, width, height), outline=0, fill=0)
#cpu = int(psutil.cpu_percent(percpu=False))
draw.text((0, 0), "Pihole Backup Status:", font=font, fill=255)
cpu = int(psutil.cpu_percent(percpu=False))
draw.text((0, 8), "CPU", font=font, fill=255)
draw.text((26, 8), "%s%%" % cpu, font=font, fill=255)
draw.rectangle(
(50, 8, 126, 8 + 6),
outline=255,
fill=0
)
draw.rectangle(
(50, 8, 50 + cpu, 8 + 6),
outline=255,
fill=255
)
mem = int(psutil.virtual_memory().percent)
draw.text((0, 16), "RAM", font=font, fill=255)
draw.text((26, 16), "%s%%" % mem, font=font, fill=255)
draw.rectangle(
(50, 16, 126, 16 + 6),
outline=255,
fill=0
)
draw.rectangle(
(50, 16, 50 + mem, 16 + 6),
outline=255,
fill=255
)
disk = int(psutil.disk_usage(mount_point).percent)
draw.text((0, 24), "Disk", font=font, fill=255)
draw.text((26, 24), "%s%%" % disk, font=font, fill=255)
draw.rectangle(
(50, 24, 126, 24 + 6),
outline=255,
fill=0
)
draw.rectangle(
(50, 24, 50 + disk, 24 + 6),
outline=255,
fill=255
)
disp.image(image)
disp.show()
time.sleep(1)
x = x+1
else:
pass
req = requests.get('http://127.0.0.1/admin/api.php')
data = req.json()
for x in range(128, -1, -16):
# Draw a black filled box to clear image.
draw.rectangle((0, 0, width, height), outline=0, fill=0)
# Display large Pi-Hole ads blocked percentage
draw.text((x, 0), "%s%%" % req.json()["ads_percentage_today"], font=font2, fill=255)
disp.image(image)
disp.show()
time.sleep(3)
draw.rectangle((0, 0, width, height), outline=0, fill=0)
draw.text(
(0, 0),
"Pi-hole (%s)" % data["status"],
font=font,
fill=255
)
draw.line((0, 7, width, 7), fill=255)
draw.text(
(0,8),
"Blocked: %d (%d%%)" % (
data["ads_blocked_today"],
data["ads_percentage_today"]
),
font=font,
fill=255
)
draw.text(
(0, 16),
"Queries: %d" % data["dns_queries_today"],
font=font,
fill=255
)
draw.line((0, 50, width, 50), fill=255)
draw.text(
(0, 25),
"Blocklist: %d" % data["domains_being_blocked"],
font=font,
fill=255
)
disp.image(image)
disp.show()
time.sleep(5)
except (KeyboardInterrupt, SystemExit):
draw.rectangle((0, 0, width, height), outline=0, fill=0)
disp.image(image)
disp.show()
print("Exiting...")
except Exception as e:
draw.rectangle((0, 0, width, height), outline=0, fill=0)
disp.image(image)
disp.show()
logger.error(str(e))