ccccccc / monitor.sh
cnmksjs's picture
Upload 49 files
24fd742 verified
#!/bin/bash
# 聊天应用监控脚本
echo "📊 聊天应用监控面板"
echo "===================="
echo
# 检查Docker服务
echo "🐳 Docker服务状态:"
if systemctl is-active --quiet docker; then
echo " ✅ Docker服务运行中"
else
echo " ❌ Docker服务未运行"
fi
echo
# 检查容器状态
echo "📦 容器状态:"
if command -v docker-compose >/dev/null 2>&1; then
docker-compose ps
else
docker compose ps
fi
echo
# 检查服务健康状态
echo "🏥 服务健康检查:"
# 检查后端API
if curl -f -s http://localhost:5000/api/health > /dev/null; then
echo " ✅ 后端API (http://localhost:5000)"
# 获取健康信息
HEALTH_INFO=$(curl -s http://localhost:5000/api/health)
echo " $(echo $HEALTH_INFO | jq -r '.status // "N/A"') - 运行时间: $(echo $HEALTH_INFO | jq -r '.uptime // "N/A"')秒"
echo " MongoDB: $(echo $HEALTH_INFO | jq -r '.mongodb // "N/A"')"
else
echo " ❌ 后端API (http://localhost:5000)"
fi
# 检查前端
if curl -f -s http://localhost:3000 > /dev/null; then
echo " ✅ 前端服务 (http://localhost:3000)"
else
echo " ❌ 前端服务 (http://localhost:3000)"
fi
# 检查MongoDB
if docker exec chat-mongo mongosh --eval "db.adminCommand('ping')" > /dev/null 2>&1; then
echo " ✅ MongoDB数据库"
else
echo " ❌ MongoDB数据库"
fi
echo
# 系统资源使用情况
echo "💻 系统资源使用:"
echo " CPU使用率: $(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | awk -F'%' '{print $1}')%"
echo " 内存使用: $(free -h | awk 'NR==2{printf "%.1f%%", $3*100/$2 }')"
echo " 磁盘使用: $(df -h / | awk 'NR==2{print $5}')"
echo
# Docker资源使用
echo "🐳 Docker资源使用:"
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"
echo
# 最近的日志
echo "📋 最近的日志 (最后10行):"
if command -v docker-compose >/dev/null 2>&1; then
docker-compose logs --tail=10
else
docker compose logs --tail=10
fi
echo
# 网络连接
echo "🌐 网络连接:"
echo " 端口3000状态: $(netstat -tuln | grep :3000 > /dev/null && echo "✅ 监听中" || echo "❌ 未监听")"
echo " 端口5000状态: $(netstat -tuln | grep :5000 > /dev/null && echo "✅ 监听中" || echo "❌ 未监听")"
echo " 端口27017状态: $(netstat -tuln | grep :27017 > /dev/null && echo "✅ 监听中" || echo "❌ 未监听")"
echo
# 数据库统计
echo "📊 数据库统计:"
if docker exec chat-mongo mongosh chatapp --eval "
print('用户数量: ' + db.users.countDocuments());
print('消息数量: ' + db.messages.countDocuments());
print('今日消息: ' + db.messages.countDocuments({timestamp: {\$gte: new Date(new Date().setHours(0,0,0,0))}}));
" 2>/dev/null; then
echo " ✅ 数据库统计获取成功"
else
echo " ❌ 无法获取数据库统计"
fi
echo
echo "🔄 监控完成 - $(date)"
echo "💡 提示: 使用 'watch -n 30 ./monitor.sh' 可以每30秒自动刷新监控信息"