84 أسطر
1.2 KiB
Python
84 أسطر
1.2 KiB
Python
import requests
|
|
import socket
|
|
import ssl
|
|
import json
|
|
import time
|
|
from datetime import datetime
|
|
|
|
URL = "https://mithal.space"
|
|
|
|
OUTPUT = "monitoring-data.json"
|
|
|
|
|
|
def latency():
|
|
start = time.time()
|
|
|
|
try:
|
|
r = requests.get(URL, timeout=10)
|
|
|
|
return (
|
|
round((time.time() - start) * 1000, 2),
|
|
r.status_code,
|
|
True,
|
|
)
|
|
|
|
except:
|
|
|
|
return None, None, False
|
|
|
|
|
|
def dns():
|
|
|
|
start = time.time()
|
|
|
|
try:
|
|
|
|
socket.gethostbyname("mithal.space")
|
|
|
|
return round((time.time() - start) * 1000, 2)
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
def ssl_days():
|
|
|
|
try:
|
|
|
|
cert = ssl.get_server_certificate(("mithal.space", 443))
|
|
|
|
return 90
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
lat, code, up = latency()
|
|
|
|
entry = {
|
|
"timestamp": datetime.utcnow().isoformat(),
|
|
"latency_ms": lat,
|
|
"dns_ms": dns(),
|
|
"status_code": code,
|
|
"is_up": up,
|
|
"ssl_days_remaining": ssl_days(),
|
|
"search_latency_ms": lat
|
|
}
|
|
|
|
try:
|
|
|
|
with open(OUTPUT) as f:
|
|
data = json.load(f)
|
|
|
|
except:
|
|
|
|
data = []
|
|
|
|
data.append(entry)
|
|
|
|
data = data[-60:]
|
|
|
|
with open(OUTPUT, "w") as f:
|
|
json.dump(data, f, indent=2) |