Beispiel #1
0
        /// <summary>
        /// Collects cpu usage stats and forwards to the <see cref="CollectMessage.Receivers"/>
        /// </summary>
        /// <param name="message"><see cref="CollectMessage"/></param>
        private void CollectStats(CollectMessage message)
        {
            _output.Tell(new SimpleMessage($"CpuCollectionActor received collect dated {message.CollectUtc:dd/MM/yyyy HH:mm:ss}"));

            var processorSearcher = new ManagementObjectSearcher(@"root\CIMV2",
                                                                 "SELECT * FROM Win32_PerfFormattedData_Counters_ProcessorInformation");

            var obj = processorSearcher.Get().Cast <ManagementObject>().FirstOrDefault();

            if (obj != null)
            {
                var percent = double.Parse(obj["PercentProcessorTime"].ToString());
                var msg     = new CpuMessage(percent / 100);
                foreach (var r in message.Receivers)
                {
                    r.Tell(msg);
                }
            }
        }
        /// <summary>
        /// Collects memory usage stats and forwards to the <see cref="CollectMessage.Receivers"/>
        /// </summary>
        /// <param name="message"><see cref="CollectMessage"/></param>
        private void CollectStats(CollectMessage message)
        {
            _output.Tell(new SimpleMessage($"MemoryCollectionActor received collect dated {message.CollectUtc:dd/MM/yyyy HH:mm:ss}"));

            var wmiObject = new ManagementObjectSearcher("select * from Win32_OperatingSystem");

            var memoryValues = wmiObject.Get().Cast <ManagementObject>().Select(mo => new {
                FreePhysicalMemory     = Double.Parse(mo["FreePhysicalMemory"].ToString()),
                TotalVisibleMemorySize = Double.Parse(mo["TotalVisibleMemorySize"].ToString())
            }).FirstOrDefault();

            if (memoryValues != null)
            {
                var used = (memoryValues.TotalVisibleMemorySize - memoryValues.FreePhysicalMemory);
                var msg  = new MemoryMessage(used);
                foreach (var r in message.Receivers)
                {
                    r.Tell(msg);
                }
            }
        }