Initial commit
هذا الالتزام موجود في:
8
.dockerignore
Normal file
8
.dockerignore
Normal file
@@ -0,0 +1,8 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.pyo
|
||||
*.pyd
|
||||
.git
|
||||
.gitignore
|
||||
.env
|
||||
venv/
|
||||
3
.gitignore
مباع
Normal file
3
.gitignore
مباع
Normal file
@@ -0,0 +1,3 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
health.log
|
||||
16
Dockerfile
Normal file
16
Dockerfile
Normal file
@@ -0,0 +1,16 @@
|
||||
FROM python:3.11-slim
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt .
|
||||
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY . .
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
CMD ["python", "app.py"]
|
||||
70
app.py
Normal file
70
app.py
Normal file
@@ -0,0 +1,70 @@
|
||||
from flask import Flask, jsonify, render_template, request
|
||||
import time
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Application start time
|
||||
start_time = time.time()
|
||||
|
||||
# Metrics
|
||||
request_count = 0
|
||||
last_response_time = 0
|
||||
|
||||
|
||||
@app.before_request
|
||||
def before_request():
|
||||
request.start_time = time.perf_counter()
|
||||
|
||||
|
||||
@app.after_request
|
||||
def after_request(response):
|
||||
global request_count
|
||||
global last_response_time
|
||||
|
||||
request_count += 1
|
||||
last_response_time = round(
|
||||
(time.perf_counter() - request.start_time) * 1000, 2
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def home():
|
||||
return render_template("dashboard.html")
|
||||
|
||||
|
||||
@app.route("/health")
|
||||
def health():
|
||||
return jsonify({
|
||||
"status": "UP",
|
||||
"message": "Application is healthy",
|
||||
"uptime_seconds": round(time.time() - start_time, 2),
|
||||
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"version": "1.0.0"
|
||||
}), 200
|
||||
|
||||
|
||||
@app.route("/metrics")
|
||||
def metrics():
|
||||
return jsonify({
|
||||
"status": "UP",
|
||||
"requests": request_count,
|
||||
"response_time_ms": last_response_time,
|
||||
"uptime_seconds": round(time.time() - start_time, 2)
|
||||
})
|
||||
|
||||
|
||||
@app.route("/api/info")
|
||||
def api_info():
|
||||
return jsonify({
|
||||
"application": "Ghaymah SRE API",
|
||||
"language": "Python",
|
||||
"framework": "Flask",
|
||||
"version": "1.0.0",
|
||||
"author": "Moustafa Medhat"
|
||||
})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=8080)
|
||||
30
health-check.sh
Executable file
30
health-check.sh
Executable file
@@ -0,0 +1,30 @@
|
||||
#!/bin/bash
|
||||
|
||||
URL="http://localhost:8080/health"
|
||||
LOG_FILE="/home/ec2-user/projects/ghaymah-exam-moustafa-medhat-sre/q1-deploy-monitor/health.log"
|
||||
|
||||
touch "$LOG_FILE"
|
||||
|
||||
echo "===== Health Monitor Started =====" >> "$LOG_FILE"
|
||||
|
||||
while true
|
||||
do
|
||||
START=$(date +%s%3N)
|
||||
|
||||
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$URL")
|
||||
|
||||
END=$(date +%s%3N)
|
||||
RESPONSE_TIME=$((END - START))
|
||||
|
||||
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
|
||||
if [ "$HTTP_CODE" -eq 200 ]; then
|
||||
STATUS="UP"
|
||||
else
|
||||
STATUS="DOWN"
|
||||
fi
|
||||
|
||||
echo "[$TIMESTAMP] Status=$STATUS | HTTP=$HTTP_CODE | Response=${RESPONSE_TIME}ms" >> "$LOG_FILE"
|
||||
|
||||
sleep 30
|
||||
done
|
||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
Flask==3.1.0
|
||||
151
templates/dashboard.html
Normal file
151
templates/dashboard.html
Normal file
@@ -0,0 +1,151 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Ghaymah SRE Dashboard</title>
|
||||
|
||||
<style>
|
||||
*{
|
||||
margin:0;
|
||||
padding:0;
|
||||
box-sizing:border-box;
|
||||
font-family:Arial, Helvetica, sans-serif;
|
||||
}
|
||||
|
||||
body{
|
||||
background:#f5f7fb;
|
||||
}
|
||||
|
||||
.container{
|
||||
width:900px;
|
||||
margin:40px auto;
|
||||
}
|
||||
|
||||
h1{
|
||||
text-align:center;
|
||||
margin-bottom:30px;
|
||||
color:#2c3e50;
|
||||
}
|
||||
|
||||
.cards{
|
||||
display:flex;
|
||||
justify-content:space-between;
|
||||
gap:20px;
|
||||
}
|
||||
|
||||
.card{
|
||||
flex:1;
|
||||
background:white;
|
||||
border-radius:10px;
|
||||
padding:25px;
|
||||
text-align:center;
|
||||
box-shadow:0 5px 15px rgba(0,0,0,.1);
|
||||
}
|
||||
|
||||
.title{
|
||||
color:#666;
|
||||
font-size:18px;
|
||||
margin-bottom:15px;
|
||||
}
|
||||
|
||||
.value{
|
||||
font-size:34px;
|
||||
font-weight:bold;
|
||||
}
|
||||
|
||||
.up{
|
||||
color:green;
|
||||
}
|
||||
|
||||
.down{
|
||||
color:red;
|
||||
}
|
||||
|
||||
.footer{
|
||||
margin-top:35px;
|
||||
text-align:center;
|
||||
color:#777;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
|
||||
<h1>🚀 Ghaymah SRE Dashboard</h1>
|
||||
|
||||
<div class="cards">
|
||||
|
||||
<div class="card">
|
||||
<div class="title">Status</div>
|
||||
<div id="status" class="value">Loading...</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="title">Response Time</div>
|
||||
<div id="response" class="value">0 ms</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="title">Requests</div>
|
||||
<div id="requests" class="value">0</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="footer">
|
||||
Last Update:
|
||||
<span id="time">--</span>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
|
||||
async function loadMetrics(){
|
||||
|
||||
try{
|
||||
|
||||
const response = await fetch('/metrics');
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
const status=document.getElementById("status");
|
||||
|
||||
status.innerHTML=data.status;
|
||||
|
||||
status.className="value";
|
||||
|
||||
if(data.status==="UP")
|
||||
status.classList.add("up");
|
||||
else
|
||||
status.classList.add("down");
|
||||
|
||||
document.getElementById("response").innerHTML=data.response_time_ms+" ms";
|
||||
|
||||
document.getElementById("requests").innerHTML=data.requests;
|
||||
|
||||
document.getElementById("time").innerHTML=
|
||||
new Date().toLocaleTimeString();
|
||||
|
||||
}
|
||||
|
||||
catch(error){
|
||||
|
||||
document.getElementById("status").innerHTML="DOWN";
|
||||
|
||||
document.getElementById("status").className="value down";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
loadMetrics();
|
||||
|
||||
setInterval(loadMetrics,30000);
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
المرجع في مشكلة جديدة
حظر مستخدم