需求:整机cpu,内存,网络流量;每个服务占用cpu,内存,线程数
public class CPU { public void Run() { string name = "Service"; //Process[] p =Process.GetProcesses() ; Process[] p1 = Process.GetProcessesByName("tService"); PerformanceCounter cpuCounter1 = new PerformanceCounter("Process", "% Processor Time", name);//% User Time % Processor Time PerformanceCounter ramCounter1 = new PerformanceCounter("Process", "Working Set - Private", name); //PerformanceCounter ramCounter1 = new PerformanceCounter("Memory", "Available MBytes", name); //IPGlobalStatistics ipstat = properties.GetIPv4GlobalStatistics(); Console.WriteLine(name + "电脑CPU使用率:" + cpuCounter1.NextValue() + "%"); Console.WriteLine(name + "电脑可使用内存:" + ramCounter1.NextValue() / 1024 / 1024 + "MB"); //Console.WriteLine($"接收数据包:{ipstat.ReceivedPackets/1024}Kbps"); //Console.WriteLine($"发送数据包:{ipstat.ReceivedPacketsDelivered / 1024}Kbps"); Console.WriteLine(); PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");//可以用内存 % Committed Bytes In Use 内存使用率 Console.WriteLine("电脑CPU使用率:" + cpuCounter.NextValue() + "%"); Console.WriteLine("电脑可使用内存:" + ramCounter.NextValue() + "MB"); Console.WriteLine(); IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); IPGlobalStatistics ipstat = properties.GetIPv4GlobalStatistics(); long receivedPBegin = ipstat.ReceivedPackets; long ReceivedPDBegin = ipstat.ReceivedPacketsDelivered; while (true) { System.Threading.Thread.Sleep(1000); ipstat = properties.GetIPv4GlobalStatistics(); Console.WriteLine("电脑CPU使用率:" + cpuCounter.NextValue() + " %"); Console.WriteLine("电脑可使用内存:" + ramCounter.NextValue() + "MB"); long receivedPackets = ipstat.ReceivedPackets; long receivedPacketsD = ipstat.ReceivedPacketsDelivered; Console.WriteLine($"接收数据包:{receivedPackets - receivedPBegin}"); Console.WriteLine($"发送数据包:{receivedPacketsD - ReceivedPDBegin}"); Console.WriteLine(); Console.WriteLine(name + "电脑CPU使用率:" + cpuCounter1.NextValue() + "%"); Console.WriteLine(name + "服务内存===:" + ramCounter1.NextValue() / 1024 / 1024 + "MB"); Console.WriteLine(name + "线程数量:" + Process.GetProcessesByName(name)[0].Threads.Count); Console.WriteLine(); receivedPBegin = receivedPackets; ReceivedPDBegin = receivedPacketsD; } } }
上下行网速:
public void Run2() { //初始化Counter PerformanceCounterCategory pcCategory = new PerformanceCounterCategory("Network Interface"); string[] iNames = pcCategory.GetInstanceNames();//此函数第一次执行特别耗时(不知道为什么) PerformanceCounter[] pCounters = pcCategory.GetCounters(iNames[0]);//iNames[0]为"ASIX AX88772C USB2.0 to Fast Ethernet Adapter";iNames[1]为"Intel[R] Ethernet Connection [7] I219-V" //pCounters. //给网络监控计数器赋值 mCounter = pCounters[0]; mCounter.NextValue();//初始值 while (true) { System.Threading.Thread.Sleep(1000); double SpeedKbps = mCounter.NextValue() * 8 / 1000; if ((SpeedKbps / 1000) > 1) { Console.WriteLine(String.Format("{0:f1} Mbps", SpeedKbps / 1000)); //得到该适配器的上传速度 } else { Console.WriteLine(String.Format("{0:f1} Kbps", SpeedKbps)); //得到该适配器的上传速度 } } }
查询PerformanceCounterCategory属性:
public void Run() { string[] instanceNames; ArrayList counters = new ArrayList(); PerformanceCounterCategory mycat = new PerformanceCounterCategory("Memory"); try { instanceNames = mycat.GetInstanceNames(); if (instanceNames.Length == 0) { counters.AddRange(mycat.GetCounters()); } else { for (int i = 0; i < instanceNames.Length; i++) { counters.AddRange(mycat.GetCounters(instanceNames[i])); } } for (int i = 0; i < instanceNames.Length; i++) { Console.WriteLine(instanceNames[i]); } Console.WriteLine("******************************"); foreach (PerformanceCounter counter in counters) { Console.WriteLine(counter.CounterName); } } catch (Exception) { Console.WriteLine("Unable to list the counters for this category"); } }