服务器性能监控之WMI

1.WMI简介
WMI是英文WindowsManagementInstrumentation的简写,通过使用WMI,我们可以获取本地或远程服务器的性能参数和进程运行情况,以及大部分硬件信息,但前提是运行的用户要有足够的权限,如administrator组用户等。这也是做负载均衡所需要且比较方便快捷的途径。

2.使用时首先添加System.Management.dll,然后引用

usingSystem.Management;

3.定义远程访问

publicclassManagementConnectPool
...{
privatestaticSystem.Management.ConnectionOptionsConn=newConnectionOptions();
privatestaticManagementObjectSearchermos=newManagementObjectSearcher();
privatestringusername="";
privatestringpwd="";
privatestringspace="";
privatestringserver="";

publicManagementConnectPool(stringmpusername,stringmppwd,stringmpspace,stringmpserver)
...{
this.username=mpusername;
this.pwd=mppwd;
this.space=mpspace;
this.server=mpserver;
Conn.Username=mpusername;
Conn.Password=mppwd;
stringscopestring="//"+mpserver+mpspace;
System.Management.ManagementScopeMs=newManagementScope(scopestring);
Ms.Connect();
mos.Scope=Ms;
}

publicManagementObjectCollectiongetQueryResult(stringqueryString)
...{
ObjectQueryoq=newObjectQuery();
oq.QueryString=queryString;
mos.Query=oq;
ManagementObjectCollectionmoc=mos.Get();
returnmoc;
}
}

4.获取CPU,内存,网络流量等信息

publicclassMonitor
...{
privatestringusername="";
privatestringpwd="";
privatestringip="";
ManagementConnectPoolmcp;
WMSServerClassserver;


publicMonitor(stringusername,stringpwd,stringip)...{
this.username=username;
this.pwd=pwd;
this.ip=ip;
mcp=newManagementConnectPool(username,pwd,"/root/cimv2",ip);
server=newWMSServerClass();
}

WMI方式获取网卡流量#regionWMI方式获取网卡流量
privatevoidgetNetWorkFlow()
...{
ManagementObjectCollectionmoc=mcp.getQueryResult(@"select*fromWin32_NetworkAdapterwheremacaddress<>nullandmanufacturer<>'Microsoft'");
string[]list=newstring[moc.Count];
foreach(System.Management.ManagementObjectmymacinmoc)
...{
stringa=mymac["Speed"].ToString();//nullWMI未实现该属性
Console.WriteLine(a.ToString());
}
}
#endregion

WMI方式获取CPU信息#regionWMI方式获取CPU信息
privatevoidgetCpuInfo()
...{
ManagementObjectCollectionmoc=mcp.getQueryResult("select*fromWin32_Processor");
string[]list=newstring[moc.Count];
inti=0;
foreach(ManagementObjectmoinmoc)
...{
stringtotal=mo.GetPropertyValue("LoadPercentage").ToString();
list[i]=total;
i++;
}
}
#endregion

WMI方式获取内存使用率#regionWMI方式获取内存使用率
publicstringgetMemoryUsage()
...{
ManagementObjectCollectionmoc=mcp.getQueryResult("select*fromWin32_LogicalMemoryConfiguration");
inttotalm=1;intavilablem=1;
foreach(ManagementObjectmoinmoc)
...{
stringtotal=mo.GetPropertyValue("TotalPhysicalMemory").ToString();
totalm=int.Parse(total);
}
moc=mcp.getQueryResult("select*fromWin32_PerfRawData_PerfOS_Memory");
foreach(ManagementObjectmoinmoc)
...{
stringavilable=mo.GetPropertyValue("AvailableKBytes").ToString();
avilablem=int.Parse(avilable);
}
intusedm=totalm-avilablem;
doublememoryusage=(double)usedm*(double)100/(double)totalm;
returnmemoryusage.ToString();
}
#endregion
}

5.获取本地机器信息(WEB)

