Edit code and Dockerfile

هذا الالتزام موجود في:
root
2026-07-27 03:33:09 +00:00
الأصل 05edcc3fce
التزام 8fa1893c85
6 ملفات معدلة مع 187 إضافات و371 حذوفات

عرض الملف

@@ -11,139 +11,82 @@ URL = "https://mithal.space"
OUTPUT_FILE = "metrics.json"
def http_check():
def collect_metrics():
hostname = urlparse(URL).hostname
# HTTP latency & uptime
start = time.time()
try:
r = requests.get(URL, timeout=10)
response = requests.get(URL, timeout=10)
latency = round((time.time() - start) * 1000, 2)
return r.status_code, latency
except:
latency = round((time.time() - start) * 1000, 2)
return 0, latency
def dns_lookup():
host = urlparse(URL).hostname
status = response.status_code
except Exception:
latency = -1
status = 0
# DNS lookup
start = time.time()
try:
socket.gethostbyname(host)
except:
pass
return round((time.time() - start) * 1000, 2)
def ssl_info():
host = urlparse(URL).hostname
socket.gethostbyname(hostname)
dns = round((time.time() - start) * 1000, 2)
except Exception:
dns = -1
# SSL expiry
ssl_expiry = "Unavailable"
ssl_days = -1
try:
ctx = ssl.create_default_context()
with socket.create_connection((host,443),timeout=10) as sock:
with ctx.wrap_socket(sock,server_hostname=host) as ssock:
context = ssl.create_default_context()
with socket.create_connection((hostname, 443), timeout=10) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
cert = ssock.getpeercert()
expiry = datetime.strptime(
cert["notAfter"],
"%b %d %H:%M:%S %Y %Z"
)
days = (expiry-datetime.utcnow()).days
return expiry.strftime("%Y-%m-%d"),days
except:
return "Unavailable",-1
def search_response():
start=time.time()
try:
requests.get(URL,timeout=10)
except:
expiry = datetime.strptime(cert["notAfter"], "%b %d %H:%M:%S %Y %Z")
ssl_expiry = expiry.strftime("%Y-%m-%d")
ssl_days = (expiry - datetime.utcnow()).days
except Exception:
pass
return round((time.time()-start)*1000,2)
# Search response
start = time.time()
try:
requests.get(URL, timeout=10)
search_latency = round((time.time() - start) * 1000, 2)
except Exception:
search_latency = -1
if os.path.exists(OUTPUT_FILE):
with open(OUTPUT_FILE) as f:
data = json.load(f)
else:
data = {"history": []}
if os.path.exists(OUTPUT_FILE):
history = data.get("history", [])
with open(OUTPUT_FILE,"r") as f:
history.append({
"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"status": status,
"latency": latency,
"dns": dns,
"ssl_expiry": ssl_expiry,
"ssl_days_left": ssl_days,
"search_latency": search_latency
})
data=json.load(f)
history = history[-1440:]
else:
data={
"history":[]
uptime = round(
len([x for x in history if x["status"] == 200]) /
len(history) * 100,
2
)
output = {
"uptime": uptime,
"last_check": history[-1],
"history": history[-10:],
"latency_chart": [x["latency"] for x in history[-60:]]
}
status,latency=http_check()
record={
"time":datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"status":status,
"latency":latency,
"dns":dns_lookup(),
"search_latency":search_response()
}
ssl_date,ssl_days=ssl_info()
record["ssl_expiry"]=ssl_date
record["ssl_days_left"]=ssl_days
data["history"].append(record)
if len(data["history"])>1440:
data["history"]=data["history"][-1440:]
success=len([x for x in data["history"] if x["status"]==200])
uptime=round(success/len(data["history"])*100,2)
output={
"uptime":uptime,
"last_check":record,
"history":data["history"][-10:],
"latency_chart":[x["latency"] for x in data["history"][-60:]]
}
with open(OUTPUT_FILE,"w") as f:
json.dump(output,f,indent=4)
print("="*40)
print("Mithal Monitoring")
print("="*40)
print("Status :",status)
print("Latency :",latency,"ms")
print("DNS :",record["dns"],"ms")
print("Search :",record["search_latency"],"ms")
print("SSL Expiry :",ssl_date)
print("Days Left :",ssl_days)
print("Uptime :",uptime,"%")
with open(OUTPUT_FILE, "w") as f:
json.dump(output, f, indent=4)