flea/_deploy/scripts/health-check.sh
ruberoid 169acd2181
All checks were successful
continuous-integration/drone/tag Build is passing
release
2025-10-16 17:38:25 +04:00

109 lines
4.8 KiB
Bash
Executable File

#!/bin/bash
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 🏥 Nocr Services Health Check Script
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Checks health endpoints and pod status for all Nocr services
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
NAMESPACE="nocr"
FAILED_CHECKS=0
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🏥 Running health checks for Nocr services"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
echo "❌ kubectl not found. Please install kubectl."
exit 1
fi
# Function to check pod health
check_pod_health() {
local deployment=$1
local service_name=$2
echo ""
echo "🔍 Checking $service_name..."
# Get pod status
PODS=$(kubectl get pods -n "$NAMESPACE" -l app="$deployment" -o json)
# Check if any pods exist
POD_COUNT=$(echo "$PODS" | jq -r '.items | length')
if [ "$POD_COUNT" -eq 0 ]; then
echo "❌ No pods found for $service_name"
FAILED_CHECKS=$((FAILED_CHECKS + 1))
return 1
fi
# Check pod status
RUNNING_PODS=$(echo "$PODS" | jq -r '.items[] | select(.status.phase=="Running") | .metadata.name' | wc -l)
READY_PODS=$(echo "$PODS" | jq -r '.items[] | select(.status.conditions[] | select(.type=="Ready" and .status=="True")) | .metadata.name' | wc -l)
echo " 📊 Pods: $RUNNING_PODS running, $READY_PODS ready (total: $POD_COUNT)"
if [ "$RUNNING_PODS" -eq 0 ]; then
echo " ❌ No running pods for $service_name"
kubectl get pods -n "$NAMESPACE" -l app="$deployment"
FAILED_CHECKS=$((FAILED_CHECKS + 1))
return 1
fi
if [ "$READY_PODS" -eq 0 ]; then
echo " ❌ No ready pods for $service_name"
kubectl get pods -n "$NAMESPACE" -l app="$deployment"
echo " 🔍 Pod details:"
kubectl describe pods -n "$NAMESPACE" -l app="$deployment" | grep -A 20 "Conditions:"
FAILED_CHECKS=$((FAILED_CHECKS + 1))
return 1
fi
# Try to check health endpoint if service has one
case "$deployment" in
"telegram-listener"|"text-matcher"|"users"|"telegram-client")
POD_NAME=$(echo "$PODS" | jq -r '.items[0].metadata.name')
if [ -n "$POD_NAME" ]; then
echo " 🌐 Checking /health endpoint..."
if kubectl exec -n "$NAMESPACE" "$POD_NAME" -- curl -f -s http://localhost:8080/health > /dev/null 2>&1; then
echo " ✅ Health endpoint responding"
else
echo " ⚠️ Health endpoint not responding (might be warming up)"
fi
fi
;;
esac
echo "$service_name is healthy"
return 0
}
# Check each service
SERVICES=(
"telegram-listener:Telegram Listener"
"text-matcher:Text Matcher"
"users:Users Service"
"telegram-client:Telegram Client"
)
for service_info in "${SERVICES[@]}"; do
IFS=':' read -r deployment service_name <<< "$service_info"
check_pod_health "$deployment" "$service_name"
done
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📊 Summary:"
kubectl get pods -n "$NAMESPACE" -o wide
echo ""
if [ $FAILED_CHECKS -eq 0 ]; then
echo "✅ All health checks passed!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
exit 0
else
echo "$FAILED_CHECKS health check(s) failed"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
exit 1
fi