# HostManager **Repository Path**: myonsoul/host-manager ## Basic Information - **Project Name**: HostManager - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-06-28 - **Last Updated**: 2025-06-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 主机管理系统 基于Django + Celery的企业主机管理系统,提供主机、城市、机房管理,支持主机连通性检测、密码自动管理、统计分析等功能。 ## 技术栈 - **后端框架**: Django 4.2.7 - **API框架**: Django REST Framework - **任务队列**: Celery - **消息代理**: Redis - **数据库**: PostgreSQL/SQLite - **加密**: cryptography ## 核心功能 1. **主机管理**: 支持主机、城市、机房的增删改查操作 2. **连通性检测**: 提供ping检测API,实时检测主机网络状态 3. **密码管理**: 每8小时自动更新主机root密码并加密存储 4. **统计分析**: 每日00:00按城市和机房维度统计主机数量 5. **性能监控**: 中间件统计每个API请求的耗时 ## 项目结构 ``` host_management_system/ ├── manage.py # Django管理脚本 ├── requirements.txt # 项目依赖 ├── config/ # Django配置 │ ├── settings/ # 分环境配置 │ ├── urls.py # 主URL配置 │ ├── wsgi.py # WSGI配置 │ └── celery.py # Celery配置 ├── apps/ # 应用目录 │ ├── hosts/ # 主机管理应用 │ │ ├── models.py # 数据模型 │ │ ├── views.py # API视图 │ │ ├── serializers.py # 序列化器 │ │ ├── utils.py # 工具函数 │ │ ├── tasks.py # Celery任务 │ │ └── admin.py # 后台管理 │ ├── statistics/ # 统计应用 │ │ ├── models.py # 统计模型 │ │ ├── views.py # 统计API │ │ └── tasks.py # 统计任务 │ └── middleware/ # 中间件 │ └── performance.py # 性能监控中间件 └── logs/ # 日志目录 ``` ## 快速开始 ### 方式一:Docker部署 (推荐) #### 生产环境部署 ```bash # 1. 克隆项目 git clone cd 主机管理系统 # 2. 初始化环境配置 make init # 编辑 .env 文件,配置生产环境参数 # 3. 构建和启动服务 make build make up # 4. 查看服务状态 docker-compose ps make logs ``` #### 开发环境部署 ```bash # 启动开发环境 make dev-build make dev-up # 查看开发环境日志 make dev-logs ``` #### Docker常用命令 ```bash make help # 查看所有可用命令 make shell # 进入Django shell make migrate # 运行数据库迁移 make superuser # 创建超级用户 make backup-db # 备份数据库 make health # 检查服务健康状态 make clean # 清理容器和镜像 ``` #### 服务访问地址 - **Web应用**: http://localhost (通过Nginx) - **API接口**: http://localhost/api/ - **Django Admin**: http://localhost/admin/ - **直接访问Django**: http://localhost:8000 默认管理员账号:`admin` / `admin123` ### 方式二:传统部署 #### 1. 环境准备 ```bash # Python 3.8+ python --version # 安装Redis # Ubuntu/Debian sudo apt-get install redis-server # macOS brew install redis # 启动Redis redis-server ``` ### 2. 项目安装 ```bash # 克隆项目 git clone cd host_management_system # 创建虚拟环境 python -m venv venv source venv/bin/activate # Linux/Mac # venv\Scripts\activate # Windows # 安装依赖 pip install -r requirements.txt ``` ### 3. 配置环境变量 创建`.env`文件: ```bash # Django配置 SECRET_KEY=your-secret-key-here DEBUG=True ALLOWED_HOSTS=localhost,127.0.0.1 # 数据库配置(开发环境使用SQLite) DB_NAME=host_management DB_USER=postgres DB_PASSWORD=password DB_HOST=localhost DB_PORT=5432 # Redis配置 CELERY_BROKER_URL=redis://localhost:6379/0 CELERY_RESULT_BACKEND=redis://localhost:6379/0 # 密码加密密钥(自动生成) PASSWORD_ENCRYPTION_KEY=your-fernet-key-here ``` ### 4. 数据库初始化 ```bash # 生成迁移文件 python manage.py makemigrations # 应用迁移 python manage.py migrate # 创建超级用户 python manage.py createsuperuser ``` ### 5. 启动服务 ```bash # 终端1:启动Django开发服务器 python manage.py runserver # 终端2:启动Celery Worker celery -A config worker -l info # 终端3:启动Celery Beat(定时任务) celery -A config beat -l info ``` ## API接口 ### 主机管理 - `GET /api/cities/` - 获取城市列表 - `POST /api/cities/` - 创建城市 - `GET /api/cities/{id}/` - 获取城市详情 - `PUT /api/cities/{id}/` - 更新城市 - `DELETE /api/cities/{id}/` - 删除城市 - `GET /api/datacenters/` - 获取机房列表 - `POST /api/datacenters/` - 创建机房 - `GET /api/hosts/` - 获取主机列表 - `POST /api/hosts/` - 创建主机 - `POST /api/hosts/{id}/ping/` - 检测主机连通性 - `POST /api/hosts/batch_ping/` - 批量ping检测 ### 统计分析 - `GET /api/statistics/hosts/` - 获取统计数据 - `GET /api/statistics/hosts/summary/` - 获取统计摘要 - `GET /api/statistics/hosts/trends/` - 获取趋势数据 ### API响应格式 ```json { "code": 200, "message": "success", "data": { // 响应数据 } } ``` ## 定时任务 系统配置了以下定时任务: 1. **密码更新**: 每8小时更新所有主机密码 2. **统计生成**: 每日00:00生成主机统计数据 可以通过以下命令手动执行任务: ```bash # 手动更新密码 python manage.py shell -c "from apps.hosts.tasks import update_host_passwords; update_host_passwords.delay()" # 手动生成统计 python manage.py shell -c "from apps.statistics.tasks import generate_daily_statistics; generate_daily_statistics.delay()" ``` ## 监控和日志 系统会生成以下日志文件: - `logs/django.log` - Django应用日志 - `logs/performance.log` - 性能监控日志 可以通过以下命令查看日志: ```bash # 查看应用日志 tail -f logs/django.log # 查看性能日志 tail -f logs/performance.log ``` ## 生产环境部署 ### 使用Docker部署 ```bash # 构建镜像 docker build -t host-management . # 运行容器 docker run -d -p 8000:8000 --name host-management-app host-management ``` ### 使用gunicorn + nginx ```bash # 安装gunicorn pip install gunicorn # 启动gunicorn gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 4 # nginx配置 server { listen 80; server_name your_domain.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /static/ { alias /path/to/staticfiles/; } } ``` ## 开发说明 ### 添加新功能 1. 在相应的`models.py`中定义数据模型 2. 在`serializers.py`中定义序列化器 3. 在`views.py`中实现API视图 4. 在`urls.py`中配置URL路由 5. 运行`python manage.py makemigrations`生成迁移 6. 运行`python manage.py migrate`应用迁移 ### 测试 ```bash # 运行所有测试 python manage.py test # 运行特定应用的测试 python manage.py test apps.hosts # 运行特定测试类 python manage.py test apps.hosts.tests.HostModelTest ``` ## 常见问题 ### Q1: Celery任务不执行? A1: 检查Redis是否正常运行,确保Celery Worker和Beat都已启动。 ### Q2: 密码加密密钥丢失? A2: 密钥丢失会导致无法解密现有密码,需要重新生成所有主机密码。 ### Q3: 如何备份数据? A3: ```bash # 导出数据 python manage.py dumpdata > backup.json # 导入数据 python manage.py loaddata backup.json ``` ## 许可证 MIT License ## 联系方式 如有问题或建议,请提交Issue或联系开发团队。