From 2ac682c0000892099cae22dc3adc74306bd6f07e Mon Sep 17 00:00:00 2001 From: mt77 Date: Fri, 21 Feb 2025 01:48:20 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20monitor.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- monitor.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 monitor.py diff --git a/monitor.py b/monitor.py new file mode 100644 index 0000000..a110ddb --- /dev/null +++ b/monitor.py @@ -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()