From b53c6904574edb56317be817a2b3b7d6c65328da Mon Sep 17 00:00:00 2001 From: ryzij Date: Sun, 28 Jun 2026 01:38:39 +0300 Subject: [PATCH] First commit --- .gitignore | 86 +++++++++++++++++++ .../Contracts/LoadAverageResponse.cs | 3 + .../Contracts/MemoryResponse.cs | 10 +++ .../Controllers/MonitorController.cs | 38 ++++++++ .../MonitorAgent.API/MonitorAgent.API.csproj | 19 ++++ .../MonitorAgent.API/MonitorAgent.API.http | 6 ++ .../MonitorAgent/MonitorAgent.API/Program.cs | 33 +++++++ .../Properties/launchSettings.json | 23 +++++ .../MonitorAgent.API/appsettings.json | 9 ++ .../MonitorAgent.Application.csproj | 13 +++ .../Services/MonitorService.cs | 78 +++++++++++++++++ .../Abstraction/IMonitorService.cs | 12 +++ .../MonitorAgent.Core/Models/MemoryInfo.cs | 10 +++ .../MonitorAgent.Core.csproj | 9 ++ Backend/MonitorAgent/MonitorAgent.slnx | 6 ++ README.md | 3 + 16 files changed, 358 insertions(+) create mode 100644 .gitignore create mode 100644 Backend/MonitorAgent/MonitorAgent.API/Contracts/LoadAverageResponse.cs create mode 100644 Backend/MonitorAgent/MonitorAgent.API/Contracts/MemoryResponse.cs create mode 100644 Backend/MonitorAgent/MonitorAgent.API/Controllers/MonitorController.cs create mode 100644 Backend/MonitorAgent/MonitorAgent.API/MonitorAgent.API.csproj create mode 100644 Backend/MonitorAgent/MonitorAgent.API/MonitorAgent.API.http create mode 100644 Backend/MonitorAgent/MonitorAgent.API/Program.cs create mode 100644 Backend/MonitorAgent/MonitorAgent.API/Properties/launchSettings.json create mode 100644 Backend/MonitorAgent/MonitorAgent.API/appsettings.json create mode 100644 Backend/MonitorAgent/MonitorAgent.Application/MonitorAgent.Application.csproj create mode 100644 Backend/MonitorAgent/MonitorAgent.Application/Services/MonitorService.cs create mode 100644 Backend/MonitorAgent/MonitorAgent.Core/Abstraction/IMonitorService.cs create mode 100644 Backend/MonitorAgent/MonitorAgent.Core/Models/MemoryInfo.cs create mode 100644 Backend/MonitorAgent/MonitorAgent.Core/MonitorAgent.Core.csproj create mode 100644 Backend/MonitorAgent/MonitorAgent.slnx create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0064acd --- /dev/null +++ b/.gitignore @@ -0,0 +1,86 @@ +# Build results +[Dd]ebug/ +[Rr]elease/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ +publish/ + +# Visual Studio +.vs/ +*.user +*.suo +*.userosscache +*.sln.docstates + +# Rider / JetBrains +.idea/ +*.sln.iml + +# VS Code +.vscode/ + +# ASP.NET Core +appsettings.Development.json +appsettings.Local.json + +# Environment variables +.env +#.env.* + +# Logs +logs/ +*.log + +# NuGet +*.nupkg +packages/ +!packages/build/ + +# Entity Framework +*.db +*.db-shm +*.db-wal + +# SQLite +*.sqlite +*.sqlite3 + +# OS files +.DS_Store +Thumbs.db + +# Node modules +node_modules/ + +# Coverage / tests +coverage/ +TestResults/ + +# Docker +docker-compose.yml +docker-compose.override.yml + +# Generated files +*.cache +*.tmp +*.temp + +# Publish output +wwwroot/dist/ +wwwroot/build/ + +# Secrets +secrets.json + +# Certificates +*.pfx +*.pem +*.key + +# Resharper +_ReSharper*/ +*.DotSettings.user diff --git a/Backend/MonitorAgent/MonitorAgent.API/Contracts/LoadAverageResponse.cs b/Backend/MonitorAgent/MonitorAgent.API/Contracts/LoadAverageResponse.cs new file mode 100644 index 0000000..5eee235 --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.API/Contracts/LoadAverageResponse.cs @@ -0,0 +1,3 @@ +namespace MonitorAgent.API.Contracts; + +public record class LoadAverageResponse(decimal[] LoadAverage); \ No newline at end of file diff --git a/Backend/MonitorAgent/MonitorAgent.API/Contracts/MemoryResponse.cs b/Backend/MonitorAgent/MonitorAgent.API/Contracts/MemoryResponse.cs new file mode 100644 index 0000000..41ffacb --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.API/Contracts/MemoryResponse.cs @@ -0,0 +1,10 @@ +namespace MonitorAgent.API.Contracts; + +public record class MemoryResponse +( + int MemTotal, + int MemFree, + int MemAvailable, + int SwapTotal, + int SwapFree +); \ No newline at end of file diff --git a/Backend/MonitorAgent/MonitorAgent.API/Controllers/MonitorController.cs b/Backend/MonitorAgent/MonitorAgent.API/Controllers/MonitorController.cs new file mode 100644 index 0000000..744c224 --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.API/Controllers/MonitorController.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.Mvc; +using MonitorAgent.API.Contracts; +using MonitorAgent.Core.Abstraction; +namespace MonitorAgent.API.Controllers; + +[ApiController] +[Route("[controller]")] +public class MonitorController(IMonitorService monitorService) : ControllerBase +{ + [HttpGet("uptime")] + public async Task> GetUptimeAsync(CancellationToken cancellationToken = default) + { + var uptime = await monitorService.GetUptimeAsync(cancellationToken); + return Ok(uptime); + } + + [HttpGet("memory")] + public async Task> GetMemoryAsync(CancellationToken cancellationToken = default) + { + var memInfo = await monitorService.GetMemoryInfoAsync(cancellationToken); + var response = new MemoryResponse( + memInfo.MemTotal, + memInfo.MemFree, + memInfo.MemAvailable, + memInfo.SwapTotal, + memInfo.SwapFree + ); + + return Ok(response); + } + + [HttpGet("loadavg")] + public async Task> GetActionResultAsync(CancellationToken cancellationToken = default) + { + var response = new LoadAverageResponse(await monitorService.GetLoadAverageAsync(cancellationToken)); + return Ok(response); + } +} \ No newline at end of file diff --git a/Backend/MonitorAgent/MonitorAgent.API/MonitorAgent.API.csproj b/Backend/MonitorAgent/MonitorAgent.API/MonitorAgent.API.csproj new file mode 100644 index 0000000..9538de7 --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.API/MonitorAgent.API.csproj @@ -0,0 +1,19 @@ + + + + net10.0 + enable + enable + + + + + + + + + + + + + diff --git a/Backend/MonitorAgent/MonitorAgent.API/MonitorAgent.API.http b/Backend/MonitorAgent/MonitorAgent.API/MonitorAgent.API.http new file mode 100644 index 0000000..eebef9a --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.API/MonitorAgent.API.http @@ -0,0 +1,6 @@ +@MonitorAgent.API_HostAddress = http://localhost:5170 + +GET {{MonitorAgent.API_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/Backend/MonitorAgent/MonitorAgent.API/Program.cs b/Backend/MonitorAgent/MonitorAgent.API/Program.cs new file mode 100644 index 0000000..65ae649 --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.API/Program.cs @@ -0,0 +1,33 @@ +using MonitorAgent.Core.Abstraction; +using MonitorAgent.Application.Services; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi +builder.Services.AddOpenApi(); + +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +builder.Services.AddScoped(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.UseSwagger().UseSwaggerUI(); + +app.Run(); diff --git a/Backend/MonitorAgent/MonitorAgent.API/Properties/launchSettings.json b/Backend/MonitorAgent/MonitorAgent.API/Properties/launchSettings.json new file mode 100644 index 0000000..ff9d55b --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.API/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5170", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7032;http://localhost:5170", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Backend/MonitorAgent/MonitorAgent.API/appsettings.json b/Backend/MonitorAgent/MonitorAgent.API/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.API/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Backend/MonitorAgent/MonitorAgent.Application/MonitorAgent.Application.csproj b/Backend/MonitorAgent/MonitorAgent.Application/MonitorAgent.Application.csproj new file mode 100644 index 0000000..3930b21 --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.Application/MonitorAgent.Application.csproj @@ -0,0 +1,13 @@ + + + + + + + + net10.0 + enable + enable + + + diff --git a/Backend/MonitorAgent/MonitorAgent.Application/Services/MonitorService.cs b/Backend/MonitorAgent/MonitorAgent.Application/Services/MonitorService.cs new file mode 100644 index 0000000..6d5d516 --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.Application/Services/MonitorService.cs @@ -0,0 +1,78 @@ +using MonitorAgent.Core.Abstraction; +using MonitorAgent.Core.Models; +using System.Diagnostics; +using System.Globalization; +using System.Text.RegularExpressions; +namespace MonitorAgent.Application.Services; + +public class MonitorService : IMonitorService +{ + // private static async Task 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); + // } + + public async Task GetUptimeAsync(CancellationToken cancellationToken = default) + { + var uptime = await File.ReadAllTextAsync("/proc/uptime", cancellationToken); + return decimal.Parse(uptime.Split()[1], CultureInfo.InvariantCulture); + } + + public async Task GetMemoryInfoAsync(CancellationToken cancellationToken = default) + { + var memInfo = new MemoryInfo(); + + var raw = (await File.ReadAllTextAsync("/proc/meminfo", cancellationToken)).Split(':', '\n'); + for (int i = 0; i < raw.Length - 1; i++) + { + switch (raw[i].ToLower()) + { + case "memtotal": + memInfo.MemTotal = ParseInt(raw[i + 1]); + break; + case "memfree": + memInfo.MemFree = ParseInt(raw[i + 1]); + break; + case "memavailable": + memInfo.MemAvailable = ParseInt(raw[i + 1]); + break; + case "swaptotal": + memInfo.SwapTotal = ParseInt(raw[i + 1]); + break; + case "swapfree": + memInfo.SwapFree = ParseInt(raw[i + 1]); + break; + } + } + + return memInfo; + } + + public async Task GetLoadAverageAsync(CancellationToken cancellationToken = default) + { + var raw = (await File.ReadAllTextAsync("/proc/loadavg", cancellationToken)).Split(); + var loadavg = new decimal[3]; + + for (int i = 0; i < loadavg.Length; i++) + loadavg[i] = decimal.Parse(raw[i], CultureInfo.InvariantCulture); + + return loadavg; + } + + 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 new file mode 100644 index 0000000..9883a53 --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.Core/Abstraction/IMonitorService.cs @@ -0,0 +1,12 @@ +using MonitorAgent.Core.Models; + +namespace MonitorAgent.Core.Abstraction; + +public interface IMonitorService +{ + public Task GetUptimeAsync(CancellationToken cancellationToken = default); + + public Task GetMemoryInfoAsync(CancellationToken cancellationToken = default); + + public Task GetLoadAverageAsync(CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/Backend/MonitorAgent/MonitorAgent.Core/Models/MemoryInfo.cs b/Backend/MonitorAgent/MonitorAgent.Core/Models/MemoryInfo.cs new file mode 100644 index 0000000..d29bb88 --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.Core/Models/MemoryInfo.cs @@ -0,0 +1,10 @@ +namespace MonitorAgent.Core.Models; + +public class MemoryInfo +{ + public int MemTotal { get; set; } + public int MemFree { get; set; } + public int MemAvailable { get; set; } + public int SwapTotal { get; set; } + public int SwapFree { get; set; } +} diff --git a/Backend/MonitorAgent/MonitorAgent.Core/MonitorAgent.Core.csproj b/Backend/MonitorAgent/MonitorAgent.Core/MonitorAgent.Core.csproj new file mode 100644 index 0000000..b760144 --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.Core/MonitorAgent.Core.csproj @@ -0,0 +1,9 @@ + + + + net10.0 + enable + enable + + + diff --git a/Backend/MonitorAgent/MonitorAgent.slnx b/Backend/MonitorAgent/MonitorAgent.slnx new file mode 100644 index 0000000..1516269 --- /dev/null +++ b/Backend/MonitorAgent/MonitorAgent.slnx @@ -0,0 +1,6 @@ + + + + + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..a260515 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Server Monitor + +Сервис для сбора статистики работы сервера