Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fwls/simpleMonitor

Repository files navigation

高性能监控系统

基于 Fastify + Knex.js + React + Ant Design 构建的现代化监控系统。

功能特性

1. 监控管理

  • ✅ 支持 API 监控和状态监控
  • ✅ 自定义 HTTP 请求方法、请求头、请求体
  • ✅ 可配置检查间隔和超时时间
  • ✅ 支持预期状态码和响应内容验证
  • ✅ 实时监控状态和历史记录查询

2. 触发器管理

  • ✅ 简单模式:预置常见触发条件
    • 状态码等于/不等于
    • 响应时间大于/等于
    • 状态为成功/失败
  • ✅ 高级模式:支持自定义 JavaScript 表达式
    • 可用变量:
      • statusCode: 状态码 或 证书天数
      • responseTime: 响应时间(ms)
      • status: 状态字符串 ("up" 或 "down")
      • body: 响应体内容
      • certInfo: 证书详细信息对象
    • 使用示例:
      • statusCode !== 200 - 状态码不等于200
      • responseTime > 1000 - 响应时间大于1000毫秒
      • status === "down" && body.includes("Error") - 状态为失败且响应体包含"Error"
      • certInfo && statusCode < 7 - 证书剩余天数小于7天
  • ✅ 连续失败次数配置
  • ✅ 关联多个通知渠道

3. 通知渠道

  • ✅ 邮件通知(SMTP)
  • ✅ Webhook 通知
  • ✅ 支持自定义请求头
  • ✅ 测试通知功能

4. 告警规则

  • ✅ 基于 Cron 表达式的定时执行
  • ✅ 自定义执行表达式
  • ✅ 暂停/启用规则

5. 前端管理界面

  • ✅ 现代化 UI 设计(Ant Design)
  • ✅ 实时数据更新(WebSocket)
  • ✅ 响应式布局
  • ✅ 仪表盘数据可视化
  • ✅ 操作日志和历史记录

技术栈

后端

  • Fastify: 高性能 Node.js Web 框架
  • Knex.js: SQL 查询构建器
  • Better-SQLite3: 轻量级数据库
  • Node-cron: 定时任务调度
  • Nodemailer: 邮件发送
  • Axios: HTTP 客户端
  • JWT: 身份认证

前端

  • React 18: UI 框架
  • Ant Design 5: UI 组件库
  • Axios: HTTP 客户端
  • Chart.js: 图表库
  • Day.js: 日期处理

安装和运行

1. 安装依赖

npm install

2. 初始化数据库

npm run migrate

这将创建数据库表并插入默认管理员账户:

  • 用户名: admin
  • 密码: admin123

3. 启动服务

# 开发模式
npm run dev
# 生产模式
npm start

服务将在 http://localhost:3000 启动

目录结构

monitor/
├── src/
│ ├── db/
│ │ ├── config.js # 数据库配置
│ │ ├── migrations/ # 数据库迁移
│ │ ├── seeds/ # 种子数据
│ │ └── migrate.js # 迁移执行脚本
│ ├── routes/
│ │ ├── auth.routes.js # 认证路由
│ │ ├── monitor.routes.js # 监控路由
│ │ ├── trigger.routes.js # 触发器路由
│ │ └── notification.routes.js # 通知路由
│ ├── services/
│ │ ├── monitor.service.js # 监控服务
│ │ ├── trigger.service.js # 触发器服务
│ │ ├── notification.service.js # 通知服务
│ │ └── alert.service.js # 告警服务
│ └── server.js # 主服务器文件
├── public/
│ ├── index.html # 主页面(简化版)
│ ├── loader.js # 组件加载器
│ ├── app.js # 主应用组件
│ └── components/ # 组件目录
│ ├── LoginForm.js # 登录表单组件
│ ├── Dashboard.js # 仪表盘组件
│ ├── MonitorManager.js # 监控管理组件
│ ├── TriggerManager.js # 触发器管理组件
│ ├── NotificationManager.js # 通知渠道管理组件
│ └── README.md # 组件架构说明
├── package.json
├── .gitignore
└── README.md

