linux support
This commit is contained in:
		| @@ -9,6 +9,7 @@ | |||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <PackageReference Include="Esiur" Version="2.4.1" /> |     <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.Diagnostics.PerformanceCounter" Version="8.0.0" /> | ||||||
|     <PackageReference Include="System.Management" Version="8.0.0" /> |     <PackageReference Include="System.Management" Version="8.0.0" /> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| using Esiur.Core; | using Esiur.Core; | ||||||
| using Esiur.Data; | using Esiur.Data; | ||||||
| using Esiur.Resource; | using Esiur.Resource; | ||||||
|  | using Mono.Unix.Native; | ||||||
| using System; | using System; | ||||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||||
| using System.Diagnostics; | using System.Diagnostics; | ||||||
| @@ -25,6 +26,10 @@ namespace AZ.Compute.Agent | |||||||
|  |  | ||||||
|         const double nicSpeed = 1 * 1024 * 1024; // 1 MBps |         const double nicSpeed = 1 * 1024 * 1024; // 1 MBps | ||||||
|  |  | ||||||
|  |         // linux | ||||||
|  |         float prevCpuIdle = 0f; | ||||||
|  |         float prevCpuTotal = 0f; | ||||||
|  |  | ||||||
|  |  | ||||||
|         [Export] string id; |         [Export] string id; | ||||||
|         [Export] string ip; |         [Export] string ip; | ||||||
| @@ -63,6 +68,70 @@ namespace AZ.Compute.Agent | |||||||
|             return job; |             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() |         public Node() | ||||||
|         { |         { | ||||||
|  |  | ||||||
| @@ -75,22 +144,19 @@ namespace AZ.Compute.Agent | |||||||
|  |  | ||||||
|             Ip = nic.GetIPProperties().UnicastAddresses.First(x => x.Address.AddressFamily == AddressFamily.InterNetwork).Address.ToString(); |             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'")) |  | ||||||
|             { |             { | ||||||
|                 cpuClock = (uint)(Mo["CurrentClockSpeed"]); |  | ||||||
|                 cpuMaxClock = (uint)(Mo["MaxClockSpeed"]); |                 using (ManagementObject Mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'")) | ||||||
|  |                 { | ||||||
|  |                     cpuClock = (uint)(Mo["CurrentClockSpeed"]); | ||||||
|  |                     cpuMaxClock = (uint)(Mo["MaxClockSpeed"]); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); | ||||||
|  |                 ramCounter = new PerformanceCounter("Memory", "Available MBytes"); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             //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 = new System.Timers.Timer(); | ||||||
|             timer.Elapsed += Timer_Elapsed; |             timer.Elapsed += Timer_Elapsed; | ||||||
| @@ -103,14 +169,28 @@ namespace AZ.Compute.Agent | |||||||
|             try |             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); |                 Network = (float)(((ntb - totalNetworkBytes) / 5.0) / nicSpeed); | ||||||
|  |  | ||||||
|                 totalNetworkBytes = ntb; |                 totalNetworkBytes = ntb; | ||||||
|  |  | ||||||
|                 Cpu = cpuCounter.NextValue(); |                 //Console.WriteLine("Net " + Network + " " + ntb); | ||||||
|                 Ram = ramCounter.NextValue(); |  | ||||||
|  |  | ||||||
|                 foreach (Job job in jobs) |                 foreach (Job job in jobs) | ||||||
|                 { |                 { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user