diff --git a/Backend/MonitorAgent/MonitorAgent.API/Contracts/ConnectionsResponse.cs b/Backend/MonitorAgent/MonitorAgent.API/Contracts/ConnectionsResponse.cs new file mode 100644 index 0000000..1e87c5d --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.API/Contracts/ConnectionsResponse.cs @@ -0,0 +1,7 @@ +namespace MonitorAgent.API.Contracts; + +public class ConnectionsResponse +{ + public int Tcp {get;set;} + public int Udp {get;set;} +} \ No newline at end of file diff --git a/Backend/MonitorAgent/MonitorAgent.API/Controllers/MonitorController.cs b/Backend/MonitorAgent/MonitorAgent.API/Controllers/MonitorController.cs index 5987ab8..7039dcc 100644 --- a/Backend/MonitorAgent/MonitorAgent.API/Controllers/MonitorController.cs +++ b/Backend/MonitorAgent/MonitorAgent.API/Controllers/MonitorController.cs @@ -48,4 +48,17 @@ public class MonitorController( return Ok(response); } + + [HttpGet("connections")] + public async Task GetConnectionsCountAsync(CancellationToken cancellationToken = default) + { + var count = await monitorService.GetConnectionsCountAsync(cancellationToken); + var response = new ConnectionsResponse + { + Tcp = count.TcpCount, + Udp = count.UdpCount + }; + + return Ok(response); + } } \ No newline at end of file diff --git a/Backend/MonitorAgent/MonitorAgent.Application/Services/MonitorService.cs b/Backend/MonitorAgent/MonitorAgent.Application/Services/MonitorService.cs index 19c5950..8b70f8c 100644 --- a/Backend/MonitorAgent/MonitorAgent.Application/Services/MonitorService.cs +++ b/Backend/MonitorAgent/MonitorAgent.Application/Services/MonitorService.cs @@ -73,6 +73,17 @@ public class MonitorService : IMonitorService return loadavg; } + + // TODO: проверить на других системах + public async Task GetConnectionsCountAsync(CancellationToken cancellationToken = default) + { + var sockstat = await File.ReadAllLinesAsync("/proc/net/sockstat", cancellationToken); + + return new ConnectionsCount( + int.Parse(sockstat[0].Split()[2]), + int.Parse(sockstat[1].Split()[2]) + ); + } public static int ParseInt(string str) => int.Parse(Regex.Match(str, @"\d+").Value); } \ No newline at end of file diff --git a/Backend/MonitorAgent/MonitorAgent.Core/Abstraction/IMonitorService.cs b/Backend/MonitorAgent/MonitorAgent.Core/Abstraction/IMonitorService.cs index 9883a53..216e757 100644 --- a/Backend/MonitorAgent/MonitorAgent.Core/Abstraction/IMonitorService.cs +++ b/Backend/MonitorAgent/MonitorAgent.Core/Abstraction/IMonitorService.cs @@ -4,9 +4,11 @@ namespace MonitorAgent.Core.Abstraction; public interface IMonitorService { - public Task GetUptimeAsync(CancellationToken cancellationToken = default); + public Task GetUptimeAsync(CancellationToken cancellationToken); - public Task GetMemoryInfoAsync(CancellationToken cancellationToken = default); + public Task GetMemoryInfoAsync(CancellationToken cancellationToken); - public Task GetLoadAverageAsync(CancellationToken cancellationToken = default); + public Task GetLoadAverageAsync(CancellationToken cancellationToken); + + public Task GetConnectionsCountAsync(CancellationToken cancellationToken); } \ No newline at end of file diff --git a/Backend/MonitorAgent/MonitorAgent.Core/Models/ConnectionsCount.cs b/Backend/MonitorAgent/MonitorAgent.Core/Models/ConnectionsCount.cs new file mode 100644 index 0000000..63dc9be --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.Core/Models/ConnectionsCount.cs @@ -0,0 +1,7 @@ +namespace MonitorAgent.Core.Models; + +public class ConnectionsCount(int tcp, int udp) +{ + public readonly int TcpCount = tcp; + public readonly int UdpCount = udp; +} \ No newline at end of file