258 lines
8.3 KiB
Python
258 lines
8.3 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""
|
|||
|
|
Flask 提示词大师 - 监控管理脚本
|
|||
|
|
统一管理监控和日志功能
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import os
|
|||
|
|
import sys
|
|||
|
|
import time
|
|||
|
|
import json
|
|||
|
|
import subprocess
|
|||
|
|
import threading
|
|||
|
|
from datetime import datetime
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
# 添加项目路径到 Python 路径
|
|||
|
|
project_root = Path(__file__).parent
|
|||
|
|
sys.path.insert(0, str(project_root))
|
|||
|
|
|
|||
|
|
class MonitorManager:
|
|||
|
|
"""监控管理类"""
|
|||
|
|
|
|||
|
|
def __init__(self):
|
|||
|
|
self.project_root = project_root
|
|||
|
|
self.monitor_process = None
|
|||
|
|
self.log_manager_process = None
|
|||
|
|
self.is_running = False
|
|||
|
|
|
|||
|
|
def start_monitoring(self):
|
|||
|
|
"""启动监控"""
|
|||
|
|
try:
|
|||
|
|
print("🚀 启动服务监控...")
|
|||
|
|
|
|||
|
|
# 启动监控脚本
|
|||
|
|
self.monitor_process = subprocess.Popen(
|
|||
|
|
[sys.executable, "simple_monitor.py"],
|
|||
|
|
cwd=self.project_root,
|
|||
|
|
stdout=subprocess.PIPE,
|
|||
|
|
stderr=subprocess.PIPE,
|
|||
|
|
text=True
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
print(f"✅ 监控进程已启动 (PID: {self.monitor_process.pid})")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 启动监控失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def stop_monitoring(self):
|
|||
|
|
"""停止监控"""
|
|||
|
|
try:
|
|||
|
|
if self.monitor_process:
|
|||
|
|
print("🛑 停止服务监控...")
|
|||
|
|
self.monitor_process.terminate()
|
|||
|
|
self.monitor_process.wait(timeout=5)
|
|||
|
|
print("✅ 监控已停止")
|
|||
|
|
self.monitor_process = None
|
|||
|
|
else:
|
|||
|
|
print("ℹ️ 没有运行中的监控进程")
|
|||
|
|
|
|||
|
|
except subprocess.TimeoutExpired:
|
|||
|
|
print("⚠️ 监控进程未响应,强制终止")
|
|||
|
|
self.monitor_process.kill()
|
|||
|
|
self.monitor_process = None
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 停止监控失败: {e}")
|
|||
|
|
|
|||
|
|
def check_service_status(self):
|
|||
|
|
"""检查服务状态"""
|
|||
|
|
try:
|
|||
|
|
print("🔍 检查服务状态...")
|
|||
|
|
|
|||
|
|
result = subprocess.run(
|
|||
|
|
[sys.executable, "simple_monitor.py", "check"],
|
|||
|
|
cwd=self.project_root,
|
|||
|
|
capture_output=True,
|
|||
|
|
text=True,
|
|||
|
|
timeout=10
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
if result.returncode == 0:
|
|||
|
|
print("✅ 服务运行正常")
|
|||
|
|
if result.stdout:
|
|||
|
|
print(result.stdout)
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print("❌ 服务运行异常")
|
|||
|
|
if result.stderr:
|
|||
|
|
print(result.stderr)
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except subprocess.TimeoutExpired:
|
|||
|
|
print("⚠️ 服务检查超时")
|
|||
|
|
return False
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 服务检查失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def manage_logs(self, action="stats"):
|
|||
|
|
"""管理日志"""
|
|||
|
|
try:
|
|||
|
|
print(f"📁 执行日志管理操作: {action}")
|
|||
|
|
|
|||
|
|
result = subprocess.run(
|
|||
|
|
[sys.executable, "log_manager.py", action],
|
|||
|
|
cwd=self.project_root,
|
|||
|
|
capture_output=True,
|
|||
|
|
text=True,
|
|||
|
|
timeout=30
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
if result.returncode == 0:
|
|||
|
|
print("✅ 日志管理操作完成")
|
|||
|
|
if result.stdout:
|
|||
|
|
print(result.stdout)
|
|||
|
|
else:
|
|||
|
|
print("❌ 日志管理操作失败")
|
|||
|
|
if result.stderr:
|
|||
|
|
print(result.stderr)
|
|||
|
|
|
|||
|
|
except subprocess.TimeoutExpired:
|
|||
|
|
print("⚠️ 日志管理操作超时")
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 日志管理失败: {e}")
|
|||
|
|
|
|||
|
|
def generate_report(self):
|
|||
|
|
"""生成监控报告"""
|
|||
|
|
try:
|
|||
|
|
print("📊 生成监控报告...")
|
|||
|
|
|
|||
|
|
result = subprocess.run(
|
|||
|
|
[sys.executable, "simple_monitor.py", "report"],
|
|||
|
|
cwd=self.project_root,
|
|||
|
|
capture_output=True,
|
|||
|
|
text=True,
|
|||
|
|
timeout=10
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
if result.returncode == 0:
|
|||
|
|
print("✅ 监控报告已生成")
|
|||
|
|
if result.stdout:
|
|||
|
|
print(result.stdout)
|
|||
|
|
else:
|
|||
|
|
print("❌ 生成监控报告失败")
|
|||
|
|
if result.stderr:
|
|||
|
|
print(result.stderr)
|
|||
|
|
|
|||
|
|
except subprocess.TimeoutExpired:
|
|||
|
|
print("⚠️ 生成报告超时")
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 生成报告失败: {e}")
|
|||
|
|
|
|||
|
|
def show_dashboard(self):
|
|||
|
|
"""显示监控仪表板"""
|
|||
|
|
try:
|
|||
|
|
print("=" * 60)
|
|||
|
|
print("📊 Flask 提示词大师 - 监控仪表板")
|
|||
|
|
print("=" * 60)
|
|||
|
|
|
|||
|
|
# 检查服务状态
|
|||
|
|
service_ok = self.check_service_status()
|
|||
|
|
|
|||
|
|
# 显示日志统计
|
|||
|
|
self.manage_logs("stats")
|
|||
|
|
|
|||
|
|
# 显示监控报告
|
|||
|
|
self.generate_report()
|
|||
|
|
|
|||
|
|
print("=" * 60)
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 显示仪表板失败: {e}")
|
|||
|
|
|
|||
|
|
def run_interactive(self):
|
|||
|
|
"""运行交互式模式"""
|
|||
|
|
self.is_running = True
|
|||
|
|
|
|||
|
|
print("🎮 进入交互式监控模式")
|
|||
|
|
print("命令:")
|
|||
|
|
print(" start - 启动监控")
|
|||
|
|
print(" stop - 停止监控")
|
|||
|
|
print(" status - 检查服务状态")
|
|||
|
|
print(" logs - 管理日志")
|
|||
|
|
print(" report - 生成报告")
|
|||
|
|
print(" dashboard - 显示仪表板")
|
|||
|
|
print(" quit - 退出")
|
|||
|
|
|
|||
|
|
while self.is_running:
|
|||
|
|
try:
|
|||
|
|
command = input("\n📝 请输入命令: ").strip().lower()
|
|||
|
|
|
|||
|
|
if command == "quit":
|
|||
|
|
self.is_running = False
|
|||
|
|
print("👋 退出监控管理")
|
|||
|
|
elif command == "start":
|
|||
|
|
self.start_monitoring()
|
|||
|
|
elif command == "stop":
|
|||
|
|
self.stop_monitoring()
|
|||
|
|
elif command == "status":
|
|||
|
|
self.check_service_status()
|
|||
|
|
elif command == "logs":
|
|||
|
|
self.manage_logs()
|
|||
|
|
elif command == "report":
|
|||
|
|
self.generate_report()
|
|||
|
|
elif command == "dashboard":
|
|||
|
|
self.show_dashboard()
|
|||
|
|
else:
|
|||
|
|
print("❓ 未知命令,请重试")
|
|||
|
|
|
|||
|
|
except KeyboardInterrupt:
|
|||
|
|
print("\n👋 退出监控管理")
|
|||
|
|
self.is_running = False
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 执行命令失败: {e}")
|
|||
|
|
|
|||
|
|
# 确保停止监控
|
|||
|
|
self.stop_monitoring()
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""主函数"""
|
|||
|
|
manager = MonitorManager()
|
|||
|
|
|
|||
|
|
if len(sys.argv) == 1:
|
|||
|
|
# 交互式模式
|
|||
|
|
manager.run_interactive()
|
|||
|
|
elif sys.argv[1] == "start":
|
|||
|
|
# 启动监控
|
|||
|
|
manager.start_monitoring()
|
|||
|
|
elif sys.argv[1] == "stop":
|
|||
|
|
# 停止监控
|
|||
|
|
manager.stop_monitoring()
|
|||
|
|
elif sys.argv[1] == "status":
|
|||
|
|
# 检查状态
|
|||
|
|
manager.check_service_status()
|
|||
|
|
elif sys.argv[1] == "logs":
|
|||
|
|
# 管理日志
|
|||
|
|
action = sys.argv[2] if len(sys.argv) > 2 else "stats"
|
|||
|
|
manager.manage_logs(action)
|
|||
|
|
elif sys.argv[1] == "report":
|
|||
|
|
# 生成报告
|
|||
|
|
manager.generate_report()
|
|||
|
|
elif sys.argv[1] == "dashboard":
|
|||
|
|
# 显示仪表板
|
|||
|
|
manager.show_dashboard()
|
|||
|
|
else:
|
|||
|
|
print("用法:")
|
|||
|
|
print(" python monitor_manager.py # 交互式模式")
|
|||
|
|
print(" python monitor_manager.py start # 启动监控")
|
|||
|
|
print(" python monitor_manager.py stop # 停止监控")
|
|||
|
|
print(" python monitor_manager.py status # 检查状态")
|
|||
|
|
print(" python monitor_manager.py logs [action] # 管理日志")
|
|||
|
|
print(" python monitor_manager.py report # 生成报告")
|
|||
|
|
print(" python monitor_manager.py dashboard # 显示仪表板")
|
|||
|
|
|
|||
|
|
if __name__ == '__main__':
|
|||
|
|
main()
|