mirror of
https://github.com/ryzij/ServerMonitor.git
synced 2026-07-14 03:46:58 +00:00
First commit
This commit is contained in:
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user