API 文档

认证

  • POST /api/auth/login - 用户登录
  • GET /api/auth/me - 获取当前用户信息

监控管理

  • GET /api/monitors - 获取所有监控
  • GET /api/monitors/:id - 获取单个监控
  • POST /api/monitors - 创建监控
  • PUT /api/monitors/:id - 更新监控
  • DELETE /api/monitors/:id - 删除监控
  • GET /api/monitors/:id/history - 获取监控历史
  • GET /api/monitors/:id/stats - 获取监控统计
  • POST /api/monitors/:id/check - 手动触发检查

触发器管理

  • GET /api/triggers - 获取所有触发器
  • GET /api/triggers/:id - 获取单个触发器
  • POST /api/triggers - 创建触发器
  • PUT /api/triggers/:id - 更新触发器
  • DELETE /api/triggers/:id - 删除触发器
  • GET /api/alerts - 获取告警记录
  • PUT /api/alerts/:id/resolve - 解决告警

通知渠道管理

  • GET /api/notifications - 获取所有通知渠道
  • GET /api/notifications/:id - 获取单个通知渠道
  • POST /api/notifications - 创建通知渠道
  • PUT /api/notifications/:id - 更新通知渠道
  • DELETE /api/notifications/:id - 删除通知渠道
  • POST /api/notifications/:id/test - 测试通知

使用示例

创建 API 监控

{
 "name": "网站API监控",
 "type": "api",
 "url": "https://api.example.com/health",
 "method": "GET",
 "interval": 60,
 "timeout": 10,
 "expected_status": "200",
 "active": true
}

创建简单触发器

{
 "name": "API失败告警",
 "monitor_id": 1,
 "type": "simple",
 "condition": "status_eq_failure",
 "consecutive_failures": 3,
 "notification_ids": [1]
}

创建高级触发器

{
 "name": "响应时间告警",
 "monitor_id": 1,
 "type": "advanced",
 "advanced_expression": "statusCode !== 200 || responseTime > 1000",
 "consecutive_failures": 1,
 "notification_ids": [1]
}

创建邮件通知渠道

{
 "name": "运维邮箱",
 "type": "email",
 "config": {
 "smtpHost": "smtp.example.com",
 "smtpPort": 587,
 "smtpUser": "alert@example.com",
 "smtpPass": "password",
 "recipients": "admin@example.com, ops@example.com"
 },
 "active": true
}

创建 Webhook 通知渠道

{
 "name": "钉钉机器人",
 "type": "webhook",
 "config": {
 "url": "https://oapi.dingtalk.com/robot/send?access_token=xxx",
 "headers": {
 "Content-Type": "application/json"
 }
 },
 "active": true
}

创建定时告警规则

{
 "name": "每日健康检查",
 "cron_expression": "0 9 * * *",
 "expression": "monitors.some(m => m.status === 'failure')",
 "status": "active",
 "description": "每天9点检查是否有监控失败"
}

环境变量

PORT=3000
JWT_SECRET=your-secret-key-change-in-production

WebSocket

系统使用 WebSocket 提供实时数据推送。连接地址:ws://localhost:3000/ws

消息格式:

{
 "type": "monitorCheck",
 "data": {
 "monitorId": 1,
 "monitor": {...},
 "result": {...}
 }
}

注意事项

  1. 生产环境请修改默认管理员密码
  2. 建议使用环境变量配置 JWT_SECRET
  3. 数据库文件 monitor.db 会自动创建在项目根目录
  4. 建议定期清理历史记录以控制数据库大小

许可证

MIT

About

基于nodejs的简单http状态以及证书监控

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

Languages

AltStyle によって変換されたページ (->オリジナル) /