linux support

This commit is contained in:
Ahmed Zamil 2024-06-28 14:48:28 +03:00
parent fd63466322
commit d4d0a77559
2 changed files with 97 additions and 16 deletions

View File

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Esiur" Version="2.4.1" />
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
<PackageReference Include="System.Management" Version="8.0.0" />
</ItemGroup>

View File

@ -1,6 +1,7 @@
using Esiur.Core;
using Esiur.Data;
using Esiur.Resource;
using Mono.Unix.Native;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@ -25,6 +26,10 @@ namespace AZ.Compute.Agent
const double nicSpeed = 1 * 1024 * 1024; // 1 MBps
// linux
float prevCpuIdle = 0f;
float prevCpuTotal = 0f;
[Export] string id;
[Export] string ip;
@ -63,6 +68,70 @@ namespace AZ.Compute.Agent
return job;
}
ulong GetNetworkLinux()
{
var netLines = File
.ReadAllLines("/proc/net/dev")
.Skip(3)
.First()
.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.Select(ulong.Parse)
.ToArray();
// Console.WriteLine($"NetL {netLines[0]} {netLines[8]}");
return netLines[0] + netLines[8];
}
float GetLinuxCPU(float interval = 5)
{
var cpuLine = File
.ReadAllLines("/proc/stat")
.First()
.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.Select(float.Parse)
.ToArray();
var idle = cpuLine[3];
var total = cpuLine.Sum();
var percent = 100.0 * (1.0 - (idle - prevCpuIdle) / (total - prevCpuTotal));
//Console.WriteLine($"CPU {percent:0.00}%");
prevCpuIdle = idle;
prevCpuTotal = total;
return (float)percent / interval;
}
float GetLinuxRAM()
{
return File
.ReadAllLines("/proc/meminfo")
.Skip(1)
.First()
.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.Select(ulong.Parse)
.First() / 1024;
//Console.WriteLine("Free " + freeMem);
var pages = Syscall.sysconf(SysconfName._SC_PHYS_PAGES);
var pageSize = Syscall.sysconf(SysconfName._SC_PAGESIZE);
var ram = pages * pageSize / 1024.0f;
Console.WriteLine($"RAM {ram:0.00}%");
return ram;
}
public Node()
{
@ -75,6 +144,8 @@ namespace AZ.Compute.Agent
Ip = nic.GetIPProperties().UnicastAddresses.First(x => x.Address.AddressFamily == AddressFamily.InterNetwork).Address.ToString();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
using (ManagementObject Mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'"))
{
@ -82,15 +153,10 @@ namespace AZ.Compute.Agent
cpuMaxClock = (uint)(Mo["MaxClockSpeed"]);
}
//foreach (ManagementObject obj in new ManagementObjectSearcher("SELECT *, Name FROM Win32_Processor").Get())
//{
// double maxSpeed = Convert.ToDouble(obj["MaxClockSpeed"]) / 1000;
// double turboSpeed = maxSpeed * cpuValue / 100;
// return string.Format("{0} Running at {1:0.00}Ghz, Turbo Speed: {2:0.00}Ghz", obj["Name"], maxSpeed, turboSpeed);
//}
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
}
timer = new System.Timers.Timer();
timer.Elapsed += Timer_Elapsed;
@ -103,14 +169,28 @@ namespace AZ.Compute.Agent
try
{
var ntb = (ulong)nic.GetIPv4Statistics().BytesReceived + (ulong)nic.GetIPv4Statistics().BytesSent;
ulong ntb = 0;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
ntb = (ulong)nic.GetIPv4Statistics().BytesReceived + (ulong)nic.GetIPv4Statistics().BytesSent;
Cpu = cpuCounter.NextValue();
Ram = ramCounter.NextValue();
}
else
{
ntb = GetNetworkLinux();
Cpu = GetLinuxCPU();
Ram = GetLinuxRAM();
}
Network = (float)(((ntb - totalNetworkBytes) / 5.0) / nicSpeed);
totalNetworkBytes = ntb;
Cpu = cpuCounter.NextValue();
Ram = ramCounter.NextValue();
//Console.WriteLine("Net " + Network + " " + ntb);
foreach (Job job in jobs)
{