mirror of
https://github.com/ryzij/ServerMonitor.git
synced 2026-07-14 03:46:58 +00:00
Compare commits
4 Commits
main
..
20ea6645f9
| Author | SHA1 | Date | |
|---|---|---|---|
| 20ea6645f9 | |||
| 9db0542d6b | |||
| 08aa1bbf90 | |||
| 7c4d557dc4 |
@@ -1,7 +0,0 @@
|
|||||||
namespace MonitorAgent.API.Contracts;
|
|
||||||
|
|
||||||
public class ConnectionsCountResponse
|
|
||||||
{
|
|
||||||
public int Tcp {get;set;}
|
|
||||||
public int Udp {get;set;}
|
|
||||||
}
|
|
||||||
@@ -48,17 +48,4 @@ 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,9 +15,7 @@ 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()[0], CultureInfo.InvariantCulture);
|
return decimal.Parse(uptime.Split()[1], CultureInfo.InvariantCulture);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<MemoryInfo> GetMemoryInfoAsync(CancellationToken cancellationToken = default)
|
public async Task<MemoryInfo> GetMemoryInfoAsync(CancellationToken cancellationToken = default)
|
||||||
@@ -73,17 +73,6 @@ 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);
|
||||||
}
|
}
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
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,11 +4,9 @@ namespace MonitorAgent.Core.Abstraction;
|
|||||||
|
|
||||||
public interface IMonitorService
|
public interface IMonitorService
|
||||||
{
|
{
|
||||||
public Task<decimal> GetUptimeAsync(CancellationToken cancellationToken);
|
public Task<decimal> GetUptimeAsync(CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
public Task<MemoryInfo> GetMemoryInfoAsync(CancellationToken cancellationToken);
|
public Task<MemoryInfo> GetMemoryInfoAsync(CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
public Task<decimal[]> GetLoadAverageAsync(CancellationToken cancellationToken);
|
public Task<decimal[]> GetLoadAverageAsync(CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
public Task<ConnectionsCount> GetConnectionsCountAsync(CancellationToken cancellationToken);
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
namespace MonitorAgent.Core.Abstraction;
|
|
||||||
|
|
||||||
public interface IProcessStarterService
|
|
||||||
{
|
|
||||||
Task<string> StartProcessAsync(string fileName, string arguments, CancellationToken cancellationToken);
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace MonitorAgent.Core.Models;
|
|
||||||
|
|
||||||
public class ConnectionsCount(int tcp, int udp)
|
|
||||||
{
|
|
||||||
public readonly int TcpCount = tcp;
|
|
||||||
public readonly int UdpCount = udp;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using MonitorPanel.Core.Models;
|
||||||
|
|
||||||
|
namespace MonitorPanel.API.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("[controller]")]
|
||||||
|
public class WeatherForecastController : ControllerBase
|
||||||
|
{
|
||||||
|
private static readonly string[] Summaries =
|
||||||
|
[
|
||||||
|
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||||
|
];
|
||||||
|
|
||||||
|
[HttpGet(Name = "GetWeatherForecast")]
|
||||||
|
public IEnumerable<WeatherForecast> Get()
|
||||||
|
{
|
||||||
|
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||||
|
{
|
||||||
|
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||||
|
TemperatureC = Random.Shared.Next(-20, 55),
|
||||||
|
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||||
|
})
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.9" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.9">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.2.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\MonitorPanel.Core\MonitorPanel.Core.csproj" />
|
||||||
|
<ProjectReference Include="..\MonitorPanel.DataAccess\MonitorPanel.DataAccess.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
@MonitorPanel.API_HostAddress = http://localhost:5068
|
||||||
|
|
||||||
|
GET {{MonitorPanel.API_HostAddress}}/weatherforecast/
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
###
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using MonitorPanel.Core.Abstractions;
|
||||||
|
using MonitorPanel.DataAccess;
|
||||||
|
using MonitorPanel.DataAccess.Repositories;
|
||||||
|
|
||||||
|
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.AddSwaggerGen();
|
||||||
|
|
||||||
|
builder.Services.AddDbContext<MonitorPanelDbContext>(options =>
|
||||||
|
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
||||||
|
|
||||||
|
builder.Services.AddScoped<IServersRepository, ServersRepository>();
|
||||||
|
|
||||||
|
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();
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||||
|
"profiles": {
|
||||||
|
"http": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": false,
|
||||||
|
"applicationUrl": "http://localhost:5068",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": false,
|
||||||
|
"applicationUrl": "https://localhost:7204;http://localhost:5068",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using MonitorPanel.Core.Models;
|
||||||
|
namespace MonitorPanel.Core.Abstractions;
|
||||||
|
|
||||||
|
public interface IServersRepository
|
||||||
|
{
|
||||||
|
Task<IEnumerable<Server>> GetAllServersAsync();
|
||||||
|
Task<Server?> GetServerByIdAsync(Guid id);
|
||||||
|
Task<Guid> AddServerAsync(Server server);
|
||||||
|
Task<Guid> UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port);
|
||||||
|
Task<Guid> DeleteServerAsync(Guid id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
namespace MonitorPanel.Core.Models;
|
||||||
|
|
||||||
|
public class Server(
|
||||||
|
Guid id,
|
||||||
|
string name,
|
||||||
|
bool isHttps,
|
||||||
|
string address,
|
||||||
|
string? path,
|
||||||
|
int port)
|
||||||
|
{
|
||||||
|
public readonly Guid Id = id;
|
||||||
|
public string Name { get; set; } = name;
|
||||||
|
public bool IsHttps { get; set; } = isHttps;
|
||||||
|
public string Address { get; set; } = address;
|
||||||
|
public string? Path { get; set; } = path;
|
||||||
|
public int Port { get; set; } = port;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace MonitorPanel.Core.Models;
|
||||||
|
|
||||||
|
public class WeatherForecast
|
||||||
|
{
|
||||||
|
public DateOnly Date { get; set; }
|
||||||
|
|
||||||
|
public int TemperatureC { get; set; }
|
||||||
|
|
||||||
|
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||||
|
|
||||||
|
public string? Summary { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
namespace MonitorPanel.DataAccess.Entities;
|
||||||
|
|
||||||
|
public class ServerEntity
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public string Name { get; set; } = null!;
|
||||||
|
public bool IsHttps { get; set; }
|
||||||
|
[Required]
|
||||||
|
public string Address { get; set; } = null!;
|
||||||
|
public string? Path { get; set; }
|
||||||
|
[Required]
|
||||||
|
public int Port { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.9" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.9">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.9">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\MonitorPanel.Core\MonitorPanel.Core.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using MonitorPanel.DataAccess.Entities;
|
||||||
|
namespace MonitorPanel.DataAccess;
|
||||||
|
|
||||||
|
public class MonitorPanelDbContext(DbContextOptions<MonitorPanelDbContext> options) : DbContext(options)
|
||||||
|
{
|
||||||
|
public DbSet<ServerEntity> Servers => Set<ServerEntity>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using MonitorPanel.Core.Abstractions;
|
||||||
|
using MonitorPanel.Core.Models;
|
||||||
|
using MonitorPanel.DataAccess.Entities;
|
||||||
|
namespace MonitorPanel.DataAccess.Repositories;
|
||||||
|
|
||||||
|
public class ServersRepository(MonitorPanelDbContext db) : IServersRepository
|
||||||
|
{
|
||||||
|
public async Task<Guid> AddServerAsync(Server server)
|
||||||
|
{
|
||||||
|
var entity = new ServerEntity
|
||||||
|
{
|
||||||
|
Id = server.Id,
|
||||||
|
Name = server.Name,
|
||||||
|
IsHttps = server.IsHttps,
|
||||||
|
Address = server.Address,
|
||||||
|
Path = server.Path,
|
||||||
|
Port = server.Port
|
||||||
|
};
|
||||||
|
|
||||||
|
await db.Servers.AddAsync(entity);
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
|
return entity.Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Guid> DeleteServerAsync(Guid id)
|
||||||
|
{
|
||||||
|
await db.Servers.Where(s => s.Id == id).ExecuteDeleteAsync();
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<Server>> GetAllServersAsync()
|
||||||
|
{
|
||||||
|
return await db.Servers.AsNoTracking()
|
||||||
|
.Select(s => new Server(s.Id, s.Name, s.IsHttps, s.Address, s.Path, s.Port))
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Server?> GetServerByIdAsync(Guid id)
|
||||||
|
{
|
||||||
|
var entity = await db.Servers.FirstOrDefaultAsync(s => s.Id == id);
|
||||||
|
return entity == null ? null : new Server(entity.Id, entity.Name, entity.IsHttps, entity.Address, entity.Path, entity.Port);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Guid> UpdateServerAsync(Guid id, string name, bool isHttps, string address, string? path, int port)
|
||||||
|
{
|
||||||
|
var entity = await db.Servers.FirstOrDefaultAsync(s => s.Id == id)
|
||||||
|
?? throw new InvalidOperationException("Server not found");
|
||||||
|
|
||||||
|
entity.Name = name;
|
||||||
|
entity.IsHttps = isHttps;
|
||||||
|
entity.Address = address;
|
||||||
|
entity.Path = path;
|
||||||
|
entity.Port = port;
|
||||||
|
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
return entity.Id;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<Solution>
|
||||||
|
<Project Path="MonitorPanel.API/MonitorPanel.API.csproj" />
|
||||||
|
<Project Path="MonitorPanel.Core/MonitorPanel.Core.csproj" />
|
||||||
|
<Project Path="MonitorPanel.DataAccess/MonitorPanel.DataAccess.csproj" />
|
||||||
|
</Solution>
|
||||||
Reference in New Issue
Block a user