public override async Task <OSInfo> GetOSInfoAsync(CancellationToken cancellationToken) { OSInfo osInfo = default(OSInfo); (int exitCode, List <string> outputLines) = await ExecuteProcessAsync("lsb_release", "-d"); if (exitCode == 0 && outputLines.Count == 1) { /* ** Example: ** Description:\tUbuntu 18.04.2 LTS */ osInfo.Name = outputLines[0].Split(':', count: 2)[1].Trim(); } osInfo.Version = await File.ReadAllTextAsync("/proc/version"); osInfo.Language = string.Empty; osInfo.Status = "OK"; osInfo.NumberOfProcesses = Process.GetProcesses().Length; // Source code: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/proc/meminfo.c Dictionary <string, ulong> memInfo = LinuxProcFS.ReadMemInfo(); osInfo.TotalVisibleMemorySizeKB = memInfo[MemInfoConstants.MemTotal]; osInfo.FreePhysicalMemoryKB = memInfo[MemInfoConstants.MemFree]; osInfo.AvailableMemoryKB = memInfo[MemInfoConstants.MemAvailable]; // On Windows, TotalVirtualMemorySize = TotalVisibleMemorySize + SizeStoredInPagingFiles. // SizeStoredInPagingFiles - Total number of kilobytes that can be stored in the operating system paging files—0 (zero) // indicates that there are no paging files. Be aware that this number does not represent the actual physical size of the paging file on disk. // https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem osInfo.TotalVirtualMemorySizeKB = osInfo.TotalVisibleMemorySizeKB + memInfo[MemInfoConstants.SwapTotal]; osInfo.FreeVirtualMemoryKB = osInfo.FreePhysicalMemoryKB + memInfo[MemInfoConstants.SwapFree]; (float uptime, float idleTime) = await LinuxProcFS.ReadUptimeAsync(); osInfo.LastBootUpTime = DateTime.UtcNow.AddSeconds(-uptime).ToString("o"); try { osInfo.InstallDate = new DirectoryInfo("/var/log/installer").CreationTimeUtc.ToString("o"); } catch (IOException) { osInfo.InstallDate = "N/A"; } return(osInfo); }
/// <summary> /// Reads data from the /proc/meminfo file. /// </summary> public static Dictionary <string, ulong> ReadMemInfo() { // Currently /proc/meminfo contains 51 rows on Ubuntu 18. Dictionary <string, ulong> result = new Dictionary <string, ulong>(capacity: 64); // Source code: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/proc/meminfo.c using (StreamReader sr = new StreamReader("/proc/meminfo", encoding: Encoding.UTF8)) { string line; while ((line = sr.ReadLine()) != null) { int colonIndex = line.IndexOf(':'); string key = line.Substring(0, colonIndex); ulong value = LinuxProcFS.ReadUInt64(line, colonIndex + 1); result.Add(key, value); } } return(result); }