static SystemInfo AppendSysInfo(SystemInfo sysInfo, string targetIP, string processName) { try { sysInfo.TotalMemory = PerfmonCounterManager.GetCounterItemValue("Committed Bytes", string.Empty, "Memory", targetIP); sysInfo.TotalCPU = PerfmonCounterManager.GetSampleItemValue("% Processor Time", "_Total", "Processor", targetIP); if (string.IsNullOrEmpty(processName) == false) { sysInfo.ProcessName = processName; sysInfo.ProcessMemory = PerfmonCounterManager.GetCounterItemValue("Private Bytes", sysInfo.ProcessName, "Process", targetIP); sysInfo.ProcessCPU = PerfmonCounterManager.GetSampleItemValue("% Processor Time", sysInfo.ProcessName, "Process", targetIP); sysInfo.ProcessThreadCount = PerfmonCounterManager.GetCounterItemValue("Thread Count", sysInfo.ProcessName, "Process", targetIP); } } catch (Exception ex) { sysInfo.TotalCPU = -1.0f; sysInfo.ProcessCPU = -1.0f; } return(sysInfo); }
static void RefreshSampleCounterMap() { string[] keys = null; if (SampleCounterMap.Keys.Count > 0) { lock (syscObject) { keys = new string[SampleCounterMap.Keys.Count]; SampleCounterMap.Keys.CopyTo(keys, 0); } foreach (var key in keys) { try { var counter = SampleCounterMap[key].Value; var counterInstance = PerfmonCounterManager.GetCounterInstance(counter.InstanceName, counter.CategoryName, counter.MachineName); if (counterInstance == null) { continue; } var counterItem = (PerformanceCounter)counterInstance.CounterData[counter.ItemName]; var itemValue = counterItem.NextValue(); } catch (Exception ex) { new FatalException(ex).HandleException(); } } } Thread.Sleep(1000); if (keys != null && keys.Length > 0) { foreach (var key in keys) { try { var counter = SampleCounterMap[key].Value; var counterInstance = PerfmonCounterManager.GetCounterInstance(counter.InstanceName, counter.CategoryName, "."); if (counterInstance == null) { continue; } var counterItem = (PerformanceCounter)counterInstance.CounterData[counter.ItemName]; var itemValue = counterItem.NextValue(); SampleCounterMap[key] = new KeyValuePair <float, SampleCounterProxy>(itemValue, counter); } catch (Exception ex) { new FatalException(ex).HandleException(); } } } RefreshSampleCounterMap(); }