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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
---
title: '监控与分析'
description: '全面监控 MCP 服务器性能、健康状况和使用情况'
---

## 概述

MCPHub 提供全面的监控和分析功能,帮助您跟踪 MCP 服务器的性能、健康状况和使用模式。系统提供实时指标、历史分析和智能警报。

## 实时监控

### 仪表板概览

主监控仪表板显示关键指标:

```bash
# 访问监控仪表板
curl -X GET http://localhost:3000/api/monitoring/dashboard \
  -H "Authorization: Bearer $TOKEN"
```

响应示例:

```json
{
  "overview": {
    "totalServers": 12,
    "activeServers": 10,
    "totalRequests": 15420,
    "avgResponseTime": "245ms",
    "errorRate": "0.3%",
    "uptime": "99.9%"
  },
  "realtime": {
    "requestsPerMinute": 85,
    "activeConnections": 156,
    "memoryUsage": "68%",
    "cpuUsage": "42%"
  }
}
```

### 服务器健康状态

```bash
# 获取所有服务器状态
curl -X GET http://localhost:3000/api/monitoring/servers/health \
  -H "Authorization: Bearer $TOKEN"
```

## 性能指标

### 响应时间监控

```json
{
  "responseTime": {
    "metrics": [
      {
        "serverId": "server-1",
        "average": "180ms",
        "p50": "150ms",
        "p95": "300ms",
        "p99": "500ms"
      }
    ],
    "alerts": {
      "slowResponse": {
        "threshold": "1s",
        "current": "180ms",
        "status": "normal"
      }
    }
  }
}
```

### 吞吐量分析

```bash
# 获取吞吐量统计
curl -X GET http://localhost:3000/api/monitoring/throughput \
  -H "Authorization: Bearer $TOKEN" \
  -G -d "timeRange=1h" -d "granularity=5m"
```

响应示例:

```json
{
  "timeRange": "1h",
  "granularity": "5m",
  "data": [
    {
      "timestamp": "2024-01-01T12:00:00Z",
      "totalRequests": 450,
      "successfulRequests": 448,
      "failedRequests": 2,
      "avgResponseTime": "230ms"
    }
  ],
  "summary": {
    "peakThroughput": 520,
    "averageThroughput": 412,
    "totalRequests": 4944
  }
}
```

### 资源使用监控

```json
{
  "resources": {
    "cpu": {
      "usage": "42%",
      "cores": 8,
      "processes": [
        { "name": "mcp-server-1", "cpu": "15%" },
        { "name": "mcp-server-2", "cpu": "12%" }
      ]
    },
    "memory": {
      "total": "32GB",
      "used": "21.6GB",
      "available": "10.4GB",
      "byProcess": [
        { "name": "mcp-server-1", "memory": "2.1GB" },
        { "name": "mcp-server-2", "memory": "1.8GB" }
      ]
    },
    "network": {
      "bytesIn": "1.2GB",
      "bytesOut": "890MB",
      "packetsIn": 1250000,
      "packetsOut": 980000
    },
    "disk": {
      "total": "1TB",
      "used": "340GB",
      "available": "660GB",
      "iops": 1200
    }
  }
}
```

## 日志管理

### 集中化日志收集

配置日志聚合:

```json
{
  "logging": {
    "centralized": true,
    "storage": {
      "type": "elasticsearch",
      "config": {
        "hosts": ["localhost:9200"],
        "index": "mcphub-logs",
        "retention": "30d"
      }
    },
    "levels": ["error", "warn", "info", "debug"],
    "sources": ["application", "mcp-servers", "nginx", "system"]
  }
}
```

### 实时日志查看

```bash
# 获取实时日志流
curl -X GET http://localhost:3000/api/monitoring/logs/stream \
  -H "Authorization: Bearer $TOKEN" \
  -H "Accept: text/event-stream"

# 过滤日志
curl -X GET http://localhost:3000/api/monitoring/logs \
  -H "Authorization: Bearer $TOKEN" \
  -G -d "level=error" -d "server=server-1" -d "limit=100"
```

### 日志分析

```bash
# 错误模式分析
curl -X GET http://localhost:3000/api/monitoring/logs/analysis \
  -H "Authorization: Bearer $TOKEN" \
  -G -d "type=error-patterns" -d "timeRange=24h"
```

响应示例:

```json
{
  "analysis": {
    "errorPatterns": [
      {
        "pattern": "Connection timeout",
        "occurrences": 45,
        "trend": "increasing",
        "affectedServers": ["server-2", "server-3"]
      },
      {
        "pattern": "Memory allocation failed",
        "occurrences": 12,
        "trend": "stable",
        "affectedServers": ["server-1"]
      }
    ],
    "recommendations": ["检查服务器网络连接", "增加内存限制配置"]
  }
}
```

## 警报系统

### 配置警报规则

```json
{
  "alerts": [
    {
      "name": "high-response-time",
      "description": "响应时间过高警报",
      "condition": "avg(response_time) > 1000ms over 5m",
      "severity": "warning",
      "notifications": {
        "email": ["admin@company.com"],
        "slack": "#alerts",
        "webhook": "https://hooks.company.com/alerts"
      }
    },
    {
      "name": "server-down",
      "description": "服务器宕机警报",
      "condition": "server_status == 'down'",
      "severity": "critical",
      "notifications": {
        "email": ["oncall@company.com"],
        "sms": ["+1234567890"],
        "pagerduty": "service-key"
      }
    },
    {
      "name": "high-error-rate",
      "description": "错误率过高",
      "condition": "error_rate > 5% over 10m",
      "severity": "error",
      "notifications": {
        "slack": "#dev-team"
      }
    }
  ]
}
```

### 警报通知

```bash
# 测试警报通知
curl -X POST http://localhost:3000/api/monitoring/alerts/test \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "alertName": "high-response-time",
    "channel": "email"
  }'
```

### 警报历史

```bash
# 获取警报历史
curl -X GET http://localhost:3000/api/monitoring/alerts/history \
  -H "Authorization: Bearer $TOKEN" \
  -G -d "timeRange=7d" -d "severity=critical"
```

## 用户活动监控

### 用户会话跟踪

```json
{
  "userActivity": {
    "activeSessions": 156,
    "totalUsers": 89,
    "sessionsByUser": [
      {
        "userId": "user123",
        "sessions": 3,
        "lastActivity": "2024-01-01T12:30:00Z",
        "totalRequests": 245
      }
    ],
    "topUsers": [
      { "userId": "power-user", "requests": 1250 },
      { "userId": "regular-user", "requests": 890 }
    ]
  }
}
```

### 使用模式分析

```bash
# 获取使用模式分析
curl -X GET http://localhost:3000/api/monitoring/usage-patterns \
  -H "Authorization: Bearer $TOKEN" \
  -G -d "timeRange=30d"
```

响应示例:

```json
{
  "patterns": {
    "peakHours": [9, 10, 14, 15, 16],
    "peakDays": ["tuesday", "wednesday", "thursday"],
    "toolUsage": [
      {"tool": "filesystem", "usage": 45%},
      {"tool": "web-search", "usage": 30%},
      {"tool": "database", "usage": 25%}
    ],
    "userBehavior": {
      "avgSessionDuration": "45m",
      "avgRequestsPerSession": 23,
      "returnRate": "78%"
    }
  }
}
```

## 容量规划

### 资源预测

```json
{
  "capacityPlanning": {
    "currentCapacity": {
      "cpu": "42%",
      "memory": "68%",
      "network": "35%",
      "storage": "34%"
    },
    "predictions": {
      "timeHorizon": "30d",
      "cpuForecast": [
        { "date": "2024-01-07", "usage": "48%" },
        { "date": "2024-01-14", "usage": "52%" },
        { "date": "2024-01-21", "usage": "56%" },
        { "date": "2024-01-28", "usage": "61%" }
      ],
      "recommendations": [
        "考虑在第3周增加CPU资源",
        "内存使用稳定,暂不需要扩容",
        "建议监控网络带宽趋势"
      ]
    }
  }
}
```

### 自动扩缩容

```json
{
  "autoScaling": {
    "enabled": true,
    "policies": [
      {
        "metric": "cpu_usage",
        "scaleUp": {
          "threshold": "80%",
          "duration": "5m",
          "action": "add_instance"
        },
        "scaleDown": {
          "threshold": "30%",
          "duration": "15m",
          "action": "remove_instance"
        }
      },
      {
        "metric": "request_queue_length",
        "scaleUp": {
          "threshold": 100,
          "duration": "2m",
          "action": "add_instance"
        }
      }
    ]
  }
}
```

