修复错误

This commit is contained in:
MasonLiu 2026-05-26 00:09:05 +08:00
parent 5a8be69267
commit 4d1a9111b6

View File

@ -322,13 +322,14 @@ class IDCMonitor:
"""通过Ping检测主机""" """通过Ping检测主机"""
self.logger.info("开始Ping检测所有VPS...") self.logger.info("开始Ping检测所有VPS...")
unreachable_hosts = []
# 获取VPS列表
vps_data = self.get_vps_list() vps_data = self.get_vps_list()
if not vps_data or 'host' not in vps_data: if not vps_data or 'host' not in vps_data:
self.logger.error("无法获取VPS列表") self.logger.error("无法获取VPS列表")
return [] return []
unreachable_hosts = []
for host in vps_data['host']: for host in vps_data['host']:
host_id = host['id'] host_id = host['id']
ip = host.get('dedicatedip', '') ip = host.get('dedicatedip', '')
@ -371,28 +372,57 @@ class IDCMonitor:
self.logger.info(f"开始HTTP检测 {len(domains)} 个域名...") self.logger.info(f"开始HTTP检测 {len(domains)} 个域名...")
# 获取VPS列表建立域名到ID的映射 unreachable_domains = []
vps_data = self.get_vps_list()
if not vps_data or 'host' not in vps_data:
self.logger.error("无法获取VPS列表")
return []
# 建立域名到VPS ID的映射
domain_to_vps = {}
for host in vps_data['host']:
domain = host.get('domain', '')
if domain:
domain_to_vps[domain] = host
unreachable_hosts = []
for domain in domains: for domain in domains:
self.logger.debug(f"正在HTTP检测: {domain}") self.logger.debug(f"正在HTTP检测: {domain}")
if not self.check_http_host(domain): if not self.check_http_host(domain):
self.logger.warning(f"域名 {domain} HTTP检测失败") self.logger.warning(f"域名 {domain} HTTP检测失败")
unreachable_domains.append(domain)
# 查找对应的VPS if unreachable_domains:
self.logger.warning(f"发现 {len(unreachable_domains)} 个域名访问异常: {', '.join(unreachable_domains)}")
else:
self.logger.info("所有域名HTTP检测正常")
return unreachable_domains
def check_and_power_on(self, unreachable_hosts_or_domains):
"""检查并开机无法访问的VPS
Args:
unreachable_hosts_or_domains:
- ping模式: [{'id': xxx, 'ip': xxx, ...}, ...]
- http模式: ['domain1.com', 'domain2.com', ...]
"""
if not unreachable_hosts_or_domains:
self.logger.info("未发现需要处理的异常情况")
return
way = self.config.get('WAY', 'ping')
# 如果是HTTP模式需要先找到域名对应的VPS
if way == 'http':
unreachable_domains = unreachable_hosts_or_domains
self.logger.info(f"开始查找 {len(unreachable_domains)} 个异常域名对应的VPS...")
# 获取VPS列表建立域名到ID的映射
vps_data = self.get_vps_list()
if not vps_data or 'host' not in vps_data:
self.logger.error("无法获取VPS列表")
return
# 建立域名到VPS的映射
domain_to_vps = {}
for host in vps_data['host']:
domain = host.get('domain', '')
if domain:
domain_to_vps[domain] = host
# 转换域名为VPS信息
unreachable_hosts = []
for domain in unreachable_domains:
if domain in domain_to_vps: if domain in domain_to_vps:
host = domain_to_vps[domain] host = domain_to_vps[domain]
ip = host.get('dedicatedip', '') ip = host.get('dedicatedip', '')
@ -409,19 +439,13 @@ class IDCMonitor:
'product_name': host.get('product_name', '') 'product_name': host.get('product_name', '')
}) })
else: else:
self.logger.warning(f"域名 {domain} 未找到对应的VPS") self.logger.warning(f"域名 {domain} 未找到对应的VPS跳过")
if unreachable_hosts:
self.logger.warning(f"发现 {len(unreachable_hosts)} 个域名访问异常")
else: else:
self.logger.info("所有域名HTTP检测正常") # ping模式直接使用传入的数据
unreachable_hosts = unreachable_hosts_or_domains
return unreachable_hosts
def check_and_power_on(self, unreachable_hosts):
"""检查并开机无法访问的VPS"""
if not unreachable_hosts: if not unreachable_hosts:
self.logger.info("未发现需要开机的VPS") self.logger.info("所有异常的VPS都属于例外IP或找不到对应VPS无需操作")
return return
self.logger.info(f"开始检查 {len(unreachable_hosts)} 台VPS的实际状态...") self.logger.info(f"开始检查 {len(unreachable_hosts)} 台VPS的实际状态...")
@ -452,7 +476,7 @@ class IDCMonitor:
# 如果所有VPS都是开机状态记录日志 # 如果所有VPS都是开机状态记录日志
if all_are_on: if all_are_on:
self.logger.info( self.logger.info(
"检测到所有VPS均为开机状态可能是禁Ping或网站临时异常,无需操作" "检测到所有VPS均为开机状态可能是禁Ping、CDN缓存或网站临时异常,无需操作"
) )
return return
@ -519,11 +543,15 @@ class IDCMonitor:
self.logger.info(f"开始第 {int(time.time())} 时间戳的监控循环") self.logger.info(f"开始第 {int(time.time())} 时间戳的监控循环")
self.logger.info("=" * 60) self.logger.info("=" * 60)
# 检测主机 # 检测主机不调用API
unreachable_hosts = self.detect_hosts() unreachable = self.detect_hosts()
# 检查并开机 # 只有在检测到异常时才进入API流程
self.check_and_power_on(unreachable_hosts) if unreachable:
self.logger.info("检测到异常开始调用API进行进一步检查和开机...")
self.check_and_power_on(unreachable)
else:
self.logger.info("所有检测正常无需调用API")
self.logger.info("本次监控循环完成\n") self.logger.info("本次监控循环完成\n")