Files
张翔 498bb3a3c8 refactor: reorganize project structure and improve code quality
- Move CI/CD configs to config/ci/ directory
- Reorganize scripts into categorized directories (deployment, monitoring, testing, utils)
- Consolidate documentation into docs/ directory with proper structure
- Update linting and testing configurations
- Remove obsolete test reports and performance summaries
- Add new documentation for code quality tools and contact form security
- Improve project organization and maintainability
- Fix lint-staged config to only lint JS/TS files
- Disable react/react-in-jsx-scope rule for Next.js compatibility
- Ignore scripts and test config directories in ESLint
2026-03-24 13:38:58 +08:00

201 lines
5.7 KiB
Bash

#!/bin/bash
set -e
echo "🚀 监控和告警系统快速启动脚本"
echo "=================================="
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 检查 Docker
check_docker() {
echo -e "${YELLOW}📋 检查 Docker 环境...${NC}"
if ! command -v docker &> /dev/null; then
echo -e "${RED}❌ Docker 未安装${NC}"
echo "请访问 https://docs.docker.com/get-docker/ 安装 Docker"
exit 1
fi
echo -e "${GREEN}✅ Docker 已安装${NC}"
}
# 检查 Docker Compose
check_docker_compose() {
echo -e "${YELLOW}📋 检查 Docker Compose...${NC}"
if ! command -v docker-compose &> /dev/null; then
echo -e "${RED}❌ Docker Compose 未安装${NC}"
echo "请运行: sudo apt-get install docker-compose"
exit 1
fi
echo -e "${GREEN}✅ Docker Compose 已安装${NC}"
}
# 检查端口占用
check_ports() {
echo -e "${YELLOW}📋 检查端口占用...${NC}"
ports=(3000 9090 3001 9093)
port_names=("应用服务" "Prometheus" "Grafana" "Alertmanager")
for i in "${!ports[@]}"; do
port=${ports[$i]}
name=${port_names[$i]}
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then
echo -e "${YELLOW}⚠️ 端口 $port ($name) 已被占用${NC}"
else
echo -e "${GREEN}✅ 端口 $port ($name) 可用${NC}"
fi
done
}
# 创建必要的目录
create_directories() {
echo -e "${YELLOW}📋 创建必要的目录...${NC}"
mkdir -p monitoring
mkdir -p logs
mkdir -p data
echo -e "${GREEN}✅ 目录创建完成${NC}"
}
# 配置邮件服务
configure_email() {
echo -e "${YELLOW}📋 配置邮件服务...${NC}"
if [ ! -f "monitoring/alertmanager.yml" ]; then
echo -e "${RED}❌ 找不到 alertmanager.yml 配置文件${NC}"
exit 1
fi
echo "请输入邮件配置信息:"
read -p "SMTP 服务器 (默认: smtp.resend.com): " smtp_server
smtp_server=${smtp_server:-smtp.resend.com}
read -p "SMTP 端口 (默认: 587): " smtp_port
smtp_port=${smtp_port:-587}
read -p "SMTP 用户名 (默认: resend): " smtp_username
smtp_username=${smtp_username:-resend}
read -p "SMTP 密码 (Resend API Key): " smtp_password
read -p "发件人邮箱 (默认: alertmanager@novalon.cn): " from_email
from_email=${from_email:-alertmanager@novalon.cn}
read -p "接收告警的邮箱 (多个邮箱用逗号分隔): " to_emails
# 更新配置文件
sed -i.bak "s|smarthost: '.*'|smarthost: '$smtp_server:$smtp_port'|g" monitoring/alertmanager.yml
sed -i.bak "s|auth_username: '.*'|auth_username: '$smtp_username'|g" monitoring/alertmanager.yml
sed -i.bak "s|auth_password: '.*'|auth_password: '$smtp_password'|g" monitoring/alertmanager.yml
sed -i.bak "s|from: '.*'|from: '$from_email'|g" monitoring/alertmanager.yml
sed -i.bak "s|to: '.*'|to: '$to_emails'|g" monitoring/alertmanager.yml
echo -e "${GREEN}✅ 邮件配置完成${NC}"
}
# 启动监控服务
start_monitoring() {
echo -e "${YELLOW}📋 启动监控服务...${NC}"
if [ ! -f "docker-compose.monitoring.yml" ]; then
echo -e "${RED}❌ 找不到 docker-compose.monitoring.yml 文件${NC}"
exit 1
fi
docker-compose -f docker-compose.monitoring.yml up -d
echo -e "${GREEN}✅ 监控服务启动完成${NC}"
}
# 等待服务启动
wait_for_services() {
echo -e "${YELLOW}📋 等待服务启动...${NC}"
echo "等待 Prometheus 启动..."
for i in {1..30}; do
if curl -s http://localhost:9090/-/healthy > /dev/null 2>&1; then
echo -e "${GREEN}✅ Prometheus 已启动${NC}"
break
fi
sleep 2
done
echo "等待 Grafana 启动..."
for i in {1..30}; do
if curl -s http://localhost:3001/api/health > /dev/null 2>&1; then
echo -e "${GREEN}✅ Grafana 已启动${NC}"
break
fi
sleep 2
done
echo "等待 Alertmanager 启动..."
for i in {1..30}; do
if curl -s http://localhost:9093/-/healthy > /dev/null 2>&1; then
echo -e "${GREEN}✅ Alertmanager 已启动${NC}"
break
fi
sleep 2
done
}
# 显示访问信息
show_access_info() {
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}🎉 监控和告警系统启动成功!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "📊 监控服务访问地址:"
echo ""
echo -e " • Prometheus: ${YELLOW}http://localhost:9090${NC}"
echo -e " • Grafana: ${YELLOW}http://localhost:3001${NC} (admin/admin)"
echo -e " • Alertmanager: ${YELLOW}http://localhost:9093${NC}"
echo ""
echo "📋 下一步操作:"
echo ""
echo " 1. 访问 Grafana (http://localhost:3001)"
echo " 2. 登录 (用户名: admin, 密码: admin)"
echo " 3. 添加 Prometheus 数据源"
echo " 4. 导入监控仪表板"
echo ""
echo "📚 详细配置文档: docs/MONITORING_SETUP.md"
echo ""
}
# 主函数
main() {
echo ""
# 检查环境
check_docker
check_docker_compose
check_ports
# 创建目录
create_directories
# 询问是否配置邮件
echo ""
read -p "是否配置邮件服务? (y/n): " configure_email_choice
if [[ $configure_email_choice =~ ^[Yy]$ ]]; then
configure_email
fi
# 启动服务
start_monitoring
# 等待服务启动
wait_for_services
# 显示访问信息
show_access_info
}
# 运行主函数
main