1.如何用WMI获得指定磁盘的容量#region1.如何用WMI获得指定磁盘的容量
privatestringDriveType(stringtype)
...{
stringrtntype="";
switch(type)
...{
case"1":
rtntype="NotType";
break;
case"2":
rtntype="Floppydisk";
break;
case"3":
rtntype="Harddisk";
break;
case"4":
rtntype="Removabledriveornetworkdrive";
break;
case"5":
rtntype="CD-ROM";
break;
case"6":
rtntype="RAMdisk";
break;
default:
break;
}
returnrtntype;
}

privatevoidButton1_Click(objectsender,System.EventArgse)
...{
SelectQueryquery=newSelectQuery("Select*FromWin32_LogicalDisk");
ManagementObjectSearchersearcher=newManagementObjectSearcher(query);
foreach(ManagementBaseObjectdiskinsearcher.Get())
...{
Response.Write(disk["Name"]+""+DriveType(disk["DriveType"].ToString())+""+disk["VolumeName"]+"<br>");
}
}
#endregion

2.如何用WMI获得指定磁盘的容量#region2.如何用WMI获得指定磁盘的容量
privatevoidButton2_Click(objectsender,System.EventArgse)
...{
ManagementObjectdisk=newManagementObject("win32_logicaldisk.deviceid="c:"");
disk.Get();
Response.Write("LogicalDiskSize="+disk["Size"]+"bytes");
}
#endregion

3.如何列出机器中所有的共享资源#region3.如何列出机器中所有的共享资源
privatevoidButton3_Click(objectsender,System.EventArgse)
...{
ManagementObjectSearchersearcher=newManagementObjectSearcher("SELECT*FROMWin32_share");
foreach(ManagementObjectshareinsearcher.Get())
...{
Response.Write(share.GetText(TextFormat.Mof));
}
}
#endregion

4.怎样写程控制让系统中的某个文件夹共享或取消共享#region4.怎样写程控制让系统中的某个文件夹共享或取消共享
privatevoidButton4_Click(objectsender,System.EventArgse)
...{
/**//*首先,这需要以有相应权限的用户登录系统才行

object[]obj={"C:/Temp","我的共享",0,10,"DotNet实现的共享",""};
改为
object[]obj={"C:/Temp","我的共享",0,null,"DotNet实现的共享",""};
就可以实现授权给最多用户了。
*/
ManagementClass_class=newManagementClass(newManagementPath("Win32_Share"));
object[]obj=...{"C:/Temp","我的共享",0,10,"DotNet实现的共享",""};
_class.InvokeMethod("create",obj);
}
#endregion

5.如何获得系统服务的运行状态#region5.如何获得系统服务的运行状态
privatevoidButton5_Click(objectsender,System.EventArgse)
...{
string[]lvData=newstring[4];
ManagementObjectSearchersearcher=newManagementObjectSearcher("SELECT*FROMWin32_Service");
foreach(ManagementObjectmoinsearcher.Get())
...{
lvData[0]=mo["Name"].ToString();
lvData[1]=mo["StartMode"].ToString();
if(mo["Started"].Equals(true))
lvData[2]="Started";
else
lvData[2]="Stop";
lvData[3]=mo["StartName"].ToString();
Response.Write(lvData[0]+lvData[1]+lvData[2]+lvData[3]);
}
}
#endregion

6.通过WMI修改IP,而实现不用重新启动#region6.通过WMI修改IP,而实现不用重新启动
privatevoidButton6_Click(objectsender,System.EventArgse)
...{
ReportIP();
//SwitchToDHCP();
SwitchToprivate();
Thread.Sleep(5000);
ReportIP();
Response.Write("end.");
}

privatevoidSwitchToDHCP()
...{
ManagementBaseObjectinPar=null;
ManagementBaseObjectoutPar=null;
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
if(!(bool)mo["IPEnabled"])
continue;

inPar=mo.GetMethodParameters("EnableDHCP");
outPar=mo.InvokeMethod("EnableDHCP",inPar,null);
break;
}
}

privatevoidSwitchToprivate()
...{
ManagementBaseObjectinPar=null;
ManagementBaseObjectoutPar=null;
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
if(!(bool)mo["IPEnabled"])
continue;

inPar=mo.GetMethodParameters("Enableprivate");
inPar["IPAddress"]=newstring[]...{"192.168.1.1"};
inPar["SubnetMask"]=newstring[]...{"255.255.255.0"};
outPar=mo.InvokeMethod("Enableprivate",inPar,null);
break;
}
}

