-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpihole_monitor.py
78 lines (67 loc) · 2.66 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
import time
from datetime import datetime
import requests
from pihole import PiHole
def telegram_bot_send_text(message: str, targets: list, request_timeout: int = 3):
"""
Send telegram message
:param request_timeout: time until requests times out
:param targets: list of persons that should receive the message
:param message: message to send all chats
:return: nothing
"""
print_message(message)
for chat_id in TELEGRAM_CHAT_IDS:
if chat_id in targets:
send_text = "https://api.telegram.org/bot" + TELEGRAM_API_KEY \
+ "/sendMessage?chat_id=" + TELEGRAM_CHAT_IDS[chat_id] \
+ "&text=" + message
try:
requests.get(send_text, timeout=request_timeout)
except requests.exceptions.RequestException as e:
print_message("Exception raised while sending telegram message: " + str(e))
def print_message(message: str):
"""
Prints message including time and date
:param message: message to be printed
:return: nothing
"""
print("[" + datetime.today().strftime("%Y-%m-%d") + " - " + str(time.strftime("%H:%M:%S")) + "] "
+ message)
# Sleep time when starting script
INITIAL_SLEEP_TIME = 0
# Time to sleep between checks
REQUEST_INTERVAL = 60
# Telegram Bot API-Key
TELEGRAM_API_KEY = "XXXXXXXX"
# Chat-IDs of the users that will be informed
TELEGRAM_CHAT_IDS = {
"Peter": "12345",
}
# List of Pi-holes to check
PI_HOLES = [
PiHole("XXX.XXX.XXX.XXX", "Peters Pi-hole", ["Peter"])
]
time.sleep(INITIAL_SLEEP_TIME)
offline = False
while True:
for device in PI_HOLES:
status_code = device.get_status()
if status_code == -1 and offline is False:
offline = True
print_message("Unable to check Pi-holes. No internet connection available.")
continue
elif status_code != -1 and offline is True:
offline = False
print_message("Internet connection is available again.")
if status_code == 1:
telegram_bot_send_text(device.name + ": Device is online again!", device.users)
elif status_code == 2:
telegram_bot_send_text(device.name + ": Device seems to be offline!", device.users)
elif status_code == 3:
telegram_bot_send_text(device.name + ": Ad-blocking function is disabled!", device.users)
elif status_code == 4:
telegram_bot_send_text(device.name + ": FTL is not running anymore!", device.users)
elif status_code == 5:
telegram_bot_send_text(device.name + ": Unknown error occurred...", device.users)
time.sleep(REQUEST_INTERVAL)