mirror of
https://github.com/ryzij/ServerMonitor.git
synced 2026-07-14 03:46:58 +00:00
First commit
This commit is contained in:
+86
@@ -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
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace MonitorAgent.API.Contracts;
|
||||||
|
|
||||||
|
public record class LoadAverageResponse(decimal[] LoadAverage);
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace MonitorAgent.API.Contracts;
|
||||||
|
|
||||||
|
public record class MemoryResponse
|
||||||
|
(
|
||||||
|
int MemTotal,
|
||||||
|
int MemFree,
|
||||||
|
int MemAvailable,
|
||||||
|
int SwapTotal,
|
||||||
|
int SwapFree
|
||||||
|
);
|
||||||
@@ -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<ActionResult<decimal>> GetUptimeAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var uptime = await monitorService.GetUptimeAsync(cancellationToken);
|
||||||
|
return Ok(uptime);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("memory")]
|
||||||
|
public async Task<ActionResult<MemoryResponse>> 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<ActionResult<LoadAverageResponse>> GetActionResultAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var response = new LoadAverageResponse(await monitorService.GetLoadAverageAsync(cancellationToken));
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<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="Swashbuckle.AspNetCore" Version="10.2.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\MonitorAgent.Core\MonitorAgent.Core.csproj" />
|
||||||
|
<ProjectReference Include="..\MonitorAgent.Application\MonitorAgent.Application.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
@MonitorAgent.API_HostAddress = http://localhost:5170
|
||||||
|
|
||||||
|
GET {{MonitorAgent.API_HostAddress}}/weatherforecast/
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
###
|
||||||
@@ -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<IMonitorService, MonitorService>();
|
||||||
|
|
||||||
|
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:5170",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": false,
|
||||||
|
"applicationUrl": "https://localhost:7032;http://localhost:5170",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\MonitorAgent.Core\MonitorAgent.Core.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -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<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);
|
||||||
|
// }
|
||||||
|
|
||||||
|
public async Task<decimal> GetUptimeAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var uptime = await File.ReadAllTextAsync("/proc/uptime", cancellationToken);
|
||||||
|
return decimal.Parse(uptime.Split()[1], CultureInfo.InvariantCulture);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<MemoryInfo> 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<decimal[]> 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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using MonitorAgent.Core.Models;
|
||||||
|
|
||||||
|
namespace MonitorAgent.Core.Abstraction;
|
||||||
|
|
||||||
|
public interface IMonitorService
|
||||||
|
{
|
||||||
|
public Task<decimal> GetUptimeAsync(CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<MemoryInfo> GetMemoryInfoAsync(CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<decimal[]> GetLoadAverageAsync(CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
@@ -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,6 @@
|
|||||||
|
<Solution>
|
||||||
|
<Project Path="MonitorAgent.API/MonitorAgent.API.csproj" />
|
||||||
|
<Project Path="MonitorAgent.Application/MonitorAgent.Application.csproj" />
|
||||||
|
<Project Path="MonitorAgent.Core/MonitorAgent.Core.csproj" />
|
||||||
|
|
||||||
|
</Solution>
|
||||||
Reference in New Issue
Block a user