privatevoidReportIP()
...{
Response.Write("******CurrentIPaddresses:");
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
if(!(bool)mo["IPEnabled"])
continue;

stringstr="{0} SVC:'{1}'MAC:[{2}]";
str=string.Format(mo["Caption"].ToString(),mo["ServiceName"].ToString(),mo["MACAddress"].ToString());

Response.Write(str);

string[]addresses=(string[])mo["IPAddress"];
string[]subnets=(string[])mo["IPSubnet"];

Response.Write("Addresses:");
foreach(stringsadinaddresses)
Response.Write(sad+"<br>");

Response.Write("Subnets:");
foreach(stringsubinsubnets)
Response.Write(sub+"<br>");
}
}
#endregion

7.如何利用WMI远程重启远程计算机#region7.如何利用WMI远程重启远程计算机
privatevoidButton7_Click(objectsender,System.EventArgse)
...{
Response.Write("ComputerdetailsretrievedusingWindowsManagementInstrumentation(WMI)");
Response.Write("mailto:singlepine@hotmail.com");
Response.Write("=========================================================================");
//连接远程计算机
ConnectionOptionsco=newConnectionOptions();
co.Username="john";
co.Password="john";
System.Management.ManagementScopems=newSystem.Management.ManagementScope("//192.168.1.2/root/cimv2",co);
//查询远程计算机
System.Management.ObjectQueryoq=newSystem.Management.ObjectQuery("SELECT*FROMWin32_OperatingSystem");

ManagementObjectSearcherquery1=newManagementObjectSearcher(ms,oq);
ManagementObjectCollectionqueryCollection1=query1.Get();
foreach(ManagementObjectmoinqueryCollection1)
...{
string[]ss=...{""};
mo.InvokeMethod("Reboot",ss);
Response.Write(mo.ToString());
}
}
#endregion

8.利用WMI创建一个新的进程#region8.利用WMI创建一个新的进程
privatevoidButton8_Click(objectsender,System.EventArgse)
...{
//Gettheobjectonwhichthemethodwillbeinvoked
ManagementClassprocessClass=newManagementClass("Win32_Process");

//Getaninputparametersobjectforthismethod
ManagementBaseObjectinParams=processClass.GetMethodParameters("Create");

//Fillininputparametervalues
inParams["CommandLine"]="calc.exe";

//Executethemethod
ManagementBaseObjectoutParams=processClass.InvokeMethod("Create",inParams,null);

//Displayresults
//Note:Thereturncodeofthemethodisprovidedinthe"returnvalue"propertyoftheoutParamsobject
Response.Write("Creationofcalculatorprocessreturned:"+outParams["returnvalue"]);
Response.Write("ProcessID:"+outParams["processId"]);

}
#endregion

9.如何通过WMI终止一个进程#region9.如何通过WMI终止一个进程
privatevoidButton9_Click(objectsender,System.EventArgse)
...{
ManagementObjectservice=
newManagementObject("win32_service="winmgmt"");
InvokeMethodOptionsoptions=newInvokeMethodOptions();
options.Timeout=newTimeSpan(0,0,0,5);

ManagementBaseObjectoutParams=service.InvokeMethod("StopService",null,options);

Response.Write("ReturnStatus="+outParams["Returnvalue"]);
}
#endregion

10.如何用WMI来获取远程机器的目录以及文件#region10.如何用WMI来获取远程机器的目录以及文件
privatevoidButton10_Click(objectsender,System.EventArgse)
...{
ManagementObjectdisk=newManagementObject(

"win32_logicaldisk.deviceid="c:"");

disk.Get();

Response.Write("LogicalDiskSize="+disk["Size"]+"bytes");
}
#endregion

11.网卡的MAC地址#region11.网卡的MAC地址
privatevoidButton11_Click(objectsender,System.EventArgse)
...{
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
if((bool)mo["IPEnabled"]==true)
Response.Write("MACaddress"+mo["MacAddress"].ToString()+"<br>");
mo.Dispose();
}
}
#endregion

