monitor/monitor.py
2025-04-05 15:48:09 +05:00

69 lines
1.9 KiB
Python

import requests
import time
import daemon
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) # insecure warn disable
domains = os.getenv("TARGET_DOMAINS", "").split(',')
domains = [d.strip() for d in domains if d.strip()]
protocol = os.getenv("TARGET_PROTOCOL", "https://")
verify_domain = bool(os.getenv("TARGET_SSL_VERIFY", "True"))
token = os.getenv("BOT_TOKEN", "")
chatid = int(os.getenv("BOT_CHATID", ""))
if not domains or not token or not chatid:
print("Error: Plese set domains, tg token, and tg chat id")
exit(1)
print(domains)
timeout_domains = []
def CheckDomain(domain):
try:
r = requests.get(f"{protocol}{domain}", verify=verify_domain) # 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()