Creating server daemon. Added system info (not host info) and data usage endpoints
This commit is contained in:
52
MoonlightServers.Daemon/Services/DockerInfoService.cs
Normal file
52
MoonlightServers.Daemon/Services/DockerInfoService.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Docker.DotNet;
|
||||
using MoonCore.Attributes;
|
||||
using MoonlightServers.Daemon.Helpers;
|
||||
using MoonlightServers.Daemon.Models.UnsafeDocker;
|
||||
|
||||
namespace MoonlightServers.Daemon.Services;
|
||||
|
||||
[Singleton]
|
||||
public class DockerInfoService
|
||||
{
|
||||
private readonly DockerClient DockerClient;
|
||||
private readonly UnsafeDockerClient UnsafeDockerClient;
|
||||
|
||||
public DockerInfoService(DockerClient dockerClient, UnsafeDockerClient unsafeDockerClient)
|
||||
{
|
||||
DockerClient = dockerClient;
|
||||
UnsafeDockerClient = unsafeDockerClient;
|
||||
}
|
||||
|
||||
public async Task<string> GetDockerVersion()
|
||||
{
|
||||
var version = await DockerClient.System.GetVersionAsync();
|
||||
|
||||
return $"{version.Version} commit {version.GitCommit} ({version.APIVersion})";
|
||||
}
|
||||
|
||||
public async Task<UsageDataReport> GetDataUsage()
|
||||
{
|
||||
var response = await UnsafeDockerClient.GetDataUsage();
|
||||
|
||||
var report = new UsageDataReport()
|
||||
{
|
||||
Containers = new UsageData()
|
||||
{
|
||||
Used = response.Containers.Sum(x => x.SizeRw),
|
||||
Reclaimable = 0
|
||||
},
|
||||
Images = new UsageData()
|
||||
{
|
||||
Used = response.Images.Sum(x => x.Size),
|
||||
Reclaimable = response.Images.Where(x => x.Containers == 0).Sum(x => x.Size)
|
||||
},
|
||||
BuildCache = new UsageData()
|
||||
{
|
||||
Used = response.BuildCache.Sum(x => x.Size),
|
||||
Reclaimable = response.BuildCache.Where(x => !x.InUse).Sum(x => x.Size)
|
||||
}
|
||||
};
|
||||
|
||||
return report;
|
||||
}
|
||||
}
|
||||
101
MoonlightServers.Daemon/Services/SystemInfoService.cs
Normal file
101
MoonlightServers.Daemon/Services/SystemInfoService.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using Docker.DotNet;
|
||||
using MoonCore.Attributes;
|
||||
using MoonlightServers.Daemon.Helpers;
|
||||
|
||||
namespace MoonlightServers.Daemon.Services;
|
||||
|
||||
[Singleton]
|
||||
public class SystemInfoService
|
||||
{
|
||||
private readonly ILogger<SystemInfoService> Logger;
|
||||
private readonly IHost Host;
|
||||
|
||||
public SystemInfoService(
|
||||
ILogger<SystemInfoService> logger,
|
||||
IHost host
|
||||
)
|
||||
{
|
||||
Logger = logger;
|
||||
Host = host;
|
||||
}
|
||||
|
||||
public Task<string> GetOsName()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
// Windows platform detected
|
||||
var osVersion = Environment.OSVersion.Version;
|
||||
return Task.FromResult($"Windows {osVersion.Major}.{osVersion.Minor}.{osVersion.Build}");
|
||||
}
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
var releaseRaw = File
|
||||
.ReadAllLines("/etc/os-release")
|
||||
.FirstOrDefault(x => x.StartsWith("PRETTY_NAME="));
|
||||
|
||||
if (string.IsNullOrEmpty(releaseRaw))
|
||||
return Task.FromResult("Linux (unknown release)");
|
||||
|
||||
var release = releaseRaw
|
||||
.Replace("PRETTY_NAME=", "")
|
||||
.Replace("\"", "");
|
||||
|
||||
if (string.IsNullOrEmpty(release))
|
||||
return Task.FromResult("Linux (unknown release)");
|
||||
|
||||
return Task.FromResult(release);
|
||||
}
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
// macOS platform detected
|
||||
var osVersion = Environment.OSVersion.Version;
|
||||
return Task.FromResult($"macOS {osVersion.Major}.{osVersion.Minor}.{osVersion.Build}");
|
||||
}
|
||||
|
||||
// Unknown platform
|
||||
return Task.FromResult("N/A");
|
||||
}
|
||||
|
||||
public Task<long> GetMemoryUsage()
|
||||
{
|
||||
var process = Process.GetCurrentProcess();
|
||||
var bytes = process.PrivateMemorySize64;
|
||||
return Task.FromResult(bytes);
|
||||
}
|
||||
|
||||
public Task<TimeSpan> GetUptime()
|
||||
{
|
||||
var process = Process.GetCurrentProcess();
|
||||
var uptime = DateTime.Now - process.StartTime;
|
||||
return Task.FromResult(uptime);
|
||||
}
|
||||
|
||||
public Task<int> GetCpuUsage()
|
||||
{
|
||||
var process = Process.GetCurrentProcess();
|
||||
var cpuTime = process.TotalProcessorTime;
|
||||
var wallClockTime = DateTime.UtcNow - process.StartTime.ToUniversalTime();
|
||||
|
||||
var cpuUsage = (int)(100.0 * cpuTime.TotalMilliseconds / wallClockTime.TotalMilliseconds /
|
||||
Environment.ProcessorCount);
|
||||
|
||||
return Task.FromResult(cpuUsage);
|
||||
}
|
||||
|
||||
public Task Shutdown()
|
||||
{
|
||||
Logger.LogCritical("Restart of daemon has been requested");
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(1));
|
||||
await Host.StopAsync(CancellationToken.None);
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user