whuanle / CZGL.SystemInfo

一个.NET Core监控系统CPU内存等信息的工具
353 stars 68 forks source link

Get Unix Memory #1

Closed whuanle closed 2 years ago

whuanle commented 3 years ago

no unsupport Alpine Linux!

    private MemoryMetrics GetUnixMetrics()
    {
        var output = "";

        var info = new ProcessStartInfo("free -m");
        info.FileName = "/bin/bash";
        info.Arguments = "-c \"free -m\"";
        info.RedirectStandardOutput = true;
        
        using(var process = Process.Start(info))
        {                
            output = process.StandardOutput.ReadToEnd();
            Console.WriteLine(output);
        }

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

        return metrics;            
    }