12.CPU的系列号#region12.CPU的系列号
privatevoidButton12_Click(objectsender,System.EventArgse)
...{
stringcpuInfo="";//cpu序列号
ManagementClasscimobject=newManagementClass("Win32_Processor");
ManagementObjectCollectionmoc=cimobject.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
cpuInfo=mo.Properties["ProcessorId"].Value.ToString();
Response.Write(cpuInfo);
}
}
#endregion

13.主板的系列号#region13.主板的系列号
privatevoidButton13_Click(objectsender,System.EventArgse)
...{
ManagementObjectSearchersearcher=newManagementObjectSearcher("SELECT*FROMWin32_BaseBoard");
foreach(ManagementObjectshareinsearcher.Get())
...{
Response.Write("主板制造商:"+share["Manufacturer"].ToString());
Response.Write("型号:"+share["Product"].ToString());
Response.Write("序列号:"+share["SerialNumber"].ToString());
}
}
#endregion

14.获取硬盘ID#region14.获取硬盘ID
privatevoidButton14_Click(objectsender,System.EventArgse)
...{
StringHDid;
ManagementClasscimobject=newManagementClass("Win32_DiskDrive");
ManagementObjectCollectionmoc=cimobject.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
HDid=(string)mo.Properties["Model"].Value;
Response.Write(HDid);
}
}
#endregion

15.获取本机的用户列表#region15.获取本机的用户列表
privatevoidButton15_Click(objectsender,System.EventArgse)
...{
SelectQueryquery=newSelectQuery("SELECT*FROMWin32_UserAccount");
ManagementObjectSearchersearcher=newManagementObjectSearcher(query);
foreach(ManagementObjectosinsearcher.Get())
...{
Response.Write(os["Name"]);
}
}
#endregion
}

通过上面介绍的方法,可以很轻松的获取远程或本地机器的性能,进程和硬件信息等。另外:WMI也可以通过使用Vbscript等脚本调用。

说明:其中部分资源来自网友小山的blog。但不够详细说明WMI在服务器性能方面的能力!

1.WMI简介
WMI是英文WindowsManagementInstrumentation的简写,通过使用WMI,我们可以获取本地或远程服务器的性能参数和进程运行情况,以及大部分硬件信息,但前提是运行的用户要有足够的权限,如administrator组用户等。这也是做负载均衡所需要且比较方便快捷的途径。

2.使用时首先添加System.Management.dll,然后引用

usingSystem.Management;

3.定义远程访问

publicclassManagementConnectPool
...{
privatestaticSystem.Management.ConnectionOptionsConn=newConnectionOptions();
privatestaticManagementObjectSearchermos=newManagementObjectSearcher();
privatestringusername="";
privatestringpwd="";
privatestringspace="";
privatestringserver="";

publicManagementConnectPool(stringmpusername,stringmppwd,stringmpspace,stringmpserver)
...{
this.username=mpusername;
this.pwd=mppwd;
this.space=mpspace;
this.server=mpserver;
Conn.Username=mpusername;
Conn.Password=mppwd;
stringscopestring="//"+mpserver+mpspace;
System.Management.ManagementScopeMs=newManagementScope(scopestring);
Ms.Connect();
mos.Scope=Ms;
}

publicManagementObjectCollectiongetQueryResult(stringqueryString)
...{
ObjectQueryoq=newObjectQuery();
oq.QueryString=queryString;
mos.Query=oq;
ManagementObjectCollectionmoc=mos.Get();
returnmoc;
}
}

4.获取CPU,内存,网络流量等信息

