| Server IP : 18.193.92.211 / Your IP : 216.73.216.128 Web Server : Apache/2.4.63 (Ubuntu) System : Linux ip-10-0-0-90 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 06:59:36 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.2.28 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /opt/ |
Upload File : |
#!/bin/bash
# Health Check Script for mobile.dispo24.de
# Usage: /usr/local/bin/health-check.sh [--fix] [--restart]
# Run as root
set -euo pipefail
# ============================================
# CONFIGURATION
# ============================================
readonly SCRIPT_VERSION="1.1"
readonly PM2_USER="toto"
readonly API_ENDPOINT="http://localhost:8031/v1/test"
readonly DISK_THRESHOLD=80
readonly LOG_FILE="/var/log/health-check.log"
# Colors
readonly GREEN='\033[0;32m'
readonly RED='\033[0;31m'
readonly YELLOW='\033[1;33m'
readonly NC='\033[0m'
# Parse arguments
FIX_MODE=0
RESTART_MODE=0
for arg in "$@"; do
case "$arg" in
--fix) FIX_MODE=1 ;;
--restart) RESTART_MODE=1 ;;
*) echo "Unknown argument: $arg"; echo "Usage: $0 [--fix] [--restart]"; exit 1 ;;
esac
done
# Counters
ISSUES=0
FIXED=0
# Log buffer - only write if there are issues
LOG_BUFFER=""
# ============================================
# HELPER FUNCTIONS
# ============================================
log_ok() {
echo -e "${GREEN}✓ $1${NC}"
}
log_error() {
local msg="$1"
echo -e "${RED}❌ $msg${NC}"
LOG_BUFFER+="[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $msg\n"
ISSUES=$((ISSUES+1))
}
log_warn() {
local msg="$1"
echo -e "${YELLOW}⚠ $msg${NC}"
LOG_BUFFER+="[$(date '+%Y-%m-%d %H:%M:%S')] WARN: $msg\n"
}
log_fixed() {
local msg="$1"
echo -e "${GREEN}🔧 $msg${NC}"
LOG_BUFFER+="[$(date '+%Y-%m-%d %H:%M:%S')] FIXED: $msg\n"
FIXED=$((FIXED+1))
}
write_log() {
if [[ $ISSUES -gt 0 || $RESTART_MODE -eq 1 ]]; then
{
echo "========================================"
echo "Health Check Report - $(date '+%Y-%m-%d %H:%M:%S')"
[[ $FIX_MODE -eq 1 ]] && echo "Mode: AUTO-FIX"
[[ $RESTART_MODE -eq 1 ]] && echo "Mode: FORCED RESTART"
echo "========================================"
echo -e "$LOG_BUFFER"
echo "Summary: $ISSUES issue(s) found, $FIXED fixed"
echo ""
} >> "$LOG_FILE"
fi
}
# ============================================
# FORCED RESTART (--restart)
# ============================================
if [[ $RESTART_MODE -eq 1 ]]; then
echo "================================"
echo -e "${RED}⚠ FORCED RESTART OF ALL SERVICES${NC}"
echo "Time: $(date '+%Y-%m-%d %H:%M:%S')"
echo "================================"
echo ""
LOG_BUFFER+="[$(date '+%Y-%m-%d %H:%M:%S')] FORCED RESTART initiated\n"
echo "=== Restarting PM2 ==="
if sudo -u "$PM2_USER" pm2 restart all 2>&1; then
log_fixed "PM2 processes restarted"
else
log_error "Failed to restart PM2 processes"
fi
echo ""
echo "=== Restarting MongoDB ==="
if systemctl restart mongod.service; then
log_fixed "MongoDB restarted"
else
log_error "Failed to restart MongoDB"
fi
echo ""
echo "=== Restarting Redis ==="
if systemctl restart redis-server.service; then
log_fixed "Redis restarted"
else
log_error "Failed to restart Redis"
fi
echo ""
echo "=== Restarting Apache ==="
if systemctl restart apache2.service; then
log_fixed "Apache restarted"
else
log_error "Failed to restart Apache"
fi
echo ""
# Wait for services to settle
echo "Waiting for services to stabilize..."
sleep 5
# Verify API after restart
echo "=== Verifying API ==="
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$API_ENDPOINT" 2>/dev/null || echo "000")
if [[ "$HTTP_CODE" =~ ^(200|201|204)$ ]]; then
log_ok "API responding (HTTP $HTTP_CODE)"
else
log_error "API not responding after restart (HTTP $HTTP_CODE)"
fi
echo ""
write_log
echo "================================"
if [[ $ISSUES -eq 0 ]]; then
log_ok "ALL SERVICES RESTARTED SUCCESSFULLY"
exit 0
else
echo -e "${RED}❌ $ISSUES ISSUE(S) DURING RESTART${NC}"
echo -e "${YELLOW}Log written to: $LOG_FILE${NC}"
exit 1
fi
fi
# ============================================
# HEADER
# ============================================
echo "================================"
echo "SERVICE HEALTH CHECK v${SCRIPT_VERSION}"
[[ $FIX_MODE -eq 1 ]] && echo "Mode: AUTO-FIX ENABLED"
echo "Time: $(date '+%Y-%m-%d %H:%M:%S')"
echo "================================"
echo ""
# ============================================
# 1. PM2 STATUS
# ============================================
echo "=== PM2 Processes ==="
if PM2_OUTPUT=$(sudo -u "$PM2_USER" pm2 jlist 2>&1); then
if echo "$PM2_OUTPUT" | jq -e '.[] | select(.pm2_env.status != "online")' &>/dev/null; then
log_error "Some PM2 processes are not online"
if [[ $FIX_MODE -eq 1 ]]; then
log_warn "Attempting to restart stopped PM2 processes..."
if sudo -u "$PM2_USER" pm2 restart all 2>&1; then
sleep 3
log_fixed "PM2 processes restarted"
else
log_error "Failed to restart PM2 processes"
fi
fi
else
log_ok "All PM2 processes online"
fi
else
log_error "Cannot query PM2 status"
fi
sudo -u "$PM2_USER" pm2 list 2>/dev/null || true
echo ""
# ============================================
# 2. MONGODB STATUS
# ============================================
echo "=== MongoDB ==="
if systemctl is-active --quiet mongod.service; then
log_ok "mongod.service is ACTIVE"
else
log_error "mongod.service is NOT RUNNING"
if [[ $FIX_MODE -eq 1 ]]; then
log_warn "Attempting to start MongoDB..."
if systemctl start mongod.service; then
sleep 2
log_fixed "MongoDB started successfully"
else
log_error "Failed to start MongoDB"
fi
fi
fi
echo ""
# ============================================
# 3. REDIS STATUS
# ============================================
echo "=== Redis ==="
if systemctl is-active --quiet redis-server.service; then
log_ok "redis-server.service is ACTIVE"
else
log_error "redis-server.service is NOT RUNNING"
if [[ $FIX_MODE -eq 1 ]]; then
log_warn "Attempting to start Redis..."
if systemctl start redis-server.service; then
sleep 2
log_fixed "Redis started successfully"
else
log_error "Failed to start Redis"
fi
fi
fi
echo ""
# ============================================
# 4. APACHE STATUS
# ============================================
echo "=== Apache ==="
if systemctl is-active --quiet apache2.service; then
log_ok "apache2.service is ACTIVE"
else
log_error "apache2.service is NOT RUNNING"
if [[ $FIX_MODE -eq 1 ]]; then
log_warn "Attempting to start Apache..."
if systemctl start apache2.service; then
sleep 2
log_fixed "Apache started successfully"
else
log_error "Failed to start Apache"
fi
fi
fi
echo ""
# ============================================
# 5. API ENDPOINT TEST
# ============================================
echo "=== API Endpoint Test ==="
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "$API_ENDPOINT" 2>/dev/null || echo "000")
if [[ "$HTTP_CODE" =~ ^(200|201|204)$ ]]; then
log_ok "API responding (HTTP $HTTP_CODE)"
else
log_error "API not responding properly (HTTP $HTTP_CODE)"
if [[ $FIX_MODE -eq 1 && "$HTTP_CODE" == "000" ]]; then
log_warn "API unreachable - restarting PM2..."
sudo -u "$PM2_USER" pm2 restart all 2>&1 && sleep 3
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "$API_ENDPOINT" 2>/dev/null || echo "000")
[[ "$HTTP_CODE" =~ ^(200|201|204)$ ]] && log_fixed "API recovered after restart"
fi
fi
echo ""
# ============================================
# 6. DISK SPACE
# ============================================
echo "=== Disk Space ==="
DISK_USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
if [[ "$DISK_USAGE" -lt "$DISK_THRESHOLD" ]]; then
log_ok "Disk usage: ${DISK_USAGE}%"
else
log_error "Disk usage HIGH: ${DISK_USAGE}%"
fi
echo ""
# ============================================
# WRITE LOG & SUMMARY
# ============================================
write_log
echo "================================"
if [[ $ISSUES -eq 0 ]]; then
log_ok "ALL SYSTEMS OK"
exit 0
else
echo -e "${RED}❌ $ISSUES ISSUE(S) FOUND${NC}"
[[ $FIX_MODE -eq 1 ]] && echo -e "${GREEN}🔧 $FIXED ISSUE(S) AUTO-FIXED${NC}"
echo -e "${YELLOW}Log written to: $LOG_FILE${NC}"
exit 1
fi