TNT/scripts/healthcheck.sh
m1ngsama 5f8b8fd843 feat: enhance anonymous access and long-term stability
Improvements for low-barrier anonymous access:
- Enhanced welcome message to clarify anonymous access
- Added EASY_SETUP.md guide in Chinese and English
- Updated README with anonymous access notes

Long-term stability enhancements:
- Improved systemd service with auto-restart and resource limits
- Added log rotation script (scripts/logrotate.sh)
- Added health check script (scripts/healthcheck.sh)
- Added cron setup script for automated maintenance
- Added anonymous access test suite

Testing:
- All security features verified (10/10 passed)
- Anonymous access tests passed (2/2)
- Health check verified

This ensures:
- Zero-barrier SSH access (any username, any password)
- Stable long-term operation with auto-restart
- Automated log management
- Continuous health monitoring
2026-01-22 15:06:54 +08:00

76 lines
2 KiB
Bash
Executable file

#!/bin/bash
# TNT Health Check Script
# Verifies the server is running and accepting connections
PORT="${1:-2222}"
TIMEOUT="${2:-5}"
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "TNT Health Check"
echo "=================="
echo ""
# Check if process is running
echo -n "Process check: "
if pgrep -x tnt > /dev/null; then
echo -e "${GREEN}${NC} TNT process is running"
PID=$(pgrep -x tnt)
echo " PID: $PID"
else
echo -e "${RED}${NC} TNT process is not running"
exit 1
fi
# Check if port is listening
echo -n "Port check: "
if lsof -i:$PORT -sTCP:LISTEN > /dev/null 2>&1 || netstat -ln 2>/dev/null | grep -q ":$PORT "; then
echo -e "${GREEN}${NC} Port $PORT is listening"
else
echo -e "${RED}${NC} Port $PORT is not listening"
exit 1
fi
# Try to connect via SSH
echo -n "Connection test: "
timeout $TIMEOUT ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=$TIMEOUT -p $PORT test@localhost exit 2>/dev/null &
CONNECT_PID=$!
wait $CONNECT_PID
CONNECT_RESULT=$?
if [ $CONNECT_RESULT -eq 0 ] || [ $CONNECT_RESULT -eq 255 ]; then
# Exit code 255 means SSH connection was established but auth failed, which is expected
echo -e "${GREEN}${NC} SSH connection successful"
else
echo -e "${YELLOW}${NC} SSH connection timeout or failed (but port is listening)"
fi
# Check log file
LOG_FILE="/var/lib/tnt/messages.log"
if [ -f "$LOG_FILE" ]; then
LOG_SIZE=$(du -h "$LOG_FILE" | cut -f1)
echo "Log file: $LOG_SIZE"
else
LOG_FILE="./messages.log"
if [ -f "$LOG_FILE" ]; then
LOG_SIZE=$(du -h "$LOG_FILE" | cut -f1)
echo "Log file: $LOG_SIZE"
else
echo "Log file: Not found (will be created on first message)"
fi
fi
# Memory usage
echo -n "Memory usage: "
if [ ! -z "$PID" ]; then
MEM=$(ps -p $PID -o rss= | awk '{printf "%.1f MB", $1/1024}')
echo "$MEM"
fi
echo ""
echo -e "${GREEN}Health check passed${NC}"
exit 0