Ejemplo n.º 1
0
        private CpuMetrics GetUnixMetrics()
        {
            var output = "";

            var info = new ProcessStartInfo("Iostat -c");

            info.FileName  = "/bin/bash";
            info.Arguments = "-c \"Iostat -c\"";
            info.RedirectStandardOutput = true;

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

            var lines = output.Split("\n");
            var cpu   = lines[2].Split(" ", StringSplitOptions.RemoveEmptyEntries);

            var metrics = new CpuMetrics();

            metrics.LoadPercentage = int.Parse(cpu[0]);

            return(metrics);
        }
Ejemplo n.º 2
0
        private CpuMetrics GetWindowsMetrics()
        {
            var output = "";

            var info = new ProcessStartInfo();

            info.FileName  = "wmic";
            info.Arguments = "cpu get loadpercentage /value";
            info.RedirectStandardOutput = true;

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

            var lines           = output.Trim().Split("\n");
            var totalPercentage = lines[0].Split("=", StringSplitOptions.RemoveEmptyEntries);
            //var totalPercentage = lines[1].Split("=", StringSplitOptions.RemoveEmptyEntries);

            var metrics = new CpuMetrics();

            try
            {
                metrics.LoadPercentage = int.Parse(totalPercentage[1]); // Math.Round(/ 1024, 2);
            }
            catch (Exception)
            {
                metrics.LoadPercentage = -1;
            }


            return(metrics);
        }
Ejemplo n.º 3
0
 public void Create(CpuMetrics item)
 {
     using (var connection = new SQLiteConnection(SQLConnected.ConnectionString))
     {
         connection.Execute(@"INSERT INTO cpumetrics(value, time) VALUES(@value, @time)",
                            new
         {
             value = item.Value,
             time  = item.Time.ToUnixTimeSeconds()
         });
     }
 }
Ejemplo n.º 4
0
        public bool CheckHealth()
        {
            //var isWin = !IsUnix();

            //if (isWin)
            //{
            //    return GetWindowsBasedMetrics();
            //}
            CpuMetrics infoMpStat = GetUnixCpuInfoWithMpStat();
            CpuMetrics infoTop    = GetUnixCpuInfoWithTop();
            var        message    = $", CPU Usage: {infoMpStat.LoadPercentage:F}%";

            Logger.LogCritical($"infoMpStat {infoMpStat.LoadPercentage} raw data {infoMpStat.RawData}");
            Logger.LogError($"infoTop {infoTop.LoadPercentage} raw data {infoTop.RawData}");
            return(true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 获取CPU信息
        /// </summary>
        /// <returns></returns>
        private static CpuMetrics GetCpuMetrics()
        {
            var cpu = new CpuMetrics();

            if (_osMetrics.IsUnix)
            {
                var cpuName = ShellHelper.Bash("cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c").Split(" ", StringSplitOptions.RemoveEmptyEntries);
                cpu.CoreCount   = Convert.ToInt32(cpuName[0].Trim());
                cpuName[0]      = string.Empty;
                cpu.CpuName     = string.Join(" ", cpuName).TrimStart().Trim();
                cpu.ThreadCount = Convert.ToInt32(ShellHelper.Bash("grep 'processor' /proc/cpuinfo | sort -u | wc -l").Trim());
                cpu.Used        = ShellHelper.Bash("top -b -n1 | grep \"Cpu(s)\" | awk '{print $2 + $4}'").Trim() + " %";
            }
            else
            {
                cpu.CpuName     = ShellHelper.Cmd("wmic", "cpu get Name").Replace("Name", string.Empty).Trim();
                cpu.CoreCount   = Convert.ToInt32(ShellHelper.Cmd("wmic", "cpu get NumberOfCores").Replace("NumberOfCores", string.Empty).Trim());
                cpu.ThreadCount = Convert.ToInt32(ShellHelper.Cmd("wmic", "cpu get ThreadCount").Replace("ThreadCount", string.Empty).Trim());
                cpu.Used        = ShellHelper.Cmd("wmic", "cpu get LoadPercentage").Replace("LoadPercentage", string.Empty).Trim() + " %";
            }

            return(cpu);
        }
Ejemplo n.º 6
0
        private CpuMetrics GetCpuMetrics()
        {
            var result = new CpuMetrics();

            using (var currentProcess = Process.GetCurrentProcess())
                                                                      #pragma warning disable CA1416 // Validate platform compatibility
                result.AssignedProcessorCount = (int)Bits.NumberOfSetBits(currentProcess.ProcessorAffinity.ToInt64());
#pragma warning restore CA1416                                                                       // Validate platform compatibility

            result.ProcessorCount = Environment.ProcessorCount;

            ThreadPool.GetAvailableThreads(out var workerThreads, out var completionPortThreads);
            result.ThreadPoolAvailableWorkerThreads         = workerThreads;
            result.ThreadPoolAvailableCompletionPortThreads = completionPortThreads;

            var cpuUsage = Server.MetricCacher.GetValue(MetricCacher.Keys.Server.CpuUsage, Server.CpuUsageCalculator.Calculate);

            result.ProcessUsage  = cpuUsage.ProcessCpuUsage;
            result.MachineUsage  = cpuUsage.MachineCpuUsage;
            result.MachineIoWait = cpuUsage.MachineIoWait;

            return(result);
        }
Ejemplo n.º 7
0
        public void CpuMetrics()
        {
            CpuMetrics metrics = Runtime.GetCpuMetrics();

            Assert.True(metrics != null);
        }
Ejemplo n.º 8
0
 public SystemMetrics(CpuMetrics cpu, MemoryMetrics memory)
 {
     Cpu    = cpu;
     Memory = memory;
 }