config_health_check.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. """
  2. 配置健康检查工具
  3. 用于验证配置文件的完整性和正确性
  4. """
  5. import sys
  6. from typing import List, Dict, Any
  7. from core.utils.config_manager import get_config_manager
  8. from core.utils.spider_config import SpiderConfig
  9. from config import settings
  10. class ConfigHealthCheck:
  11. """
  12. 配置健康检查工具
  13. """
  14. def __init__(self):
  15. self.config_manager = get_config_manager()
  16. self.errors = []
  17. self.warnings = []
  18. def check_env_config(self) -> bool:
  19. """
  20. 检查环境配置
  21. """
  22. try:
  23. # 检查必要配置是否存在
  24. required_settings = [
  25. 'DB_HOST', 'DB_USER', 'DB_PASSWORD', 'DB_NAME',
  26. 'ROCKETMQ_ENDPOINT', 'ROCKETMQ_ACCESS_KEY_ID', 'ROCKETMQ_ACCESS_KEY_SECRET',
  27. 'FEISHU_APPID', 'FEISHU_APPSECRET',
  28. 'ALIYUN_ACCESS_KEY_ID', 'ALIYUN_ACCESS_KEY_SECRET',
  29. 'REDIS_HOST', 'REDIS_PASSWORD'
  30. ]
  31. for setting in required_settings:
  32. if not getattr(settings, setting, None):
  33. self.errors.append(f"环境配置缺失: {setting}")
  34. # 检查URL格式
  35. url_settings = ['ROCKETMQ_ENDPOINT']
  36. for setting in url_settings:
  37. url = getattr(settings, setting, None)
  38. if url and not isinstance(url, str):
  39. self.errors.append(f"URL配置格式错误: {setting}")
  40. return len(self.errors) == 0
  41. except Exception as e:
  42. self.errors.append(f"环境配置检查异常: {str(e)}")
  43. return False
  44. def check_spider_configs(self) -> bool:
  45. """
  46. 检查所有爬虫配置
  47. """
  48. try:
  49. platforms = self.config_manager.list_platforms()
  50. if not platforms:
  51. self.warnings.append("未找到任何平台配置")
  52. return True
  53. valid_count = 0
  54. for platform in platforms:
  55. try:
  56. config = self.config_manager.get_platform_config(platform)
  57. # 验证配置字段
  58. if not config.platform:
  59. self.errors.append(f"平台 {platform} 缺少 platform 字段")
  60. if not config.mode:
  61. self.errors.append(f"平台 {platform} 缺少 mode 字段")
  62. if not config.url:
  63. self.errors.append(f"平台 {platform} 缺少 url 字段")
  64. valid_count += 1
  65. except Exception as e:
  66. self.errors.append(f"平台 {platform} 配置验证失败: {str(e)}")
  67. return len(self.errors) == 0
  68. except Exception as e:
  69. self.errors.append(f"爬虫配置检查异常: {str(e)}")
  70. return False
  71. def check_file_permissions(self) -> bool:
  72. """
  73. 检查配置文件权限
  74. """
  75. import os
  76. from core.utils.path_utils import spiders_config_path
  77. try:
  78. # 检查爬虫配置文件是否存在
  79. if not os.path.exists(spiders_config_path):
  80. self.errors.append(f"爬虫配置文件不存在: {spiders_config_path}")
  81. return False
  82. # 检查文件是否可读
  83. if not os.access(spiders_config_path, os.R_OK):
  84. self.errors.append(f"爬虫配置文件不可读: {spiders_config_path}")
  85. return len(self.errors) == 0
  86. except Exception as e:
  87. self.errors.append(f"文件权限检查异常: {str(e)}")
  88. return False
  89. def run_all_checks(self) -> Dict[str, Any]:
  90. """
  91. 运行所有检查
  92. """
  93. self.errors.clear()
  94. self.warnings.clear()
  95. env_ok = self.check_env_config()
  96. spider_ok = self.check_spider_configs()
  97. file_ok = self.check_file_permissions()
  98. overall_ok = env_ok and spider_ok and file_ok
  99. return {
  100. "success": overall_ok,
  101. "errors": self.errors.copy(),
  102. "warnings": self.warnings.copy(),
  103. "details": {
  104. "env_config": env_ok,
  105. "spider_configs": spider_ok,
  106. "file_permissions": file_ok
  107. }
  108. }
  109. def print_report(self):
  110. """
  111. 打印健康检查报告
  112. """
  113. result = self.run_all_checks()
  114. print("=" * 50)
  115. print("配置健康检查报告")
  116. print("=" * 50)
  117. if result["success"]:
  118. print("✓ 所有配置检查通过")
  119. else:
  120. print("✗ 配置存在问题")
  121. print(f"\n详细信息:")
  122. print(f" 环境配置: {'✓' if result['details']['env_config'] else '✗'}")
  123. print(f" 爬虫配置: {'✓' if result['details']['spider_configs'] else '✗'}")
  124. print(f" 文件权限: {'✓' if result['details']['file_permissions'] else '✗'}")
  125. if result["warnings"]:
  126. print(f"\n警告:")
  127. for warning in result["warnings"]:
  128. print(f" - {warning}")
  129. if result["errors"]:
  130. print(f"\n错误:")
  131. for error in result["errors"]:
  132. print(f" - {error}")
  133. print("\n统计信息:")
  134. try:
  135. stats = self.config_manager.get_config_stats()
  136. print(f" 平台数量: {stats['total_platforms']}")
  137. print(f" 运行环境: {stats['env']}")
  138. except Exception as e:
  139. print(f" 统计信息获取失败: {e}")
  140. print("=" * 50)
  141. return result
  142. def run_health_check():
  143. """
  144. 运行配置健康检查
  145. """
  146. checker = ConfigHealthCheck()
  147. return checker.print_report()
  148. if __name__ == "__main__":
  149. result = run_health_check()
  150. sys.exit(0 if result["success"] else 1)