使用VMI获取服务器端物理内存、CPU

来源:互联网 发布:淘宝助理是做什么工作 编辑:程序博客网 时间:2024/05/16 08:55
 ConnectionOptions options =
           new ConnectionOptions();
            options.Username = "administrator";
            options.Password = "********";
 
 
            // Make a connection to a remote computer.
            // Replace the "FullComputerName" section of the
            // string "\\\\FullComputerName\\root\\cimv2" with
            // the full computer name or IP address of the
            // remote computer.
            ManagementScope scope =
                new ManagementScope(
                "\\\\FullComputerName\\root\\cimv2", options);
            scope.Connect();
 
            //Query system for Operating System information
            ObjectQuery query = new ObjectQuery(
                "select * from Win32_Processor");
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher(scope, query);
 
            ManagementObjectCollection queryCollection = searcher.Get();
            foreach (ManagementObject m in queryCollection)
            {
                Console.WriteLine("CPU : {0}",
                    m["LoadPercentage"].ToString());   //cpu 使用率
            }
 
            double totalMem = 0;
            string strMsg = "";
            ObjectQuery query1 = new ObjectQuery(
               "Select TotalPhysicalMemory from Win32_LogicalMemoryConfiguration");
            ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query1);
            int i = 0;
            ManagementObjectCollection queryCollection1 = search.Get();
            foreach (ManagementObject info in queryCollection1)
            {
                i = i + 1;
                totalMem += Convert.ToDouble(info["TotalPhysicalMemory"].ToString()) / 1024;
                strMsg += string.Format("物理内存({0}):大小:{1}MB", i, Convert.ToDouble(info["TotalPhysicalMemory"].ToString()) / 1024);
            }
            strMsg += string.Format("总物理内存的大小:{0}MB <br/>", totalMem);
            Console.WriteLine(strMsg);
0 0
原创粉丝点击