-
Notifications
You must be signed in to change notification settings - Fork 0
/
pihole_monitor.py
84 lines (73 loc) · 2.45 KB
/
pihole_monitor.py
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
"""PiHole monitor"""
import json
import socket
from time import sleep
from urllib.request import urlopen
import psutil
from vfd import COLS, VFD
vfd = VFD(0, 0)
welcome = "PiHole Monitor"
HOST = str(socket.gethostbyname(socket.gethostname()))
URL = "http://" + HOST + "/admin/api.php"
def cpu_state() -> str:
"""Get CPU data."""
cpu_temp = psutil.sensors_temperatures()['cpu-thermal'][0].current
return f'{int(psutil.cpu_freq().current)} MHz {cpu_temp:.1f} C'
def get_bytes(t, iface='eth0') -> int:
"""Get raw network speed."""
with open('/sys/class/net/' + iface + '/statistics/' + t + '_bytes', 'r') as f:
data = f.read()
return int(data)
def net_speed() -> str:
"""Calculate live network speed and provide readable value."""
tx1 = get_bytes('tx')
rx1 = get_bytes('rx')
sleep(1)
tx2 = get_bytes("tx")
rx2 = get_bytes("rx")
tx_speed = (tx2 - tx1) / 1000000.0
rx_speed = (rx2 - rx1) / 1000000.0
return f"TX:{tx_speed:.3f} RX:{rx_speed:.3f}"
def main():
vfd.home()
vfd.text(welcome.center(COLS))
vfd.setCursor(0, 1)
vfd.text("IP:" + HOST)
sleep(3)
try:
while True:
data = json.load(urlopen(URL))
blocked = data["ads_blocked_today"]
percent = data["ads_percentage_today"]
queries = data["dns_queries_today"]
domains = data["domains_being_blocked"]
print("Blocked" + str(blocked) + "," + "Percent" + str(percent))
vfd.home()
vfd.text(cpu_state().center(COLS))
vfd.setCursor(0, 1)
vfd.text("Blocked: " + str(blocked))
vfd.setCursor(0, 2)
vfd.text("Percent: " + str(percent) + "%")
vfd.setCursor(0, 3)
vfd.text(net_speed().center(COLS))
sleep(4)
vfd.home()
vfd.text(cpu_state().center(COLS))
vfd.setCursor(0, 1)
vfd.text("Queries: " + str(queries))
vfd.setCursor(0, 2)
vfd.text("Domains: " + str(domains))
vfd.setCursor(0, 3)
vfd.text(net_speed().center(COLS))
sleep(4)
except KeyboardInterrupt:
print("Stopping..")
vfd.clear()
vfd.text("User interrupted".center(COLS))
sleep(2)
vfd.clear()
print("Stopped")
finally:
vfd.clear()
if __name__ == "__main__":
main()