الملفات
ghaymah-exam-Mohamed_Mousta…/q1-deploy-monitor/health-check.sh
Mohamed Moustafa c653222887 first commit
2026-07-27 23:27:12 +03:00

76 أسطر
1.8 KiB
Bash
ملف تنفيذي

#!/usr/bin/env bash
set -euo pipefail
HEALTH_URL="${HEALTH_URL:-https://teams-ghayama-dc56d4853672.hosted.ghaymah.systems/health}"
INTERVAL="${INTERVAL:-30}"
LOG_FILE="${LOG_FILE:-monitor.log}"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
timestamp() {
date '+%Y-%m-%d %H:%M:%S'
}
check_health() {
local ts
ts="$(timestamp)"
local response
local http_code
local response_time
response=$(curl -s -o /dev/null -w '%{http_code}|%{time_total}' \
--connect-timeout 5 \
--max-time 10 \
"$HEALTH_URL" 2>&1) || true
if [[ $? -ne 0 ]] || [[ -z "$response" ]]; then
printf "[%s]\n" "$ts"
printf "${RED}Status: DOWN${NC}\n"
printf "Error: Connection refused or timeout\n\n"
printf "[%s] DOWN - Connection refused or timeout\n" "$ts" >> "$LOG_FILE"
return
fi
http_code=$(echo "$response" | cut -d'|' -f1)
response_time=$(echo "$response" | cut -d'|' -f2)
local response_ms
response_ms=$(awk "BEGIN {printf \"%.0f\", $response_time * 1000}")
if [[ "$http_code" -eq 200 ]]; then
printf "[%s]\n" "$ts"
printf "${GREEN}Status: UP${NC}\n"
printf "HTTP: %s\n" "$http_code"
printf "Response Time: %s ms\n\n" "$response_ms"
printf "[%s] UP - HTTP %s - %s ms\n" "$ts" "$http_code" "$response_ms" >> "$LOG_FILE"
else
printf "[%s]\n" "$ts"
printf "${RED}Status: DOWN${NC}\n"
printf "HTTP: %s\n" "$http_code"
printf "Response Time: %s ms\n\n" "$response_ms"
printf "[%s] DOWN - HTTP %s - %s ms\n" "$ts" "$http_code" "$response_ms" >> "$LOG_FILE"
fi
}
cleanup() {
printf "\n${YELLOW}Monitoring stopped.${NC}\n"
exit 0
}
trap cleanup SIGINT SIGTERM
printf "${GREEN}Ghaymah Health Monitor${NC}\n"
printf "URL: %s\n" "$HEALTH_URL"
printf "Interval: %ss\n" "$INTERVAL"
printf "Log: %s\n\n" "$LOG_FILE"
while true; do
check_health
sleep "$INTERVAL"
done