mnemo_cards/tools/deploy/check-and-restore-nginx.sh
2026-01-03 16:48:08 +03:00

130 lines
4 KiB
Bash
Executable file

#!/bin/bash
# Script to check nginx status and restore it if needed
# This script can be run manually or via cron
# Usage: ./check-and-restore-nginx.sh
set -e
SERVER_IP="147.45.152.129"
SERVER_USER="root"
echo "🔍 Checking nginx status and restoring if needed..."
echo "Server: $SERVER_IP"
echo ""
ssh "$SERVER_USER@$SERVER_IP" << 'ENDSSH'
set -e
NGINX_STATUS=0
# Check if nginx is running
if systemctl is-active --quiet nginx; then
echo "✅ Nginx is running"
# Additional check: can nginx respond?
if curl -f -s http://localhost/health > /dev/null 2>&1 || \
curl -f -s http://localhost > /dev/null 2>&1; then
echo "✅ Nginx is responding to requests"
else
echo "⚠️ Nginx process is running but not responding to requests"
NGINX_STATUS=1
fi
else
echo "❌ Nginx is not running!"
NGINX_STATUS=1
fi
# If nginx is not working, try to restore it
if [ $NGINX_STATUS -ne 0 ]; then
echo ""
echo "🔧 Attempting to restore nginx..."
# Check nginx configuration
echo "🧪 Testing nginx configuration..."
if nginx -t 2>&1; then
echo "✅ Nginx configuration is valid"
else
echo "❌ Nginx configuration has errors!"
echo "📋 Recent nginx errors:"
journalctl -u nginx --since "10min ago" --no-pager -n 20 || true
exit 1
fi
# Try to start nginx
echo ""
echo "🚀 Starting nginx..."
if systemctl start nginx; then
sleep 2
if systemctl is-active --quiet nginx; then
echo "✅ Nginx started successfully"
# Verify it's responding
sleep 1
if curl -f -s http://localhost > /dev/null 2>&1; then
echo "✅ Nginx is responding to requests"
else
echo "⚠️ Nginx started but not responding yet (may need a moment)"
fi
else
echo "❌ Failed to start nginx!"
echo "📋 Recent nginx errors:"
journalctl -u nginx --since "10min ago" --no-pager -n 30 || true
exit 1
fi
else
echo "❌ Failed to start nginx service!"
echo "📋 Recent nginx errors:"
journalctl -u nginx --since "10min ago" --no-pager -n 30 || true
exit 1
fi
fi
# Check for OOM kills in system logs
echo ""
echo "🔍 Checking for OOM kills in system logs..."
if dmesg | grep -i "oom\|killed process.*nginx" | tail -5; then
echo "⚠️ Found OOM kill events related to nginx!"
echo "💡 Consider checking system memory usage: free -h"
else
echo "✅ No recent OOM kill events for nginx"
fi
# Show current memory usage
echo ""
echo "📊 Current system memory usage:"
free -h || true
# Show nginx process info
echo ""
echo "📊 Nginx process information:"
if pgrep -f 'nginx: master' > /dev/null; then
MASTER_PID=$(pgrep -f 'nginx: master' | head -1)
echo " Master PID: $MASTER_PID"
# Check OOM score
if [ -f "/proc/$MASTER_PID/oom_score_adj" ]; then
OOM_SCORE=$(cat "/proc/$MASTER_PID/oom_score_adj")
echo " OOM Score Adjust: $OOM_SCORE"
if [ "$OOM_SCORE" = "-1000" ]; then
echo " ✅ Nginx is protected from OOM killer"
else
echo " ⚠️ Nginx OOM protection may not be configured correctly"
fi
fi
# Show memory usage of nginx processes
echo " Memory usage:"
ps aux | grep -E 'nginx: (master|worker)' | grep -v grep || true
else
echo " ⚠️ Nginx master process not found"
fi
echo ""
echo "✅ Nginx check completed"
ENDSSH
echo ""
echo "✅ Nginx status check completed!"