Добавить monitor.py

This commit is contained in:
mt77 2025-02-21 01:48:20 +05:00
parent c4dc1463f3
commit 2ac682c000

60
monitor.py Normal file
View File

@ -0,0 +1,60 @@
import requests
import time
import daemon
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) # insecure warn disable
domains = ['google.com', '127.0.0.1']
protocol = "https://"
token = "TOKEN_YOU_BOT" ### УДАЛИТЬ
chatid = YOU_CHAT_ID ### УДАЛИТЬ
timeout_domains = []
def CheckDomain(domain):
try:
r = requests.get(f"{protocol}{domain}", verify=False) # verify SSL crt disable
code = int(r.status_code)
return code
except requests.exceptions.ConnectionError:
return 1
except:
return 0
def SendAlert(msg):
response = requests.post(
url='https://api.telegram.org/bot{0}/sendMessage'.format(token),
data={'chat_id': chatid, 'text': msg}
).json()
def master():
while True:
for domain in timeout_domains:
resp = CheckDomain(domain)
print(domain, resp)
if resp == 200:
timeout_domains.remove(domain)
domains.append(domain)
SendAlert(f'✅ RESOLVED ✅\n🔹Domain is avability\n🔹Domain: {domain}')
for domain in domains:
resp = CheckDomain(domain)
print(domain, resp)
if resp != 200:
timeout_domains.append(domain)
domains.remove(domain)
if resp == 1:
SendAlert(f'⚠️ ALARM! ⚠️\n🔹Response: no route to host\n🔹Domain: {domain}')
elif resp == 0:
SendAlert(f'⚠️ ALARM! ⚠️\n🔹Response: other connect error\n🔹Domain: {domain}')
else:
SendAlert(f'⚠️ ALARM! ⚠️\n🔹Response: {resp}\n🔹Domain: {domain}')
time.sleep(20)
def run_daemon():
with daemon.DaemonContext():
master()
if __name__ == "__main__":
run_daemon()