publicclassMonitor
...{
privatestringusername="";
privatestringpwd="";
privatestringip="";
ManagementConnectPoolmcp;
WMSServerClassserver;


publicMonitor(stringusername,stringpwd,stringip)...{
this.username=username;
this.pwd=pwd;
this.ip=ip;
mcp=newManagementConnectPool(username,pwd,"/root/cimv2",ip);
server=newWMSServerClass();
}

WMI方式获取网卡流量#regionWMI方式获取网卡流量
privatevoidgetNetWorkFlow()
...{
ManagementObjectCollectionmoc=mcp.getQueryResult(@"select*fromWin32_NetworkAdapterwheremacaddress<>nullandmanufacturer<>'Microsoft'");
string[]list=newstring[moc.Count];
foreach(System.Management.ManagementObjectmymacinmoc)
...{
stringa=mymac["Speed"].ToString();//nullWMI未实现该属性
Console.WriteLine(a.ToString());
}
}
#endregion

WMI方式获取CPU信息#regionWMI方式获取CPU信息
privatevoidgetCpuInfo()
...{
ManagementObjectCollectionmoc=mcp.getQueryResult("select*fromWin32_Processor");
string[]list=newstring[moc.Count];
inti=0;
foreach(ManagementObjectmoinmoc)
...{
stringtotal=mo.GetPropertyValue("LoadPercentage").ToString();
list[i]=total;
i++;
}
}
#endregion

WMI方式获取内存使用率#regionWMI方式获取内存使用率
publicstringgetMemoryUsage()
...{
ManagementObjectCollectionmoc=mcp.getQueryResult("select*fromWin32_LogicalMemoryConfiguration");
inttotalm=1;intavilablem=1;
foreach(ManagementObjectmoinmoc)
...{
stringtotal=mo.GetPropertyValue("TotalPhysicalMemory").ToString();
totalm=int.Parse(total);
}
moc=mcp.getQueryResult("select*fromWin32_PerfRawData_PerfOS_Memory");
foreach(ManagementObjectmoinmoc)
...{
stringavilable=mo.GetPropertyValue("AvailableKBytes").ToString();
avilablem=int.Parse(avilable);
}
intusedm=totalm-avilablem;
doublememoryusage=(double)usedm*(double)100/(double)totalm;
returnmemoryusage.ToString();
}
#endregion
}

5.获取本地机器信息(WEB)

1.如何用WMI获得指定磁盘的容量#region1.如何用WMI获得指定磁盘的容量
privatestringDriveType(stringtype)
...{
stringrtntype="";
switch(type)
...{
case"1":
rtntype="NotType";
break;
case"2":
rtntype="Floppydisk";
break;
case"3":
rtntype="Harddisk";
break;
case"4":
rtntype="Removabledriveornetworkdrive";
break;
case"5":
rtntype="CD-ROM";
break;
case"6":
rtntype="RAMdisk";
break;
default:
break;
}
returnrtntype;
}

privatevoidButton1_Click(objectsender,System.EventArgse)
...{
SelectQueryquery=newSelectQuery("Select*FromWin32_LogicalDisk");
ManagementObjectSearchersearcher=newManagementObjectSearcher(query);
foreach(ManagementBaseObjectdiskinsearcher.Get())
...{
Response.Write(disk["Name"]+""+DriveType(disk["DriveType"].ToString())+""+disk["VolumeName"]+"<br>");
}
}
#endregion

2.如何用WMI获得指定磁盘的容量#region2.如何用WMI获得指定磁盘的容量
privatevoidButton2_Click(objectsender,System.EventArgse)
...{
ManagementObjectdisk=newManagementObject("win32_logicaldisk.deviceid="c:"");
disk.Get();
Response.Write("LogicalDiskSize="+disk["Size"]+"bytes");
}
#endregion

3.如何列出机器中所有的共享资源#region3.如何列出机器中所有的共享资源
privatevoidButton3_Click(objectsender,System.EventArgse)
...{
ManagementObjectSearchersearcher=newManagementObjectSearcher("SELECT*FROMWin32_share");
foreach(ManagementObjectshareinsearcher.Get())
...{
Response.Write(share.GetText(TextFormat.Mof));
}
}
#endregion

4.怎样写程控制让系统中的某个文件夹共享或取消共享#region4.怎样写程控制让系统中的某个文件夹共享或取消共享
privatevoidButton4_Click(objectsender,System.EventArgse)
...{
/**//*首先,这需要以有相应权限的用户登录系统才行

object[]obj={"C:/Temp","我的共享",0,10,"DotNet实现的共享",""};
改为
object[]obj={"C:/Temp","我的共享",0,null,"DotNet实现的共享",""};
就可以实现授权给最多用户了。
*/
ManagementClass_class=newManagementClass(newManagementPath("Win32_Share"));
object[]obj=...{"C:/Temp","我的共享",0,10,"DotNet实现的共享",""};
_class.InvokeMethod("create",obj);
}
#endregion

