init
This commit is contained in:
1 parent
c7fccf99c7
commit
8471f368eb
33 files changed
+444
-2095
No files matched your search
@@ -23,7 +23,7 @@ async def list_audit_rules(
|
||||
db_type: Optional[str] = None,
|
||||
is_enabled: Optional[bool] = None,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin", "dba")),
|
||||
current_user: User = Depends(require_role("admin", "dba", "auditor")),
|
||||
):
|
||||
stmt = select(AuditRule).order_by(AuditRule.id)
|
||||
if rule_type:
|
||||
@@ -62,7 +62,7 @@ async def create_audit_rule(
|
||||
@router.get("/stats/summary")
|
||||
async def rule_stats(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin", "dba")),
|
||||
current_user: User = Depends(require_role("admin", "dba", "auditor")),
|
||||
):
|
||||
total_stmt = select(func.count(AuditRule.id))
|
||||
total = (await db.execute(total_stmt)).scalar()
|
||||
@@ -130,7 +130,7 @@ async def seed_audit_rules(
|
||||
async def get_audit_rule(
|
||||
rule_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin", "dba")),
|
||||
current_user: User = Depends(require_role("admin", "dba", "auditor")),
|
||||
):
|
||||
result = await db.execute(select(AuditRule).where(AuditRule.id == rule_id))
|
||||
rule = result.scalar_one_or_none()
|
||||
|
||||
@@ -49,7 +49,6 @@ DB_TYPE_INFO: Dict[str, Dict[str, Any]] = {
|
||||
"查看索引": "SHOW INDEX FROM table_name",
|
||||
"查看连接": "SHOW PROCESSLIST",
|
||||
"查看状态": "SHOW STATUS LIKE 'Threads%'",
|
||||
"查看慢查询": "SELECT * FROM mysql.slow_log ORDER BY start_time DESC LIMIT 20",
|
||||
},
|
||||
"limitations": [
|
||||
"不支持存储过程调用",
|
||||
|
||||
@@ -217,42 +217,6 @@ async def show_grants(
|
||||
raise HTTPException(status_code=500, detail="查询权限失败: " + str(e))
|
||||
|
||||
|
||||
# ---- Slow Queries ----
|
||||
|
||||
@router.get("/{instance_id}/detail/slow-queries")
|
||||
async def slow_queries(
|
||||
instance_id: int,
|
||||
database: Optional[str] = Query(None, description="数据库名"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin", "dba")),
|
||||
):
|
||||
instance = await _get_decrypted_instance(db, instance_id)
|
||||
# P0: 权限检查
|
||||
await check_instance_access(current_user, instance, db)
|
||||
if instance.db_type != "mysql":
|
||||
raise HTTPException(status_code=400, detail="仅支持MySQL实例")
|
||||
try:
|
||||
# Try querying mysql.slow_log table first
|
||||
try:
|
||||
sql = (
|
||||
"SELECT start_time, user_host, query_time, lock_time, "
|
||||
"rows_sent, rows_examined, sql_text "
|
||||
"FROM mysql.slow_log ORDER BY start_time DESC LIMIT 100"
|
||||
)
|
||||
rows = await _mysql_query(instance, sql, database=database)
|
||||
return rows
|
||||
except Exception:
|
||||
# Fallback: show slow query variables
|
||||
rows = await _mysql_query(
|
||||
instance,
|
||||
"SHOW VARIABLES LIKE 'slow%'",
|
||||
database=database,
|
||||
)
|
||||
return rows
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail="查询慢查询失败: " + str(e))
|
||||
|
||||
|
||||
# ---- Processlist ----
|
||||
|
||||
@router.get("/{instance_id}/detail/processlist")
|
||||
|
||||
@@ -100,12 +100,11 @@ async def test_ldap_connection(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin")),
|
||||
):
|
||||
"""测试 LDAP 连接 — 使用 mock 模拟"""
|
||||
"""测试 LDAP 连接"""
|
||||
ldap = await _get_existing(db)
|
||||
if not ldap:
|
||||
raise HTTPException(status_code=404, detail="LDAP 配置不存在,请先创建")
|
||||
|
||||
# Use mock for testing — python-ldap may not be installed
|
||||
try:
|
||||
import ldap as _ldap # noqa
|
||||
# Real LDAP test path
|
||||
@@ -131,12 +130,9 @@ async def test_ldap_connection(
|
||||
return {"success": True, "message": f"LDAP 连接成功,用户 {data.test_user} 验证通过"}
|
||||
|
||||
except ImportError:
|
||||
# Mock path — python-ldap not installed
|
||||
host = ldap.ldap_host
|
||||
port = ldap.ldap_port
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"[Mock] LDAP 连接模拟成功 ({host}:{port}),用户 {data.test_user} 验证通过",
|
||||
"success": False,
|
||||
"message": "python-ldap 未安装,无法测试 LDAP 连接。请执行: pip install python-ldap",
|
||||
}
|
||||
except Exception as e:
|
||||
return {"success": False, "message": f"LDAP 连接失败: {str(e)}"}
|
||||
@@ -124,7 +124,9 @@ async def test_notification(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin")),
|
||||
):
|
||||
"""测试发送通知 — 目前返回模拟结果"""
|
||||
"""测试发送通知 — 实际发送测试消息到配置的渠道。"""
|
||||
from app.services.notification_sender import NotificationSender
|
||||
|
||||
result = await db.execute(
|
||||
select(NotificationChannel).where(NotificationChannel.id == channel_id)
|
||||
)
|
||||
@@ -133,24 +135,19 @@ async def test_notification(
|
||||
raise HTTPException(status_code=404, detail="通知渠道不存在")
|
||||
|
||||
config = json.loads(channel.config) if channel.config else {}
|
||||
channel_type = channel.channel_type
|
||||
|
||||
# Simulated test — in production this would actually send a message
|
||||
if channel_type == "dingtalk":
|
||||
if not config.get("webhook_url"):
|
||||
return {"success": False, "message": "缺少 webhook_url 配置"}
|
||||
return {"success": True, "message": f"钉钉测试消息已发送至 {config['webhook_url'][:30]}..."}
|
||||
|
||||
elif channel_type == "webhook_feishu":
|
||||
if not config.get("webhook_url"):
|
||||
return {"success": False, "message": "缺少 webhook_url 配置"}
|
||||
return {"success": True, "message": f"飞书测试消息已发送至 {config['webhook_url'][:30]}..."}
|
||||
|
||||
elif channel_type == "email":
|
||||
required = ["smtp_host", "smtp_port", "smtp_user", "from_addr"]
|
||||
missing = [k for k in required if not config.get(k)]
|
||||
if missing:
|
||||
return {"success": False, "message": f"缺少必要配置: {', '.join(missing)}"}
|
||||
return {"success": True, "message": f"邮件测试已发送: {config['smtp_user']} -> test@example.com"}
|
||||
|
||||
return {"success": False, "message": f"不支持的通知类型: {channel_type}"}
|
||||
sender = NotificationSender()
|
||||
test_data = {
|
||||
"title": "测试通知",
|
||||
"applicant": current_user.display_name or current_user.username,
|
||||
"db_type": "mysql",
|
||||
"target_database": "test_db",
|
||||
"sql_preview": "SELECT 1",
|
||||
"priority": "normal",
|
||||
"approver": "系统",
|
||||
"ticket_id": "TEST",
|
||||
"result": "成功",
|
||||
"duration": "10",
|
||||
"affected_rows": "0",
|
||||
}
|
||||
result = await sender.send(channel.channel_type, config, "ticket_created", test_data)
|
||||
return result
|
||||
@@ -1,103 +0,0 @@
|
||||
"""慢日志 API。"""
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.security import get_current_user, require_role
|
||||
from app.core.crypto import decrypt_password
|
||||
from app.models.user import User
|
||||
from app.models.instance import Instance
|
||||
from app.services.slow_log_collector import SlowLogCollector
|
||||
|
||||
router = APIRouter(prefix="/slow-logs", tags=["慢日志分析"])
|
||||
|
||||
|
||||
@router.get("/instances/{instance_id}")
|
||||
async def list_slow_logs(
|
||||
instance_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin", "dba")),
|
||||
):
|
||||
"""获取指定实例的慢日志。"""
|
||||
result = await db.execute(select(Instance).where(Instance.id == instance_id))
|
||||
instance = result.scalar_one_or_none()
|
||||
if not instance:
|
||||
raise HTTPException(status_code=404, detail="数据源不存在")
|
||||
|
||||
collector = SlowLogCollector()
|
||||
logs = collector.get_slow_logs(instance_id)
|
||||
|
||||
return {
|
||||
"instance_id": instance_id,
|
||||
"total": len(logs),
|
||||
"logs": [log.to_dict() for log in logs],
|
||||
}
|
||||
|
||||
|
||||
@router.post("/instances/{instance_id}/collect")
|
||||
async def collect_slow_logs(
|
||||
instance_id: int,
|
||||
database: str = "",
|
||||
limit: int = 50,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin", "dba")),
|
||||
):
|
||||
"""手动收集慢日志。"""
|
||||
result = await db.execute(select(Instance).where(Instance.id == instance_id))
|
||||
instance = result.scalar_one_or_none()
|
||||
if not instance:
|
||||
raise HTTPException(status_code=404, detail="数据源不存在")
|
||||
|
||||
if not database:
|
||||
database = instance.database or ""
|
||||
|
||||
collector = SlowLogCollector()
|
||||
instance.password = decrypt_password(instance.password)
|
||||
|
||||
try:
|
||||
if instance.db_type == "mysql":
|
||||
logs = await collector.collect_mysql_slow_logs(instance, database, limit)
|
||||
elif instance.db_type in ("postgres", "postgresql"):
|
||||
logs = await collector.collect_postgres_slow_logs(instance, database, limit)
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=f"不支持的类型: {instance.db_type}")
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"收集失败: {str(e)}")
|
||||
|
||||
await collector.save_slow_logs(instance_id, logs)
|
||||
report = collector.get_report(instance_id)
|
||||
|
||||
return report.to_dict()
|
||||
|
||||
|
||||
@router.get("/instances/{instance_id}/report")
|
||||
async def slow_log_report(
|
||||
instance_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role("admin", "dba")),
|
||||
):
|
||||
"""获取慢日志分析报告。"""
|
||||
collector = SlowLogCollector()
|
||||
report = collector.get_report(instance_id)
|
||||
return report.to_dict()
|
||||
|
||||
|
||||
@router.get("/dashboard/top")
|
||||
async def dashboard_top_queries(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""仪表盘慢查询 TOP 10。"""
|
||||
collector = SlowLogCollector()
|
||||
all_logs = []
|
||||
for instance_id, logs in collector._logs.items():
|
||||
all_logs.extend(logs)
|
||||
|
||||
# 全局 TOP 10
|
||||
top = sorted(all_logs, key=lambda x: x.avg_time_ms, reverse=True)[:10]
|
||||
return {
|
||||
"total_instances": len(collector._logs),
|
||||
"top_queries": [q.to_dict() for q in top],
|
||||
}
|
||||
@@ -5,7 +5,7 @@ from app.api.v1.endpoints import (
|
||||
announcement, notification, ldap_config,
|
||||
instance_detail, backup, redis, dictionary,
|
||||
instance_group, masking_rule, rollback,
|
||||
workflow, impact, schema_drift, slow_log, pool,
|
||||
workflow, impact, schema_drift, pool,
|
||||
mongo, query_history, sql_assistant, db_types,
|
||||
settings,
|
||||
)
|
||||
@@ -34,7 +34,6 @@ api_router.include_router(rollback.router)
|
||||
api_router.include_router(workflow.router)
|
||||
api_router.include_router(impact.router)
|
||||
api_router.include_router(schema_drift.router)
|
||||
api_router.include_router(slow_log.router)
|
||||
api_router.include_router(pool.router)
|
||||
api_router.include_router(query_history.router)
|
||||
api_router.include_router(sql_assistant.router)
|
||||
|
||||
+20
-11
@@ -29,17 +29,27 @@ async def seed_workflows():
|
||||
|
||||
|
||||
async def seed_data():
|
||||
"""Create default admin user from environment config."""
|
||||
"""Create default users with different roles."""
|
||||
from app.core.security import hash_password
|
||||
|
||||
default_users = [
|
||||
{"username": "admin", "display_name": "超级管理员", "role": "admin", "password": "admin123"},
|
||||
{"username": "dba", "display_name": "张三", "role": "dba", "password": "admin123"},
|
||||
{"username": "auditor", "display_name": "老六", "role": "auditor", "password": "admin123"},
|
||||
{"username": "developer", "display_name": "路人甲", "role": "developer", "password": "admin123"},
|
||||
]
|
||||
|
||||
async with async_session() as db:
|
||||
result = await db.execute(select(User).where(User.username == settings.ADMIN_USERNAME))
|
||||
if not result.scalar_one_or_none():
|
||||
db.add(User(
|
||||
username=settings.ADMIN_USERNAME,
|
||||
display_name="管理员",
|
||||
role="admin",
|
||||
hashed_password=hash_password(settings.ADMIN_PASSWORD),
|
||||
))
|
||||
await db.commit()
|
||||
for u in default_users:
|
||||
result = await db.execute(select(User).where(User.username == u["username"]))
|
||||
if not result.scalar_one_or_none():
|
||||
db.add(User(
|
||||
username=u["username"],
|
||||
display_name=u["display_name"],
|
||||
role=u["role"],
|
||||
hashed_password=hash_password(u["password"]),
|
||||
))
|
||||
await db.commit()
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -62,7 +72,6 @@ async def lifespan(app: FastAPI):
|
||||
import app.models.notification_template # noqa
|
||||
import app.models.scheduled_task # noqa
|
||||
import app.models.schema_revision # noqa
|
||||
import app.models.slow_log # noqa
|
||||
import app.models.query_history # noqa
|
||||
|
||||
await init_db()
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
"""慢日志模型。"""
|
||||
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, func
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class SlowLog(Base):
|
||||
"""慢日志记录。"""
|
||||
__tablename__ = "slow_logs"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
instance_id = Column(Integer, ForeignKey("instances.id"), nullable=False)
|
||||
database = Column(String(100), nullable=True)
|
||||
sql_digest = Column(String(64), nullable=True)
|
||||
sql_text = Column(Text, nullable=True)
|
||||
execution_count = Column(Integer, default=1)
|
||||
avg_time_ms = Column(Integer, nullable=True)
|
||||
max_time_ms = Column(Integer, nullable=True)
|
||||
rows_examined = Column(Integer, nullable=True)
|
||||
rows_sent = Column(Integer, nullable=True)
|
||||
created_at = Column(DateTime, server_default=func.now())
|
||||
@@ -13,7 +13,7 @@ class User(Base):
|
||||
email = Column(String(100), unique=True, nullable=True)
|
||||
phone = Column(String(20), nullable=True)
|
||||
hashed_password = Column(String(200), nullable=False)
|
||||
role = Column(String(20), nullable=False, default="developer") # admin / dba / developer / guest
|
||||
role = Column(String(20), nullable=False, default="developer") # admin / dba / auditor / developer / guest
|
||||
is_active = Column(Boolean, default=True)
|
||||
last_login = Column(DateTime, nullable=True)
|
||||
created_at = Column(DateTime, server_default=func.now())
|
||||
|
||||
@@ -18,40 +18,40 @@ class NotificationSender:
|
||||
# 内置消息模板
|
||||
TEMPLATES = {
|
||||
"ticket_created": {
|
||||
"title": "📝 新工单待审批: {title}",
|
||||
"body": "**申请人**: {applicant}\\n**数据库**: {db_type}@{target_database}\\n**SQL摘要**: {sql_preview}\\n**优先级**: {priority}",
|
||||
"title": "新工单待审批: {title}",
|
||||
"body": "**申请人**: {applicant}\n**数据库**: {db_type}@{target_database}\n**SQL摘要**: {sql_preview}",
|
||||
},
|
||||
"ticket_approved": {
|
||||
"title": "✅ 工单已审批通过: {title}",
|
||||
"body": "**审批人**: {approver}\\n**工单ID**: {ticket_id}\\n**状态**: 已批准,可执行",
|
||||
"title": "工单已审批通过: {title}",
|
||||
"body": "**审批人**: {approver}\n**工单ID**: {ticket_id}\n**状态**: 已批准,可执行",
|
||||
},
|
||||
"ticket_rejected": {
|
||||
"title": "❌ 工单已被拒绝: {title}",
|
||||
"body": "**审批人**: {approver}\\n**拒绝原因**: {comment}\\n**工单ID**: {ticket_id}",
|
||||
"title": "工单已被拒绝: {title}",
|
||||
"body": "**审批人**: {approver}\n**拒绝原因**: {comment}\n**工单ID**: {ticket_id}",
|
||||
},
|
||||
"ticket_executed": {
|
||||
"title": "🚀 工单执行完成: {title}",
|
||||
"body": "**执行结果**: {result}\\n**耗时**: {duration}ms\\n**影响行数**: {affected_rows}",
|
||||
"title": "工单执行完成: {title}",
|
||||
"body": "**执行结果**: {result}\n**耗时**: {duration}ms\n**影响行数**: {affected_rows}",
|
||||
},
|
||||
"ticket_failed": {
|
||||
"title": "⚠️ 工单执行失败: {title}",
|
||||
"body": "**错误信息**: {error}\\n**工单ID**: {ticket_id}\\n请及时处理",
|
||||
"title": "工单执行失败: {title}",
|
||||
"body": "**错误信息**: {error}\n**工单ID**: {ticket_id}\n请及时处理",
|
||||
},
|
||||
"ticket_rollback": {
|
||||
"title": "🔄 工单已回滚: {title}",
|
||||
"body": "**回滚人**: {operator}\\n**工单ID**: {ticket_id}",
|
||||
"title": "工单已回滚: {title}",
|
||||
"body": "**回滚人**: {operator}\n**工单ID**: {ticket_id}",
|
||||
},
|
||||
"backup_completed": {
|
||||
"title": "💾 备份完成: {title}",
|
||||
"body": "**数据库**: {database}\\n**文件大小**: {size}\\n**耗时**: {duration}s",
|
||||
"title": "备份完成: {title}",
|
||||
"body": "**数据库**: {database}\n**文件大小**: {size}\n**耗时**: {duration}s",
|
||||
},
|
||||
"backup_failed": {
|
||||
"title": "❌ 备份失败: {title}",
|
||||
"body": "**错误信息**: {error}\\n请检查数据库连接和磁盘空间",
|
||||
"title": "备份失败: {title}",
|
||||
"body": "**错误信息**: {error}\n请检查数据库连接和磁盘空间",
|
||||
},
|
||||
"schema_drift_detected": {
|
||||
"title": "🔍 检测到 Schema 漂移: {instance_name}",
|
||||
"body": "**数据库**: {database}\\n**发现 {drift_count} 处变更**:\\n{drift_details}",
|
||||
"title": "检测到 Schema 漂移: {instance_name}",
|
||||
"body": "**数据库**: {database}\n**发现 {drift_count} 处变更**:\n{drift_details}",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -101,6 +101,9 @@ class NotificationSender:
|
||||
title = template["title"].format(**data)
|
||||
body = template["body"].format(**data)
|
||||
|
||||
# 钉钉 markdown 单个 \n 视为空格,需要 \n\n 才能换行
|
||||
body = body.replace("\n", "\n\n")
|
||||
|
||||
payload = {
|
||||
"msgtype": "markdown",
|
||||
"markdown": {
|
||||
|
||||
@@ -1,269 +0,0 @@
|
||||
"""慢日志收集器 — 从数据库收集和分析慢查询日志。
|
||||
|
||||
支持:
|
||||
- MySQL: performance_schema.events_statements_summary_by_digest
|
||||
- PostgreSQL: pg_stat_statements
|
||||
|
||||
生成慢查询报告,包含 TOP N 慢查询、执行时间趋势等。
|
||||
"""
|
||||
import hashlib
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
from app.models.instance import Instance
|
||||
from app.services.db_connector import DBConnector
|
||||
from app.core.crypto import decrypt_password
|
||||
|
||||
|
||||
@dataclass
|
||||
class SlowLogEntry:
|
||||
"""慢日志条目。"""
|
||||
sql_digest: str # SQL 指纹
|
||||
sql_text: str # 归一化 SQL
|
||||
execution_count: int # 执行次数
|
||||
avg_time_ms: float # 平均执行时间(ms)
|
||||
max_time_ms: float # 最大执行时间(ms)
|
||||
min_time_ms: float # 最小执行时间(ms)
|
||||
rows_examined: int # 扫描行数
|
||||
rows_sent: int # 返回行数
|
||||
first_seen: Optional[str] = None
|
||||
last_seen: Optional[str] = None
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"sql_digest": self.sql_digest,
|
||||
"sql_text": self.sql_text[:200] if self.sql_text else "",
|
||||
"execution_count": self.execution_count,
|
||||
"avg_time_ms": round(self.avg_time_ms, 2),
|
||||
"max_time_ms": round(self.max_time_ms, 2),
|
||||
"min_time_ms": round(self.min_time_ms, 2),
|
||||
"rows_examined": self.rows_examined,
|
||||
"rows_sent": self.rows_sent,
|
||||
"first_seen": self.first_seen,
|
||||
"last_seen": self.last_seen,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class SlowLogReport:
|
||||
"""慢日志分析报告。"""
|
||||
total_slow_queries: int
|
||||
avg_execution_time_ms: float
|
||||
max_execution_time_ms: float
|
||||
top_queries: List[SlowLogEntry]
|
||||
time_distribution: Dict[str, int] # 按时间段分布
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"total_slow_queries": self.total_slow_queries,
|
||||
"avg_execution_time_ms": round(self.avg_execution_time_ms, 2),
|
||||
"max_execution_time_ms": round(self.max_execution_time_ms, 2),
|
||||
"top_queries": [q.to_dict() for q in self.top_queries],
|
||||
"time_distribution": self.time_distribution,
|
||||
}
|
||||
|
||||
|
||||
class SlowLogCollector:
|
||||
"""慢日志收集器。"""
|
||||
|
||||
# 内存中的慢日志存储: {instance_id: [SlowLogEntry, ...]}
|
||||
_logs: Dict[int, List[SlowLogEntry]] = {}
|
||||
|
||||
async def collect_mysql_slow_logs(self, instance: Instance,
|
||||
database: str,
|
||||
limit: int = 50) -> List[SlowLogEntry]:
|
||||
"""从 MySQL performance_schema 收集慢日志。
|
||||
|
||||
需要 MySQL 开启 performance_schema。
|
||||
"""
|
||||
sql = """
|
||||
SELECT
|
||||
DIGEST_TEXT as sql_text,
|
||||
COUNT_STAR as execution_count,
|
||||
ROUND(AVG_TIMER_WAIT / 1000000000, 3) as avg_time_ms,
|
||||
ROUND(MAX_TIMER_WAIT / 1000000000, 3) as max_time_ms,
|
||||
ROUND(MIN_TIMER_WAIT / 1000000000, 3) as min_time_ms,
|
||||
SUM_ROWS_EXAMINED as rows_examined,
|
||||
SUM_ROWS_SENT as rows_sent,
|
||||
FIRST_SEEN,
|
||||
LAST_SEEN
|
||||
FROM performance_schema.events_statements_summary_by_digest
|
||||
WHERE SCHEMA_NAME = %s
|
||||
AND AVG_TIMER_WAIT > 100000000000 -- > 100ms
|
||||
ORDER BY AVG_TIMER_WAIT DESC
|
||||
LIMIT %s
|
||||
"""
|
||||
|
||||
try:
|
||||
instance.password = decrypt_password(instance.password)
|
||||
rows, _ = await DBConnector._query_mysql(instance, "performance_schema", sql)
|
||||
except Exception as e:
|
||||
# performance_schema 可能不可用,使用替代查询
|
||||
return await self._collect_mysql_slow_logs_fallback(instance, database, limit)
|
||||
|
||||
entries = []
|
||||
for row in rows:
|
||||
sql_text = row.get("sql_text", "")
|
||||
digest = hashlib.md5(sql_text.encode()).hexdigest()[:16]
|
||||
entries.append(SlowLogEntry(
|
||||
sql_digest=digest,
|
||||
sql_text=sql_text[:500],
|
||||
execution_count=int(row.get("execution_count", 0) or 0),
|
||||
avg_time_ms=float(row.get("avg_time_ms", 0) or 0),
|
||||
max_time_ms=float(row.get("max_time_ms", 0) or 0),
|
||||
min_time_ms=float(row.get("min_time_ms", 0) or 0),
|
||||
rows_examined=int(row.get("rows_examined", 0) or 0),
|
||||
rows_sent=int(row.get("rows_sent", 0) or 0),
|
||||
first_seen=str(row.get("FIRST_SEEN", "")) if row.get("FIRST_SEEN") else None,
|
||||
last_seen=str(row.get("LAST_SEEN", "")) if row.get("LAST_SEEN") else None,
|
||||
))
|
||||
|
||||
return entries
|
||||
|
||||
async def _collect_mysql_slow_logs_fallback(self, instance: Instance,
|
||||
database: str,
|
||||
limit: int) -> List[SlowLogEntry]:
|
||||
"""备用方案:使用 SHOW PROCESSLIST 获取当前慢查询。"""
|
||||
sql = "SHOW FULL PROCESSLIST"
|
||||
|
||||
try:
|
||||
instance.password = decrypt_password(instance.password)
|
||||
rows, _ = await DBConnector._query_mysql(instance, database, sql)
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
entries = []
|
||||
for row in rows:
|
||||
time_val = row.get("Time", 0)
|
||||
info = row.get("Info", "")
|
||||
if time_val and int(time_val) > 1 and info and len(info) > 10:
|
||||
digest = hashlib.md5(info.encode()).hexdigest()[:16]
|
||||
entries.append(SlowLogEntry(
|
||||
sql_digest=digest,
|
||||
sql_text=info[:500],
|
||||
execution_count=1,
|
||||
avg_time_ms=float(time_val) * 1000,
|
||||
max_time_ms=float(time_val) * 1000,
|
||||
min_time_ms=float(time_val) * 1000,
|
||||
rows_examined=0,
|
||||
rows_sent=0,
|
||||
))
|
||||
|
||||
return entries[:limit]
|
||||
|
||||
async def collect_postgres_slow_logs(self, instance: Instance,
|
||||
database: str,
|
||||
limit: int = 50) -> List[SlowLogEntry]:
|
||||
"""从 PostgreSQL pg_stat_statements 收集慢日志。
|
||||
|
||||
需要安装 pg_stat_statements 扩展。
|
||||
"""
|
||||
sql = """
|
||||
SELECT
|
||||
query as sql_text,
|
||||
calls as execution_count,
|
||||
ROUND((total_exec_time / calls)::numeric, 3) as avg_time_ms,
|
||||
ROUND(max_exec_time::numeric, 3) as max_time_ms,
|
||||
ROUND(min_exec_time::numeric, 3) as min_time_ms,
|
||||
rows as rows_examined,
|
||||
rows as rows_sent
|
||||
FROM pg_stat_statements
|
||||
WHERE dbid = (SELECT oid FROM pg_database WHERE datname = $1)
|
||||
AND total_exec_time / calls > 100 -- > 100ms
|
||||
ORDER BY total_exec_time / calls DESC
|
||||
LIMIT $2
|
||||
"""
|
||||
|
||||
try:
|
||||
instance.password = decrypt_password(instance.password)
|
||||
# 需要先创建扩展
|
||||
await DBConnector._query_postgres(instance, database,
|
||||
"CREATE EXTENSION IF NOT EXISTS pg_stat_statements")
|
||||
rows, _ = await DBConnector._query_postgres(instance, database, sql)
|
||||
except Exception:
|
||||
# pg_stat_statements 可能不可用
|
||||
return []
|
||||
|
||||
entries = []
|
||||
for row in rows:
|
||||
sql_text = row.get("sql_text", "")
|
||||
digest = hashlib.md5(sql_text.encode()).hexdigest()[:16]
|
||||
entries.append(SlowLogEntry(
|
||||
sql_digest=digest,
|
||||
sql_text=sql_text[:500],
|
||||
execution_count=int(row.get("execution_count", 0) or 0),
|
||||
avg_time_ms=float(row.get("avg_time_ms", 0) or 0),
|
||||
max_time_ms=float(row.get("max_time_ms", 0) or 0),
|
||||
min_time_ms=float(row.get("min_time_ms", 0) or 0),
|
||||
rows_examined=int(row.get("rows_examined", 0) or 0),
|
||||
rows_sent=int(row.get("rows_sent", 0) or 0),
|
||||
))
|
||||
|
||||
return entries
|
||||
|
||||
async def save_slow_logs(self, instance_id: int,
|
||||
logs: List[SlowLogEntry]) -> None:
|
||||
"""保存慢日志到内存存储。"""
|
||||
if instance_id not in self._logs:
|
||||
self._logs[instance_id] = []
|
||||
|
||||
# 追加新日志,限制每个实例最多保存 1000 条
|
||||
self._logs[instance_id].extend(logs)
|
||||
if len(self._logs[instance_id]) > 1000:
|
||||
self._logs[instance_id] = self._logs[instance_id][-1000:]
|
||||
|
||||
def get_slow_logs(self, instance_id: int) -> List[SlowLogEntry]:
|
||||
"""获取保存的慢日志。"""
|
||||
return self._logs.get(instance_id, [])
|
||||
|
||||
def get_report(self, instance_id: int) -> SlowLogReport:
|
||||
"""生成慢日志报告。"""
|
||||
logs = self._logs.get(instance_id, [])
|
||||
|
||||
if not logs:
|
||||
return SlowLogReport(
|
||||
total_slow_queries=0,
|
||||
avg_execution_time_ms=0,
|
||||
max_execution_time_ms=0,
|
||||
top_queries=[],
|
||||
time_distribution={},
|
||||
)
|
||||
|
||||
total_avg = sum(q.avg_time_ms for q in logs) / len(logs)
|
||||
max_time = max(q.max_time_ms for q in logs)
|
||||
|
||||
# 按执行时间分布
|
||||
distribution = {
|
||||
"0-100ms": 0,
|
||||
"100-500ms": 0,
|
||||
"500ms-1s": 0,
|
||||
"1s-5s": 0,
|
||||
">5s": 0,
|
||||
}
|
||||
for q in logs:
|
||||
if q.avg_time_ms < 100:
|
||||
distribution["0-100ms"] += 1
|
||||
elif q.avg_time_ms < 500:
|
||||
distribution["100-500ms"] += 1
|
||||
elif q.avg_time_ms < 1000:
|
||||
distribution["500ms-1s"] += 1
|
||||
elif q.avg_time_ms < 5000:
|
||||
distribution["1s-5s"] += 1
|
||||
else:
|
||||
distribution[">5s"] += 1
|
||||
|
||||
# TOP 10
|
||||
top = sorted(logs, key=lambda x: x.avg_time_ms, reverse=True)[:10]
|
||||
|
||||
return SlowLogReport(
|
||||
total_slow_queries=len(logs),
|
||||
avg_execution_time_ms=round(total_avg, 2),
|
||||
max_execution_time_ms=round(max_time, 2),
|
||||
top_queries=top,
|
||||
time_distribution=distribution,
|
||||
)
|
||||
|
||||
def clear_logs(self, instance_id: int) -> None:
|
||||
"""清除指定实例的慢日志。"""
|
||||
self._logs.pop(instance_id, None)
|
||||
Reference in new issue
Block a user