الملفات

103 أسطر
2.0 KiB
Python

import requests
import socket
import ssl
import time
import json
import os
from datetime import datetime
URL = "https://mithal.space"
SEARCH_URL = "https://mithal.space/search?q=test"
DATA_FILE = "monitor_data.json"
def get_latency():
start = time.time()
r = requests.get(URL, timeout=10)
latency = (time.time() - start) * 1000
return round(latency, 2), r.status_code
def get_dns_time():
start = time.time()
socket.gethostbyname("mithal.space")
dns = (time.time() - start) * 1000
return round(dns, 2)
def get_ssl():
hostname = "mithal.space"
ctx = ssl.create_default_context()
with ctx.wrap_socket(socket.socket(), server_hostname=hostname) as s:
s.settimeout(5)
s.connect((hostname, 443))
cert = s.getpeercert()
expiry = cert["notAfter"]
expire_date = datetime.strptime(expiry, "%b %d %H:%M:%S %Y %Z")
remaining = (expire_date - datetime.utcnow()).days
return expiry, remaining
def get_search_time():
start = time.time()
try:
requests.get(SEARCH_URL, timeout=10)
except:
pass
return round((time.time() - start) * 1000, 2)
def save():
latency, status = get_latency()
dns = get_dns_time()
ssl_expiry, ssl_days = get_ssl()
search = get_search_time()
data = {
"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"latency": latency,
"status": status,
"uptime": status == 200,
"dns": dns,
"ssl_expiry": ssl_expiry,
"ssl_remaining_days": ssl_days,
"search_response": search
}
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "r") as f:
history = json.load(f)
else:
history = []
history.append(data)
history = history[-1440:]
with open(DATA_FILE, "w") as f:
json.dump(history, f, indent=4)
if __name__ == "__main__":
while True:
save()
print("Saved:", datetime.now())
time.sleep(60)