5.如何获得系统服务的运行状态#region5.如何获得系统服务的运行状态
privatevoidButton5_Click(objectsender,System.EventArgse)
...{
string[]lvData=newstring[4];
ManagementObjectSearchersearcher=newManagementObjectSearcher("SELECT*FROMWin32_Service");
foreach(ManagementObjectmoinsearcher.Get())
...{
lvData[0]=mo["Name"].ToString();
lvData[1]=mo["StartMode"].ToString();
if(mo["Started"].Equals(true))
lvData[2]="Started";
else
lvData[2]="Stop";
lvData[3]=mo["StartName"].ToString();
Response.Write(lvData[0]+lvData[1]+lvData[2]+lvData[3]);
}
}
#endregion

6.通过WMI修改IP,而实现不用重新启动#region6.通过WMI修改IP,而实现不用重新启动
privatevoidButton6_Click(objectsender,System.EventArgse)
...{
ReportIP();
//SwitchToDHCP();
SwitchToprivate();
Thread.Sleep(5000);
ReportIP();
Response.Write("end.");
}

privatevoidSwitchToDHCP()
...{
ManagementBaseObjectinPar=null;
ManagementBaseObjectoutPar=null;
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
if(!(bool)mo["IPEnabled"])
continue;

inPar=mo.GetMethodParameters("EnableDHCP");
outPar=mo.InvokeMethod("EnableDHCP",inPar,null);
break;
}
}

privatevoidSwitchToprivate()
...{
ManagementBaseObjectinPar=null;
ManagementBaseObjectoutPar=null;
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
if(!(bool)mo["IPEnabled"])
continue;

inPar=mo.GetMethodParameters("Enableprivate");
inPar["IPAddress"]=newstring[]...{"192.168.1.1"};
inPar["SubnetMask"]=newstring[]...{"255.255.255.0"};
outPar=mo.InvokeMethod("Enableprivate",inPar,null);
break;
}
}

privatevoidReportIP()
...{
Response.Write("******CurrentIPaddresses:");
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
if(!(bool)mo["IPEnabled"])
continue;

stringstr="{0} SVC:'{1}'MAC:[{2}]";
str=string.Format(mo["Caption"].ToString(),mo["ServiceName"].ToString(),mo["MACAddress"].ToString());

Response.Write(str);

string[]addresses=(string[])mo["IPAddress"];
string[]subnets=(string[])mo["IPSubnet"];

Response.Write("Addresses:");
foreach(stringsadinaddresses)
Response.Write(sad+"<br>");

Response.Write("Subnets:");
foreach(stringsubinsubnets)
Response.Write(sub+"<br>");
}
}
#endregion

7.如何利用WMI远程重启远程计算机#region7.如何利用WMI远程重启远程计算机
privatevoidButton7_Click(objectsender,System.EventArgse)
...{
Response.Write("ComputerdetailsretrievedusingWindowsManagementInstrumentation(WMI)");
Response.Write("mailto:singlepine@hotmail.com");
Response.Write("=========================================================================");
//连接远程计算机
ConnectionOptionsco=newConnectionOptions();
co.Username="john";
co.Password="john";
System.Management.ManagementScopems=newSystem.Management.ManagementScope("//192.168.1.2/root/cimv2",co);
//查询远程计算机
System.Management.ObjectQueryoq=newSystem.Management.ObjectQuery("SELECT*FROMWin32_OperatingSystem");

ManagementObjectSearcherquery1=newManagementObjectSearcher(ms,oq);
ManagementObjectCollectionqueryCollection1=query1.Get();
foreach(ManagementObjectmoinqueryCollection1)
...{
string[]ss=...{""};
mo.InvokeMethod("Reboot",ss);
Response.Write(mo.ToString());
}
}
#endregion

