Добавил получение количества tcp и udp соединений

This commit is contained in:
2026-07-02 12:21:22 +03:00
parent 8efeb0e4dc
commit e0b13d510d
5 changed files with 43 additions and 3 deletions
@@ -0,0 +1,7 @@
namespace MonitorAgent.API.Contracts;
public class ConnectionsResponse
{
public int Tcp {get;set;}
public int Udp {get;set;}
}
@@ -48,4 +48,17 @@ public class MonitorController(
return Ok(response); return Ok(response);
} }
[HttpGet("connections")]
public async Task<ActionResult> GetConnectionsCountAsync(CancellationToken cancellationToken = default)
{
var count = await monitorService.GetConnectionsCountAsync(cancellationToken);
var response = new ConnectionsResponse
{
Tcp = count.TcpCount,
Udp = count.UdpCount
};
return Ok(response);
}
} }
@@ -73,6 +73,17 @@ public class MonitorService : IMonitorService
return loadavg; return loadavg;
} }
// TODO: проверить на других системах
public async Task<ConnectionsCount> 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); public static int ParseInt(string str) => int.Parse(Regex.Match(str, @"\d+").Value);
} }
@@ -4,9 +4,11 @@ namespace MonitorAgent.Core.Abstraction;
public interface IMonitorService public interface IMonitorService
{ {
public Task<decimal> GetUptimeAsync(CancellationToken cancellationToken = default); public Task<decimal> GetUptimeAsync(CancellationToken cancellationToken);
public Task<MemoryInfo> GetMemoryInfoAsync(CancellationToken cancellationToken = default); public Task<MemoryInfo> GetMemoryInfoAsync(CancellationToken cancellationToken);
public Task<decimal[]> GetLoadAverageAsync(CancellationToken cancellationToken = default); public Task<decimal[]> GetLoadAverageAsync(CancellationToken cancellationToken);
public Task<ConnectionsCount> GetConnectionsCountAsync(CancellationToken cancellationToken);
} }
@@ -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;
}