## 性能分析报告

### 生成性能报告

```bash
# 生成周报
curl -X POST http://localhost:3000/api/monitoring/reports \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "type": "performance",
    "timeRange": "7d",
    "format": "pdf",
    "sections": [
      "overview",
      "response_times",
      "error_analysis",
      "resource_usage",
      "recommendations"
    ],
    "recipients": ["manager@company.com"]
  }'
```

### 自定义报告

```json
{
  "customReport": {
    "name": "monthly-summary",
    "schedule": "0 0 1 * *",
    "template": "executive-summary",
    "data": {
      "kpis": ["uptime", "avg_response_time", "total_requests", "error_rate", "user_satisfaction"],
      "comparisons": {
        "previousMonth": true,
        "yearOverYear": true
      }
    },
    "distribution": {
      "email": ["executives@company.com"],
      "dashboard": true,
      "archive": true
    }
  }
}
```

## 监控 API

### 指标查询

```bash
# 查询自定义指标
curl -X POST http://localhost:3000/api/monitoring/query \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "query": "avg(response_time) by (server_id)",
    "timeRange": "1h",
    "step": "5m"
  }'
```

### 事件跟踪

```bash
# 记录自定义事件
curl -X POST http://localhost:3000/api/monitoring/events \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "type": "deployment",
    "title": "服务器更新",
    "description": "部署新版本 v2.1.0",
    "tags": ["deployment", "version-2.1.0"],
    "metadata": {
      "version": "2.1.0",
      "servers": ["server-1", "server-2"]
    }
  }'
```

## 第三方集成

### Prometheus 集成

```yaml
# prometheus.yml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'mcphub'
    static_configs:
      - targets: ['localhost:3000']
    metrics_path: '/api/monitoring/prometheus'
    scrape_interval: 30s
```

### Grafana 仪表板

```json
{
  "dashboard": {
    "title": "MCPHub 监控",
    "panels": [
      {
        "title": "请求率",
        "type": "graph",
        "targets": [
          {
            "expr": "rate(mcphub_requests_total[5m])",
            "legendFormat": "{{server_id}}"
          }
        ]
      },
      {
        "title": "响应时间",
        "type": "graph",
        "targets": [
          {
            "expr": "histogram_quantile(0.95, mcphub_response_time_histogram)",
            "legendFormat": "95th percentile"
          }
        ]
      }
    ]
  }
}
```

### ELK Stack 集成

```json
{
  "logstash": {
    "input": {
      "beats": {
        "port": 5044
      }
    },
    "filter": {
      "if": "[fields][service] == 'mcphub'",
      "json": {
        "source": "message"
      },
      "date": {
        "match": ["timestamp", "ISO8601"]
      }
    },
    "output": {
      "elasticsearch": {
        "hosts": ["localhost:9200"],
        "index": "mcphub-logs-%{+YYYY.MM.dd}"
      }
    }
  }
}
```

## 故障排除

### 监控问题诊断

```bash
# 检查监控服务状态
curl -X GET http://localhost:3000/api/monitoring/health \
  -H "Authorization: Bearer $TOKEN"

# 验证指标收集
curl -X GET http://localhost:3000/api/monitoring/metrics/validation \
  -H "Authorization: Bearer $TOKEN"
```

### 性能问题分析

```bash
# 性能瓶颈分析
curl -X GET http://localhost:3000/api/monitoring/performance/bottlenecks \
  -H "Authorization: Bearer $TOKEN" \
  -G -d "timeRange=1h"
```

### 常见监控问题

1. **指标丢失**

   - 检查收集器配置
   - 验证网络连接
   - 检查存储空间

2. **警报不触发**

   - 验证警报规则语法
   - 检查通知配置
   - 测试通知渠道

3. **仪表板加载缓慢**
   - 优化查询时间范围
   - 增加数据聚合
   - 检查数据库性能

## 最佳实践

1. **监控层次化**: 建立应用、基础设施和业务三层监控
2. **合理的警报**: 避免警报疲劳,设置合适的阈值
3. **数据保留**: 根据业务需求设置适当的数据保留期
4. **安全监控**: 监控安全相关的指标和事件
5. **持续优化**: 定期审查和优化监控配置

有关更多信息,请参阅 [智能路由](/zh/features/smart-routing) 和 [服务器管理](/zh/features/server-management) 文档。