Пример #1
0
        public SnapshotResponse Take(string interfaceName)
        {
            var output = "";

            var arguments = string.Format("awk '{{if (NR==1){{r1=$1;}} else if (NR==2){{r2=$1;}} else {{print (((r2-r1)*8/1024/1024)*100/$1);}}}}' " +
                                          "<(cat /proc/net/dev|grep {0} | awk '{{print($2 +$10)}}') " +
                                          "<(sleep 1;cat /proc/net/dev|grep {0} | awk '{{print($2 +$10)}}') " +
                                          "<(cat /sys/class/net/{0}/speed | awk '{{print $1}}')", interfaceName);

            var info = new ProcessStartInfo
            {
                FileName  = "/bin/bash",
                Arguments = arguments,
                RedirectStandardOutput = true,
                UseShellExecute        = false,
                CreateNoWindow         = true,
            };

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

            if (float.TryParse(output.Trim(), out var percentage))
            {
                return(SnapshotResponse.Success(percentage));
            }

            return(SnapshotResponse.Fail("Failed to read info"));
        }
Пример #2
0
        public SnapshotResponse Take()
        {
            var output = "";

            var info = new ProcessStartInfo
            {
                FileName  = "/bin/bash",
                Arguments = "-c \"free | grep Mem | awk '{print $3/$2 * 100.0}'\"",
                RedirectStandardOutput = true,
                UseShellExecute        = false,
                CreateNoWindow         = true,
            };

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

            if (float.TryParse(output.Trim(), out var percentageInUsedMemory))
            {
                return(SnapshotResponse.Success(percentageInUsedMemory));
            }

            return(SnapshotResponse.Fail("Failed to read info"));
        }
Пример #3
0
        public SnapshotResponse Take()
        {
            var output = "";

            var info = new ProcessStartInfo
            {
                FileName  = "/bin/bash",
                Arguments = "-c \"awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1); }' <(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat)\"",
                RedirectStandardOutput = true,
                UseShellExecute        = false,
                CreateNoWindow         = true,
            };

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

            if (float.TryParse(output.Trim(), out var percentageInUsedCpu))
            {
                return(SnapshotResponse.Success(percentageInUsedCpu));
            }

            return(SnapshotResponse.Fail("Failed to read info"));
        }