private MemoryMetrics GetUnixMetrics()
        {
            string           output = "";
            ProcessStartInfo info   = new ProcessStartInfo("free -m")
            {
                FileName  = "/bin/bash",
                Arguments = "-c \"free -m\"",
                RedirectStandardOutput = true
            };

            using (Process process = Process.Start(info))
            {
                output = process.StandardOutput.ReadToEnd();
                Console.WriteLine(output);
            }

            string[]      lines   = output.Split("\n");
            string[]      memory  = lines[1].Split(" ", StringSplitOptions.RemoveEmptyEntries);
            MemoryMetrics metrics = new MemoryMetrics
            {
                Total = double.Parse(memory[1]),
                Used  = double.Parse(memory[2]),
                Free  = double.Parse(memory[3]),
                CheckDurationInMilliseconds = long.Parse(memory[4])
            };

            return(metrics);
        }
示例#2
0
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken)
        {
            MemoryMetricsClient client  = new MemoryMetricsClient();
            MemoryMetrics       metrics = client.GetMetrics();
            double       percentUsed    = 100 * metrics.Used / metrics.Total;
            HealthStatus status         = HealthStatus.Healthy;

            if (percentUsed > 80)
            {
                status = HealthStatus.Degraded;
            }

            if (percentUsed > 90)
            {
                status = HealthStatus.Unhealthy;
            }

            Dictionary <string, object> data = new Dictionary <string, object>
            {
                { "Total", metrics.Total },
                { "Used", metrics.Used },
                { "Free", metrics.Free },
                { "CheckDurationInMilliseconds", metrics.CheckDurationInMilliseconds }
            };

            HealthCheckResult result = new HealthCheckResult(status, null, null, data);

            return(await Task.FromResult(result));
        }
        private MemoryMetrics GetWindowsMetrics()
        {
            string output = "";

            ProcessStartInfo info = new ProcessStartInfo
            {
                FileName  = "wmic",
                Arguments = "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value",
                RedirectStandardOutput = true
            };

            using (Process process = Process.Start(info))
            {
                output = process.StandardOutput.ReadToEnd();
            }

            string[] lines = output.Trim().Split("\n");

            string[] freeMemoryParts  = lines[0].Split("=", StringSplitOptions.RemoveEmptyEntries);
            string[] totalMemoryParts = lines[1].Split("=", StringSplitOptions.RemoveEmptyEntries);

            MemoryMetrics metrics = new MemoryMetrics
            {
                Total = Math.Round(double.Parse(totalMemoryParts[1]) / 1024, 0),
                Free  = Math.Round(double.Parse(freeMemoryParts[1]) / 1024, 0),
            };

            metrics.Used = metrics.Total - metrics.Free;

            return(metrics);
        }