16 changed files with 629 additions and 33 deletions
@ -0,0 +1,41 @@ |
|||||
|
using CoreAgent.Domain.Models.Network; |
||||
|
|
||||
|
namespace CoreAgent.Domain.Interfaces.Network; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 网络状态监控接口
|
||||
|
/// </summary>
|
||||
|
public interface INetworkStatusMonitor |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 检查所有网络端点的状态
|
||||
|
/// </summary>
|
||||
|
/// <param name="endPoints">网络端点信息</param>
|
||||
|
/// <param name="configType">网络配置类型</param>
|
||||
|
/// <returns>检查结果</returns>
|
||||
|
public Task<NetworkStatusCheckResult> CheckAllEndPointsStatusAsync( |
||||
|
NetworkIPEndPointCollection endPoints, |
||||
|
NetworkConfigType configType, |
||||
|
int? timeoutSeconds = null); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查 RAN 端点状态
|
||||
|
/// </summary>
|
||||
|
/// <param name="ranEndPoint">RAN 端点信息</param>
|
||||
|
/// <returns>RAN 端点状态</returns>
|
||||
|
Task<EndPointStatusResult> CheckRanStatusAsync(RanIPEndPoint ranEndPoint); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查 IMS 端点状态
|
||||
|
/// </summary>
|
||||
|
/// <param name="imsEndPoints">IMS 端点信息列表</param>
|
||||
|
/// <returns>IMS 端点状态列表</returns>
|
||||
|
Task<List<EndPointStatusResult>> CheckImsStatusAsync(List<ImsIPEndPoint> imsEndPoints); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查 CN 端点状态
|
||||
|
/// </summary>
|
||||
|
/// <param name="cnEndPoints">CN 端点信息列表</param>
|
||||
|
/// <returns>CN 端点状态列表</returns>
|
||||
|
Task<List<EndPointStatusResult>> CheckCnStatusAsync(List<CnIPEndPoint> cnEndPoints); |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
namespace CoreAgent.Domain.Models.Network; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 网络配置类型
|
||||
|
/// </summary>
|
||||
|
public enum NetworkConfigType |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 无配置
|
||||
|
/// </summary>
|
||||
|
None = 0, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 同时包含RAG和Core配置
|
||||
|
/// </summary>
|
||||
|
BothRagAndCore, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 仅包含RAG配置
|
||||
|
/// </summary>
|
||||
|
RagOnly, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 仅包含Core配置
|
||||
|
/// </summary>
|
||||
|
CoreOnly |
||||
|
} |
@ -0,0 +1,72 @@ |
|||||
|
using CoreAgent.Domain.Models.Network; |
||||
|
|
||||
|
namespace CoreAgent.Domain.Models.Network; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 端点状态检查结果
|
||||
|
/// </summary>
|
||||
|
public class EndPointStatusResult |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 通信地址
|
||||
|
/// </summary>
|
||||
|
public string ComAddr { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 是否成功
|
||||
|
/// </summary>
|
||||
|
public bool IsSuccess { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 错误信息
|
||||
|
/// </summary>
|
||||
|
public string ErrorMessage { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 网络协议类型
|
||||
|
/// </summary>
|
||||
|
public NetworkProtocolType NetworkProtocolType { get; set; } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 网络状态检查结果
|
||||
|
/// </summary>
|
||||
|
public class NetworkStatusCheckResult |
||||
|
{ |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 是否成功
|
||||
|
/// </summary>
|
||||
|
public bool IsSuccess { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 错误信息
|
||||
|
/// </summary>
|
||||
|
public string[] ErrorMessage { get; set; } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 网络协议类型
|
||||
|
/// </summary>
|
||||
|
public enum NetworkProtocolType |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 未指定
|
||||
|
/// </summary>
|
||||
|
None = 0, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// RAN 协议
|
||||
|
/// </summary>
|
||||
|
Ran = 1, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// IMS 协议
|
||||
|
/// </summary>
|
||||
|
Ims = 2, |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// CN 协议
|
||||
|
/// </summary>
|
||||
|
Cn = 3 |
||||
|
} |
@ -0,0 +1,337 @@ |
|||||
|
using CoreAgent.Domain.Helpers; |
||||
|
using CoreAgent.Domain.Interfaces.Network; |
||||
|
using CoreAgent.Domain.Interfaces.System.Command; |
||||
|
using CoreAgent.Domain.Models.Network; |
||||
|
using CoreAgent.Domain.Models.System; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using System.Text.Json; |
||||
|
|
||||
|
namespace CoreAgent.Infrastructure.Services.Network; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 网络状态监控管理类
|
||||
|
/// </summary>
|
||||
|
public class NetworkStatusMonitor : INetworkStatusMonitor |
||||
|
{ |
||||
|
private readonly ILogger<NetworkStatusMonitor> _logger; |
||||
|
private readonly ISystemCommandExecutor _commandExecutor; |
||||
|
private readonly AppSettings _appSettings; |
||||
|
|
||||
|
public NetworkStatusMonitor( |
||||
|
ILogger<NetworkStatusMonitor> logger, |
||||
|
ISystemCommandExecutor commandExecutor, |
||||
|
IOptions<AppSettings> appSettings) |
||||
|
{ |
||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
||||
|
_commandExecutor = commandExecutor ?? throw new ArgumentNullException(nameof(commandExecutor)); |
||||
|
_appSettings = appSettings?.Value ?? throw new ArgumentNullException(nameof(appSettings)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查所有网络端点的状态
|
||||
|
/// </summary>
|
||||
|
/// <param name="endPoints">网络端点信息</param>
|
||||
|
/// <param name="configType">网络配置类型</param>
|
||||
|
/// <param name="timeoutSeconds">超时时间(秒),默认30秒</param>
|
||||
|
/// <returns>检查结果</returns>
|
||||
|
public async Task<NetworkStatusCheckResult> CheckAllEndPointsStatusAsync( |
||||
|
NetworkIPEndPointCollection endPoints, |
||||
|
NetworkConfigType configType, |
||||
|
int? timeoutSeconds = null) |
||||
|
{ |
||||
|
var result = new NetworkStatusCheckResult(); |
||||
|
var startTime = DateTime.Now; |
||||
|
var timeout = TimeSpan.FromSeconds(timeoutSeconds ?? _appSettings.NetworkStatusCheckTimeout); |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
_logger.LogInformation("开始检查网络端点状态,配置类型: {ConfigType}, 超时时间: {TimeoutSeconds}秒", |
||||
|
configType, timeout.TotalSeconds); |
||||
|
|
||||
|
// 先执行一次检查
|
||||
|
var checkResults = await CheckEndPointsByConfigTypeAsync(endPoints, configType); |
||||
|
result.IsSuccess = checkResults.All(r => r.IsSuccess); |
||||
|
|
||||
|
// 如果检查失败,进入重试循环
|
||||
|
while (!result.IsSuccess && DateTime.Now - startTime < timeout) |
||||
|
{ |
||||
|
var failedEndpoints = checkResults.Where(r => !r.IsSuccess) |
||||
|
.Select(r => $"{r.ComAddr}: {r.ErrorMessage}"); |
||||
|
var errorMessage = string.Join("; ", failedEndpoints); |
||||
|
_logger.LogWarning("网络端点状态检查未通过,失败端点: {FailedEndpoints},将在1秒后重试,已耗时: {ElapsedTime}秒", |
||||
|
errorMessage, (DateTime.Now - startTime).TotalSeconds); |
||||
|
|
||||
|
await Task.Delay(1000); // 等待1秒后重试
|
||||
|
|
||||
|
// 重试检查
|
||||
|
checkResults = await CheckEndPointsByConfigTypeAsync(endPoints, configType); |
||||
|
result.IsSuccess = checkResults.All(r => r.IsSuccess); |
||||
|
} |
||||
|
|
||||
|
if (!result.IsSuccess) |
||||
|
{ |
||||
|
result.ErrorMessage = new string[] { $"检查超时,已超过{timeout.TotalSeconds}秒" }; |
||||
|
_logger.LogWarning("网络端点状态检查超时,配置类型: {ConfigType}", configType); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
_logger.LogInformation("网络端点状态检查成功,配置类型: {ConfigType}, 耗时: {ElapsedTime}秒", |
||||
|
configType, (DateTime.Now - startTime).TotalSeconds); |
||||
|
} |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
_logger.LogError(ex, "检查网络端点状态时发生错误,配置类型: {ConfigType}", configType); |
||||
|
result.IsSuccess = false; |
||||
|
result.ErrorMessage = new string[] { $"检查过程发生异常: {ex.Message}" }; |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 根据配置类型检查相应的端点状态
|
||||
|
/// </summary>
|
||||
|
/// <returns>所有端点的检查结果</returns>
|
||||
|
private async Task<EndPointStatusResult[]> CheckEndPointsByConfigTypeAsync(NetworkIPEndPointCollection endPoints, NetworkConfigType configType) |
||||
|
{ |
||||
|
var results = new List<EndPointStatusResult>(); |
||||
|
|
||||
|
switch (configType) |
||||
|
{ |
||||
|
case NetworkConfigType.BothRagAndCore: |
||||
|
var ragResult = await CheckRagEndPointsAsync(endPoints); |
||||
|
results.Add(ragResult); |
||||
|
var coreResult = await CheckCoreEndPointsAsync(endPoints); |
||||
|
results.Add(coreResult); |
||||
|
break; |
||||
|
|
||||
|
case NetworkConfigType.RagOnly: |
||||
|
var ragOnlyResult = await CheckRagEndPointsAsync(endPoints); |
||||
|
results.Add(ragOnlyResult); |
||||
|
break; |
||||
|
|
||||
|
case NetworkConfigType.CoreOnly: |
||||
|
var coreOnlyResult = await CheckCoreEndPointsAsync(endPoints); |
||||
|
results.Add(coreOnlyResult); |
||||
|
break; |
||||
|
|
||||
|
case NetworkConfigType.None: |
||||
|
default: |
||||
|
_logger.LogWarning("无需检查网络端点状态,配置类型: {ConfigType}", configType); |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
return results.ToArray(); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查 RAG 相关端点状态
|
||||
|
/// </summary>
|
||||
|
/// <returns>检查结果</returns>
|
||||
|
private async Task<EndPointStatusResult> CheckRagEndPointsAsync(NetworkIPEndPointCollection endPoints) |
||||
|
{ |
||||
|
var result = new EndPointStatusResult(); |
||||
|
|
||||
|
if (endPoints.RanEndPoint == null) |
||||
|
{ |
||||
|
result.IsSuccess = true; |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
result.ComAddr = endPoints.RanEndPoint.ComAddr; |
||||
|
var ranResult = await CheckRanStatusAsync(endPoints.RanEndPoint); |
||||
|
_logger.LogInformation("RAN 端点状态检查: {ComAddr}, 结果: {IsSuccess}", |
||||
|
ranResult.ComAddr, ranResult.IsSuccess); |
||||
|
|
||||
|
result.IsSuccess = ranResult.IsSuccess; |
||||
|
result.ErrorMessage = ranResult.ErrorMessage; |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查 Core 相关端点状态
|
||||
|
/// </summary>
|
||||
|
/// <returns>检查结果</returns>
|
||||
|
private async Task<EndPointStatusResult> CheckCoreEndPointsAsync(NetworkIPEndPointCollection endPoints) |
||||
|
{ |
||||
|
var result = new EndPointStatusResult { IsSuccess = true }; |
||||
|
var errorMessages = new List<string>(); |
||||
|
|
||||
|
// 检查 IMS 端点
|
||||
|
if (endPoints.ImsEndPoints != null && endPoints.ImsEndPoints.Any()) |
||||
|
{ |
||||
|
var imsResults = await CheckImsStatusAsync(endPoints.ImsEndPoints); |
||||
|
foreach (var imsResult in imsResults) |
||||
|
{ |
||||
|
_logger.LogInformation("IMS 端点状态检查: {ComAddr}, 结果: {IsSuccess}", |
||||
|
imsResult.ComAddr, imsResult.IsSuccess); |
||||
|
|
||||
|
if (!imsResult.IsSuccess) |
||||
|
{ |
||||
|
result.IsSuccess = false; |
||||
|
errorMessages.Add($"IMS端点 {imsResult.ComAddr}: {imsResult.ErrorMessage}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 检查 CN 端点
|
||||
|
if (endPoints.CnEndPoints != null && endPoints.CnEndPoints.Any()) |
||||
|
{ |
||||
|
var cnResults = await CheckCnStatusAsync(endPoints.CnEndPoints); |
||||
|
foreach (var cnResult in cnResults) |
||||
|
{ |
||||
|
_logger.LogInformation("CN 端点状态检查: {ComAddr}, 结果: {IsSuccess}", |
||||
|
cnResult.ComAddr, cnResult.IsSuccess); |
||||
|
|
||||
|
if (!cnResult.IsSuccess) |
||||
|
{ |
||||
|
result.IsSuccess = false; |
||||
|
errorMessages.Add($"CN端点 {cnResult.ComAddr}: {cnResult.ErrorMessage}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (!result.IsSuccess) |
||||
|
{ |
||||
|
result.ErrorMessage = string.Join("; ", errorMessages); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查 RAN 端点状态
|
||||
|
/// </summary>
|
||||
|
/// <param name="ranEndPoint">RAN 端点信息</param>
|
||||
|
/// <returns>RAN 端点状态</returns>
|
||||
|
public async Task<EndPointStatusResult> CheckRanStatusAsync(RanIPEndPoint ranEndPoint) |
||||
|
{ |
||||
|
var result = new EndPointStatusResult(); |
||||
|
|
||||
|
if (ranEndPoint == null) |
||||
|
{ |
||||
|
result.IsSuccess = false; |
||||
|
result.ErrorMessage = "RAN 端点未配置"; |
||||
|
_logger.LogWarning(result.ErrorMessage); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
result.ComAddr = ranEndPoint.ComAddr; |
||||
|
try |
||||
|
{ |
||||
|
await CheckEndPointStatusAsync(ranEndPoint.ComAddr, "RAN"); |
||||
|
result.IsSuccess = true; |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
result.IsSuccess = false; |
||||
|
result.ErrorMessage = ex.Message; |
||||
|
_logger.LogError(ex, "检查 RAN 端点状态失败"); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查 IMS 端点状态
|
||||
|
/// </summary>
|
||||
|
/// <param name="imsEndPoints">IMS 端点信息列表</param>
|
||||
|
/// <returns>IMS 端点状态列表</returns>
|
||||
|
public async Task<List<EndPointStatusResult>> CheckImsStatusAsync(List<ImsIPEndPoint> imsEndPoints) |
||||
|
{ |
||||
|
var result = new List<EndPointStatusResult>(); |
||||
|
|
||||
|
if (imsEndPoints == null || !imsEndPoints.Any()) |
||||
|
{ |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
foreach (var imsEndPoint in imsEndPoints) |
||||
|
{ |
||||
|
var statusResult = new EndPointStatusResult |
||||
|
{ |
||||
|
ComAddr = imsEndPoint.ComAddr |
||||
|
}; |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
await CheckEndPointStatusAsync(imsEndPoint.ComAddr, "IMS"); |
||||
|
statusResult.IsSuccess = true; |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
statusResult.IsSuccess = false; |
||||
|
statusResult.ErrorMessage = ex.Message; |
||||
|
_logger.LogError(ex, "检查 IMS 端点 {ComAddr} 状态失败", imsEndPoint.ComAddr); |
||||
|
} |
||||
|
|
||||
|
result.Add(statusResult); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查 CN 端点状态
|
||||
|
/// </summary>
|
||||
|
/// <param name="cnEndPoints">CN 端点信息列表</param>
|
||||
|
/// <returns>CN 端点状态列表</returns>
|
||||
|
public async Task<List<EndPointStatusResult>> CheckCnStatusAsync(List<CnIPEndPoint> cnEndPoints) |
||||
|
{ |
||||
|
var result = new List<EndPointStatusResult>(); |
||||
|
|
||||
|
if (cnEndPoints == null || !cnEndPoints.Any()) |
||||
|
{ |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
foreach (var cnEndPoint in cnEndPoints) |
||||
|
{ |
||||
|
var statusResult = new EndPointStatusResult |
||||
|
{ |
||||
|
ComAddr = cnEndPoint.ComAddr |
||||
|
}; |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
await CheckEndPointStatusAsync(cnEndPoint.ComAddr, "CN"); |
||||
|
statusResult.IsSuccess = true; |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
statusResult.IsSuccess = false; |
||||
|
statusResult.ErrorMessage = ex.Message; |
||||
|
_logger.LogError(ex, "检查 CN 端点 {ComAddr} 状态失败", cnEndPoint.ComAddr); |
||||
|
} |
||||
|
|
||||
|
result.Add(statusResult); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查单个端点的状态
|
||||
|
/// </summary>
|
||||
|
/// <param name="comAddr">通信地址</param>
|
||||
|
/// <param name="endPointType">端点类型</param>
|
||||
|
private async Task<(bool isSuccess, string data)> CheckEndPointStatusAsync(string comAddr, string endPointType) |
||||
|
{ |
||||
|
var command = $"{_appSettings.WebSocketJsPath} {comAddr} '{_appSettings.WebSocketCommands.Stats.ToJson()}'"; |
||||
|
var result = await _commandExecutor.ExecuteCommandAsync(command, new CancellationTokenSource()); |
||||
|
|
||||
|
if (result.IsSuccess) |
||||
|
{ |
||||
|
_logger.LogInformation("{EndPointType} 端点 {ComAddr} 状态检查成功", endPointType, comAddr); |
||||
|
return (result.IsSuccess, result.Output.ParseWsResultLogs()); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
var error = $"检查失败: {result.Error}"; |
||||
|
_logger.LogWarning("{EndPointType} 端点 {ComAddr} 状态检查失败: {Error}", endPointType, comAddr, result.Error); |
||||
|
return (result.IsSuccess, result.Output.ParseWsResultLogs()); |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue