mirror of
https://github.com/ryzij/ServerMonitor.git
synced 2026-07-14 03:46:58 +00:00
Compare commits
4 Commits
20ea6645f9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 88ecb2f4a5 | |||
| d5915ccf72 | |||
| e0b13d510d | |||
| 8efeb0e4dc |
@@ -0,0 +1,7 @@
|
|||||||
|
namespace MonitorAgent.API.Contracts;
|
||||||
|
|
||||||
|
public class ConnectionsCountResponse
|
||||||
|
{
|
||||||
|
public int Tcp {get;set;}
|
||||||
|
public int Udp {get;set;}
|
||||||
|
}
|
||||||
@@ -48,4 +48,17 @@ public class MonitorController(
|
|||||||
|
|
||||||
return Ok(response);
|
return Ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("connectionsCount")]
|
||||||
|
public async Task<ActionResult> GetConnectionsCountAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var count = await monitorService.GetConnectionsCountAsync(cancellationToken);
|
||||||
|
var response = new ConnectionsCountResponse
|
||||||
|
{
|
||||||
|
Tcp = count.TcpCount,
|
||||||
|
Udp = count.UdpCount
|
||||||
|
};
|
||||||
|
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,9 @@ builder.Services.AddSwaggerGen();
|
|||||||
|
|
||||||
builder.Services.Configure<CpuMonitorSettings>(builder.Configuration.GetSection("CpuMonitorSettings"));
|
builder.Services.Configure<CpuMonitorSettings>(builder.Configuration.GetSection("CpuMonitorSettings"));
|
||||||
|
|
||||||
|
builder.Services.AddScoped<IProcessStarterService, ProcessStarterService>();
|
||||||
builder.Services.AddScoped<IMonitorService, MonitorService>();
|
builder.Services.AddScoped<IMonitorService, MonitorService>();
|
||||||
|
|
||||||
builder.Services.AddSingleton<RawCpuMonitorService>();
|
builder.Services.AddSingleton<RawCpuMonitorService>();
|
||||||
builder.Services.AddSingleton<CpuMonitorBackgroundService>();
|
builder.Services.AddSingleton<CpuMonitorBackgroundService>();
|
||||||
builder.Services.AddHostedService(provider => provider.GetRequiredService<CpuMonitorBackgroundService>());
|
builder.Services.AddHostedService(provider => provider.GetRequiredService<CpuMonitorBackgroundService>());
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class MonitorService : IMonitorService
|
|||||||
public async Task<decimal> GetUptimeAsync(CancellationToken cancellationToken = default)
|
public async Task<decimal> GetUptimeAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var uptime = await File.ReadAllTextAsync("/proc/uptime", cancellationToken);
|
var uptime = await File.ReadAllTextAsync("/proc/uptime", cancellationToken);
|
||||||
return decimal.Parse(uptime.Split()[1], CultureInfo.InvariantCulture);
|
return decimal.Parse(uptime.Split()[0], CultureInfo.InvariantCulture);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<MemoryInfo> GetMemoryInfoAsync(CancellationToken cancellationToken = default)
|
public async Task<MemoryInfo> GetMemoryInfoAsync(CancellationToken cancellationToken = default)
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using MonitorAgent.Core.Abstraction;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace MonitorAgent.Application.Services;
|
||||||
|
|
||||||
|
public class ProcessStarterService : IProcessStarterService
|
||||||
|
{
|
||||||
|
public async Task<string> StartProcessAsync(
|
||||||
|
string fileName, string arguments, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
using var process = new Process
|
||||||
|
{
|
||||||
|
StartInfo = new ProcessStartInfo
|
||||||
|
{
|
||||||
|
FileName = fileName,
|
||||||
|
Arguments = arguments,
|
||||||
|
UseShellExecute = false,
|
||||||
|
RedirectStandardOutput = true,
|
||||||
|
RedirectStandardError = true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
process.Start();
|
||||||
|
|
||||||
|
return await process.StandardOutput.ReadToEndAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,6 @@
|
|||||||
|
namespace MonitorAgent.Core.Abstraction;
|
||||||
|
|
||||||
|
public interface IProcessStarterService
|
||||||
|
{
|
||||||
|
Task<string> StartProcessAsync(string fileName, string arguments, 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user