8.利用WMI创建一个新的进程#region8.利用WMI创建一个新的进程
privatevoidButton8_Click(objectsender,System.EventArgse)
...{
//Gettheobjectonwhichthemethodwillbeinvoked
ManagementClassprocessClass=newManagementClass("Win32_Process");

//Getaninputparametersobjectforthismethod
ManagementBaseObjectinParams=processClass.GetMethodParameters("Create");

//Fillininputparametervalues
inParams["CommandLine"]="calc.exe";

//Executethemethod
ManagementBaseObjectoutParams=processClass.InvokeMethod("Create",inParams,null);

//Displayresults
//Note:Thereturncodeofthemethodisprovidedinthe"returnvalue"propertyoftheoutParamsobject
Response.Write("Creationofcalculatorprocessreturned:"+outParams["returnvalue"]);
Response.Write("ProcessID:"+outParams["processId"]);

}
#endregion

9.如何通过WMI终止一个进程#region9.如何通过WMI终止一个进程
privatevoidButton9_Click(objectsender,System.EventArgse)
...{
ManagementObjectservice=
newManagementObject("win32_service="winmgmt"");
InvokeMethodOptionsoptions=newInvokeMethodOptions();
options.Timeout=newTimeSpan(0,0,0,5);

ManagementBaseObjectoutParams=service.InvokeMethod("StopService",null,options);

Response.Write("ReturnStatus="+outParams["Returnvalue"]);
}
#endregion

10.如何用WMI来获取远程机器的目录以及文件#region10.如何用WMI来获取远程机器的目录以及文件
privatevoidButton10_Click(objectsender,System.EventArgse)
...{
ManagementObjectdisk=newManagementObject(

"win32_logicaldisk.deviceid="c:"");

disk.Get();

Response.Write("LogicalDiskSize="+disk["Size"]+"bytes");
}
#endregion

11.网卡的MAC地址#region11.网卡的MAC地址
privatevoidButton11_Click(objectsender,System.EventArgse)
...{
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
if((bool)mo["IPEnabled"]==true)
Response.Write("MACaddress"+mo["MacAddress"].ToString()+"<br>");
mo.Dispose();
}
}
#endregion

12.CPU的系列号#region12.CPU的系列号
privatevoidButton12_Click(objectsender,System.EventArgse)
...{
stringcpuInfo="";//cpu序列号
ManagementClasscimobject=newManagementClass("Win32_Processor");
ManagementObjectCollectionmoc=cimobject.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
cpuInfo=mo.Properties["ProcessorId"].Value.ToString();
Response.Write(cpuInfo);
}
}
#endregion

13.主板的系列号#region13.主板的系列号
privatevoidButton13_Click(objectsender,System.EventArgse)
...{
ManagementObjectSearchersearcher=newManagementObjectSearcher("SELECT*FROMWin32_BaseBoard");
foreach(ManagementObjectshareinsearcher.Get())
...{
Response.Write("主板制造商:"+share["Manufacturer"].ToString());
Response.Write("型号:"+share["Product"].ToString());
Response.Write("序列号:"+share["SerialNumber"].ToString());
}
}
#endregion

14.获取硬盘ID#region14.获取硬盘ID
privatevoidButton14_Click(objectsender,System.EventArgse)
...{
StringHDid;
ManagementClasscimobject=newManagementClass("Win32_DiskDrive");
ManagementObjectCollectionmoc=cimobject.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
HDid=(string)mo.Properties["Model"].Value;
Response.Write(HDid);
}
}
#endregion

15.获取本机的用户列表#region15.获取本机的用户列表
privatevoidButton15_Click(objectsender,System.EventArgse)
...{
SelectQueryquery=newSelectQuery("SELECT*FROMWin32_UserAccount");
ManagementObjectSearchersearcher=newManagementObjectSearcher(query);
foreach(ManagementObjectosinsearcher.Get())
...{
Response.Write(os["Name"]);
}
}
#endregion
}

通过上面介绍的方法,可以很轻松的获取远程或本地机器的性能,进程和硬件信息等。另外:WMI也可以通过使用Vbscript等脚本调用。

说明:其中部分资源来自网友小山的blog。但不够详细说明WMI在服务器性能方面的能力!

时间: 2024-10-25 12:36:41

服务器性能监控之WMI的相关文章

服务器性能监控之性能计数器

