• 通过WMI获取机器信息


    PerformanceCounter的介绍就不多说了,MSDN上介绍的很详细: https://msdn.microsoft.com/zh-cn/library/system.diagnostics.performancecounter(v=vs.110).aspx

     1 //对PerformanceCounter进行封装
     2  public class PerformanceCounterCollect
     3     {
     4         private PerformanceCounter counter = null;
     5         public PerformanceCounterCollect(string categoryName, string counterName, string instanceName)
     6         {
     7             counter = new PerformanceCounter(categoryName, counterName, instanceName, ".");
     8             try
     9             {
    10                 Collect();//开启采集
    11                 System.Threading.Thread.Sleep(100);
    12             }
    13             catch { }
    14         }
    15         public float Collect()
    16         {
    17             try
    18             {
    19                 return counter.NextValue();
    20             }
    21             catch (Exception ex)
    22             {
    23                 return -1;
    24             }
    25         }
    26     }
    View Code

    收集CpuUtilization:new PerformanceCounterCollect("Processor", "% Processor Time", "_Total")

    收集DiskReadSpeed: new PerformanceCounterCollect("PhysicalDisk", "Disk Read Bytes/sec", "_Total")

    收集DiskWriteSpeed:new PerformanceCounterCollect("PhysicalDisk", "Disk Write Bytes/sec", "_Total")

    收集MemoryAvailable:new PerformanceCounterCollect("Memory", "Available MBytes", "")

    收集NetworkSendSpeed这个有一些繁琐,因为机器机器多网卡是很普遍的,要获取Enable的网卡,代码如下:

     1  var ethernetInstance = GetEthernetInstance();
     2 
     3   public static string GetEthernetInstance()
     4         {
     5             var result = new PerformanceCounterCategory("Network Interface");
     6             var instanceNames = result.GetInstanceNames();
     7             var ethernetDescriptions = GetEthernetDescriptions();
     8             foreach (var instance in instanceNames)
     9             {
    10                 if (ethernetDescriptions.Contains(instance))
    11                 {
    12                     return instance;
    13                 }
    14             }
    15             return "";
    16             //throw new Exception("Not found enable ethernet instance.");
    17         }
    18 
    19  public static List<string> GetEthernetDescriptions()
    20         {
    21             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    22             ManagementObjectCollection moc = mc.GetInstances();
    23             List<string> descriptions = new List<string>();
    24             foreach (ManagementObject mo in moc)
    25             {
    26                 if ((bool)mo["IPEnabled"])
    27                 {
    28                     descriptions.Add(DesConvertInstance(mo["Description"].ToString()));
    29                 }
    30             }
    31             return descriptions;
    32         }
    33 static string DesConvertInstance(string description)
    34         {
    35             foreach (var ch in changeChars)
    36             {
    37                 description = description.Replace(ch.Key, ch.Value);
    38             }
    39             return description;
    40         }
    41 static Dictionary<string, string> changeChars = new Dictionary<string, string>()
    42         {
    43             {"(", "[" },
    44             {")", "]" },
    45             {"#", "_" },
    46         };
    View Code

    收集NetworkSendSpeed:new PerformanceCounterCollect("Network Interface", "Bytes Sent/sec", ethernetInstance)

    收集NetworkReceivedSpeed: new PerformanceCounterCollect("Network Interface", "Bytes Received/sec", ethernetInstance)

    WMI还有很多,大家有兴趣可以查看以下网址:

    https://msdn.microsoft.com/en-us/library/dn792179(v=vs.85).aspx

  • 相关阅读:
    Debian常用操作
    debian-11.0.0-amd64
    CentOS-8.4.2105-x86_64
    第20章: 二进制方式部署K8S1.20高可用集群
    天狮集团云函数实践:自定义业务逻辑实现跨境电商全球直播
    峰值利用率80%+,视频云离线转码自研上云TKE实践
    腾讯首个CNCF沙箱开源项目
    斗鱼直播云原生实践之注册中心篇
    Superedge的新特性和未来之路
    腾讯云与 Grafana Labs 达成深度合作, 推出全新 Grafana 托管服务
  • 原文地址:https://www.cnblogs.com/zhesong/p/wmimc.html
Copyright © 2020-2023  润新知