Ejemplo n.º 1
0
        public override async Task <float> NextValueAsync()
        {
            if (this.uptimeInSeconds == -1)
            {
                throw new ObjectDisposedException(nameof(LinuxCpuUtilizationProvider));
            }

            (float ut, float it) = await LinuxProcFS.ReadUptimeAsync();

            if (ut == this.uptimeInSeconds)
            {
                return(this.cpuUtilization);
            }

            this.cpuUtilization = 100 - ((it - this.idleTimeInSeconds) / (ut - this.uptimeInSeconds) / Environment.ProcessorCount * 100);

            if (this.cpuUtilization < 0)
            {
                this.cpuUtilization = 0;
            }

            this.uptimeInSeconds   = ut;
            this.idleTimeInSeconds = it;

            return(this.cpuUtilization);
        }
        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);
        }