文章来源:http://blog.csdn.net/lhfzd2004/article/details/1722379 上一篇文章<服务器性能监控之WMI>介绍了通过远程com获取服务器性能(当然也可用于本地),那么这篇主要说说windows系统自带的性能监视功能----->performancecouonter. 打开管理工具-->性能,我们可以立即看到服务器的CPU,进程运行时间,磁盘容量等性能参数走势图.然而不仅仅是这几项,我们可以通过添加技术器来查看其他的性能指标: 如果你

利用SNMP和监控宝实现vps服务器性能监控

再我们管理服务器时候windosw系统较为直观,cpu使用多少,内存用了多少带宽等等 只要在远程桌面里 的任务管理器一目了然.但是在linux环境下就没那么轻松了,尤其服务器或者vps上运行的是大型网站,如果不能及时发现服务器性能消耗,很有可能导致网站在访问高峰期,出现卡死都不知道什么情况.那么就老鹰就介绍下如何利用SNMP加监控宝实现vps性能监控,测试平台CentOS. 1.我们需要安装一个组件 NET-SNMP 命令如下: yum install net-snmp net-snmp-dev

利用SNMP和监控宝实现linux vps 服务器性能监控

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 在我们管理服务器时候windosw系统较为直观,cpu使用多少,内存用了多少带宽等等 只要在远程桌面里 的任务管理器一目了然.但是在linux环境下就没那么轻松了,尤其服务器或者vps上运行的是大型网站,如果不能及时发现服务器性能消耗,很有可能导致网站在访问高峰期,出现卡死都不知道什么情况.今天介绍下如何利用SNMP加监控宝实现vps性能监控

linux中SNMP+监控宝实现简单的服务器性能监控

监控宝推荐使用NET-SNMP,所以本文也以此为例.   首先,我们安装NET-SNMP.    代码如下 复制代码 yum install net-snmp net-snmp-devel net-snmp-utils   接下来我们开始配置NET-SNMP.   在配置之前,我们需要先确认已关闭SELinux(CentOS系统注意),然后,先暂时停止NET-SNMP    代码如下 复制代码 service snmpd stop   然后,添加一个只读账户(如vpsaa.com),创建一个密码

5款服务器性能监控神器介绍 (smokeping、cacti、nagios、zabbix、ntop)

1. smokeping Smokeping 是rrdtool 的作者Tobi Oetiker 的作品,是用Perl 写的,主要是监视网络性能,包括常规的ping,用echoping监控www 服务器性能,监视dns 查询性能,监视ssh 性能等.底层也是rrdtool做支持,特点是画的图非常漂亮,网络丢包和延迟用颜色和阴影来表示.在企业中运用较少. 2 cacti Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具. 简单的说Cacti 就是一个PH

服务器性能监控+邮件发送

客户端代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Text; using System.Threading; using System.Management; using System.Configuratio

Prometheus——进行服务器性能监控的一件法宝

最近一直在思考如何对线上服务做深度监控.基础的服务可用性监控很简单,定期Ping即可.但是怎样才能监控服务器的一些更加关键的数据呢?比如,每一个API Point的请求次数(QPS),最大响应时间,平均响应时间等.最终我希望实现的效果是有一个Dashboard,我可以清楚地看到各种参数曲线,对服务器的运行情况了然于胸. 绘制Dashboard不难,目前提供数据可视化的工具很多,随便选一个都能满足需要.关键问题是,怎样将整个流程打通? 服务器该以怎样的形式暴露出数据? 数据怎样被收集和存储起来?

利用windows性能计数器进行服务器性能监控

using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Threading; using System.Management; using System.Runtime.InteropServices; using System.Data; using System.Data.SqlClient; using System.Directory

Linux服务器性能追踪以及服务器监控常用命令

在网站性能优化中,我们经常要检查服务器的各种指标,以便快速找到害群之马.大多情况下,我们会使用cacti.nagois或者zabbix之类的监控软件,但是这类软件安装起来比较麻烦,在一个小型服务器,我们想尽快找到问题所在,还是要使用Linux提供的一些命令. 1. 内存检查:free -m 注意,重点看的是第二行,-/+ buffers/cache行,第一个数字是实际消耗的内存,第二个是实际空闲的内存. 2. 系统进程监控 top Top命令注意看输出的第二.第三行.第二行(Tasks)表示系统