系统监控接口
系统监控和统计相关的 API 接口。
获取实时状态
获取系统的实时运行状态。
http
GET /api/monitor/status
Authorization: Bearer {token}响应:
json
{
"code": 200,
"data": {
"system": {
"cpu_percent": 25.5,
"memory": {
"total": 8589934592,
"used": 3865470566,
"free": 4724464026,
"percent": 45.0
},
"disk": {
"total": 107374182400,
"used": 64424509440,
"free": 42949672960,
"percent": 60.0
},
"network": {
"bytes_recv": 1250000,
"bytes_sent": 2500000,
"packets_recv": 15000,
"packets_sent": 12000
}
},
"dns": {
"qps": 1234,
"avg_response_time": 15.2,
"query_count": 1500000,
"error_count": 100,
"cache_hit_rate": 78.5
},
"cache": {
"entries": 50000,
"size": 10485760,
"hit_rate": 78.5,
"miss_rate": 21.5
},
"timestamp": "2025-01-15T10:30:25Z"
}
}获取仪表盘统计
获取仪表盘概览统计信息。
http
GET /api/dashboard/stats
Authorization: Bearer {token}响应:
json
{
"code": 200,
"data": {
"domain_count": 150,
"record_count": 2500,
"upstream_count": 5,
"user_count": 10,
"today_queries": 125678,
"today_cache_hits": 98456,
"today_cache_misses": 27222,
"avg_response_time": 12.5,
"system_uptime": 86400
}
}获取域名排行
获取被查询次数最多的域名排行。
http
GET /api/dashboard/ranking?type=domain&limit=10
Authorization: Bearer {token}查询参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| type | string | ❌ | 排行类型:domain/client,默认 domain |
| limit | int | ❌ | 数量,默认 10 |
| start | string | ❌ | 开始时间 |
| end | string | ❌ | 结束时间 |
响应:
json
{
"code": 200,
"data": {
"type": "domain",
"list": [
{"rank": 1, "name": "www.google.com", "count": 15234, "percent": 25.5},
{"rank": 2, "name": "www.baidu.com", "count": 12567, "percent": 21.0},
{"rank": 3, "name": "api.example.com", "count": 8901, "percent": 14.8}
]
}
}获取历史数据
获取指定指标的历史数据。
http
GET /api/monitor/history?metric=qps&start=2025-01-01&end=2025-01-15&interval=1h
Authorization: Bearer {token}查询参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| metric | string | ✅ | 指标名称:qps、cpu、memory、cache_hit 等 |
| start | string | ✅ | 开始时间 |
| end | string | ✅ | 结束时间 |
| interval | string | ❌ | 数据间隔:1m、1h、1d,默认 1h |
响应:
json
{
"code": 200,
"data": {
"metric": "qps",
"interval": "1h",
"start": "2025-01-15T00:00:00Z",
"end": "2025-01-15T23:59:59Z",
"points": [
{"timestamp": "2025-01-15T00:00:00Z", "value": 1200},
{"timestamp": "2025-01-15T01:00:00Z", "value": 1350},
{"timestamp": "2025-01-15T02:00:00Z", "value": 1180}
]
}
}SSE 实时数据流
通过 Server-Sent Events 实时接收监控数据。
javascript
const eventSource = new EventSource('/api/monitor/stream');
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('监控数据:', data);
// {
// "cpu": 25.5,
// "memory": 45.0,
// "qps": 1234,
// "timestamp": "2025-01-15T10:30:25Z"
// }
};
eventSource.onerror = (error) => {
console.error('SSE 错误:', error);
};数据推送频率:
- 系统资源(CPU、内存):每 5 秒
- DNS 指标(QPS):每秒
- 缓存统计:每 10 秒
使用示例
cURL 示例
bash
TOKEN="your_jwt_token"
# 获取实时状态
curl -s "http://localhost:8085/api/monitor/status" \
-H "Authorization: Bearer $TOKEN" | jq
# 获取仪表盘统计
curl -s "http://localhost:8085/api/dashboard/stats" \
-H "Authorization: Bearer $TOKEN" | jq
# 获取域名排行
curl -s "http://localhost:8085/api/dashboard/ranking?type=domain&limit=10" \
-H "Authorization: Bearer $TOKEN" | jq
# 获取历史 QPS 数据
curl -s "http://localhost:8085/api/monitor/history?metric=qps&start=2025-01-01&end=2025-01-15" \
-H "Authorization: Bearer $TOKEN" | jq
# 获取客户端排行
curl -s "http://localhost:8085/api/dashboard/ranking?type=client&limit=10" \
-H "Authorization: Bearer $TOKEN" | jqPython 示例
python
import requests
import json
class DNSGoMonitorAPI:
def __init__(self, base_url, token):
self.base_url = base_url
self.headers = {'Authorization': f'Bearer {token}'}
def get_status(self):
"""获取实时状态"""
response = requests.get(
f'{self.base_url}/api/monitor/status',
headers=self.headers
)
return response.json()
def get_dashboard_stats(self):
"""获取仪表盘统计"""
response = requests.get(
f'{self.base_url}/api/dashboard/stats',
headers=self.headers
)
return response.json()
def get_ranking(self, type='domain', limit=10, **filters):
"""获取排行数据"""
params = {'type': type, 'limit': limit, **filters}
response = requests.get(
f'{self.base_url}/api/dashboard/ranking',
headers=self.headers,
params=params
)
return response.json()
def get_history(self, metric, start, end, interval='1h'):
"""获取历史数据"""
params = {
'metric': metric,
'start': start,
'end': end,
'interval': interval
}
response = requests.get(
f'{self.base_url}/api/monitor/history',
headers=self.headers,
params=params
)
return response.json()
def stream_monitor(self, callback):
"""SSE 实时数据流"""
import sseclient
response = requests.get(
f'{self.base_url}/api/monitor/stream',
headers=self.headers,
stream=True
)
client = sseclient.SSEClient(response)
for event in client.events():
data = json.loads(event.data)
callback(data)
# 使用示例
api = DNSGoMonitorAPI('http://localhost:8085', 'your_token')
# 获取实时状态
status = api.get_status()
print(f"CPU: {status['data']['system']['cpu_percent']}%")
print(f"QPS: {status['data']['dns']['qps']}")
# 获取仪表盘统计
dashboard = api.get_dashboard_stats()
print(f"域名数量: {dashboard['data']['domain_count']}")
print(f"今日查询: {dashboard['data']['today_queries']}")
# 获取域名排行
ranking = api.get_ranking(type='domain', limit=5)
for item in ranking['data']['list']:
print(f"{item['rank']}. {item['name']}: {item['count']}")指标说明
系统指标
| 指标 | 单位 | 说明 |
|---|---|---|
| cpu_percent | % | CPU 使用率 |
| memory.total | bytes | 总内存 |
| memory.used | bytes | 已用内存 |
| memory.percent | % | 内存使用率 |
| disk.total | bytes | 总磁盘空间 |
| disk.used | bytes | 已用磁盘空间 |
| disk.percent | % | 磁盘使用率 |
| network.bytes_recv | bytes | 接收字节数 |
| network.bytes_sent | bytes | 发送字节数 |
DNS 指标
| 指标 | 单位 | 说明 |
|---|---|---|
| qps | 次/秒 | 每秒查询数 |
| avg_response_time | ms | 平均响应时间 |
| query_count | 次 | 总查询次数 |
| error_count | 次 | 错误次数 |
| cache_hit_rate | % | 缓存命中率 |
缓存指标
| 指标 | 单位 | 说明 |
|---|---|---|
| entries | 个 | 缓存条目数 |
| size | bytes | 缓存大小 |
| hit_rate | % | 命中率 |
| miss_rate | % | 未命中率 |