1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 using System.Management;
  7 using System.Diagnostics;
  8 using Newtonsoft.Json;
  9 using Newtonsoft.Json.Linq;
 10 
 11 namespace GH.IMS
 12 {
 13     public class SystemMonit
 14     {
 15         private ManagementScope Ms;
 16         
 17         public SystemMonit()
 18         {
 19             Ms = new ManagementScope();
 20         }
 21 
 22         public SystemMonit(String ip, String userName, String password)
 23         {
 24             ConnectionOptions Conn = new ConnectionOptions();
 25             Conn.Username = userName;
 26             Conn.Password = password;
 27             Ms = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", ip), Conn);
 28 
 29             Ms.Connect();
 30         }
 31 
 32 
 33         public JArray GetLogicalDisk()
 34         {
 35             return GetInfoFromWMI("Win32_LogicalDisk", "DeviceID,Size,FreeSpace");
 36         }
 37 
 38         public  JArray GetNAConfigurationInfo()
 39         {
 40             return GetInfoFromWMI("Win32_NetworkAdapterConfiguration",
 41                 "IPAddress,DefaultIPGateway,DNSServerSearchOrder,IPSubnet,MACAddress");
 42         }
 43 
 44 
 45         public JArray GetProcessorInfo()
 46         {
 47             return GetInfoFromWMI("Win32_Processor",
 48                 "Name,Family,L2CacheSize,Manufacturer,MaxClockSpeed,ProcessorId,LoadPercentage");
 49         }
 50 
 51         public JArray GetPhysicalMemoryInfo()
 52         {
 53             JArray jArrayAvailable = GetInfoFromWMI("Win32_PerfRawData_PerfOS_Memory",
 54                 "AvailableMBytes");
 55             JArray jArrayTotal = GetInfoFromWMI("Win32_LogicalMemoryConfiguration",
 56                 "TotalPhysicalMemory");
 57             JArray result=new JArray();
 58             result.Add(jArrayAvailable);
 59             result.Add(jArrayTotal);
 60             return result;
 61         } 
 62         /// <summary>
 63         /// 通过wmi查找相关信息
 64         /// </summary>
 65         /// <param name="name">系统配置的名称</param>
 66         /// <param name="fields">需要查询的字段,多个字段用","分隔</param>
 67         /// <returns></returns>
 68         public JArray GetInfoFromWMI(String name, String fields)
 69         {
 70             JArray jarray = new JArray();
 71             try
 72             {
 73                 ManagementObjectCollection mocObj =
 74                     new ManagementObjectSearcher(Ms, 
 75                         new ObjectQuery(       
 76                             String.Format("Select {0} From {1}", fields, name))).
 77                     Get();
 78                 JObject jObj;
 79                 foreach (ManagementObject mo in mocObj)
 80                 {
 81                     jObj = new JObject();
 82                     foreach (String fieldName in fields.Split(','))
 83                     {
 84                         if (mo[fieldName] != null)
 85                             jObj.Add(new JProperty(fieldName, mo[fieldName]));
 86                     }
 87                     jarray.Add(jObj);
 88                 }
 89             }
 90             catch
 91             {
 92                 jarray = null;
 93             }
 94             return jarray;
 95         }
 96 
 97         private enum Unit { B, KB, MB, GB, ER }
 98         private string FormatBytes(double bytes)
 99         {
100             int unit = 0;
101             while (bytes > 1024)
102             {
103                 bytes /= 1024;
104                 ++unit;
105             }
106             return string.Format("{0} {1}", 
107                 bytes.ToString("F"), 
108                 ((Unit)unit).ToString());
109         }
110     }
111 }

 程序员的基础教程:菜鸟程序员

posted on 2015-04-26 18:06  itprobie-菜鸟程序员  阅读(536)  评论(0编辑  收藏  举报