标 题: 主机性能监控之wmi 获取系统信息及内存性能信息
作 者: itdef
链 接: http://www.cnblogs.com/itdef/p/3990240.html

欢迎转帖 请保持文本完整并注明出处

这里参考了http://www.cnblogs.com/lxcsmallcity/archive/2009/10/11/1580803.html

使用了PYTHON 和 vc 进行了调用WMI的代码编写

通过搜索和查看MSDN 可以找到WMI的基本用法

其实主要是WMI接口的初始化 使用 释放的过程

然后就是查找MSDN各个Win32_OperatingSystem  Win32_Process等的结构的说明 进行相关信息的获取

这里先介绍Win32_OperatingSystem的用法

Win32_OperatingSystem结构(来自MSDN)

class Win32_OperatingSystem : CIM_OperatingSystem
{
string BootDevice;
string BuildNumber;
string BuildType;
string Caption;
string CodeSet;
string CountryCode;
string CreationClassName;
string CSCreationClassName;
string CSDVersion;
string CSName;
sint16 CurrentTimeZone;
boolean DataExecutionPrevention_Available;
boolean DataExecutionPrevention_32BitApplications;
boolean DataExecutionPrevention_Drivers;
uint8 DataExecutionPrevention_SupportPolicy;
boolean Debug;
string Description;
boolean Distributed;
uint32 EncryptionLevel;
uint8 ForegroundApplicationBoost;
uint64 FreePhysicalMemory;
uint64 FreeSpaceInPagingFiles;
uint64 FreeVirtualMemory;
datetime InstallDate;
uint32 LargeSystemCache;
datetime LastBootUpTime;
datetime LocalDateTime;
string Locale;
string Manufacturer;
uint32 MaxNumberOfProcesses;
uint64 MaxProcessMemorySize;
string MUILanguages[];
string Name;
uint32 NumberOfLicensedUsers;
uint32 NumberOfProcesses;
uint32 NumberOfUsers;
uint32 OperatingSystemSKU;
string Organization;
string OSArchitecture;
uint32 OSLanguage;
uint32 OSProductSuite;
uint16 OSType;
string OtherTypeDescription;
Boolean PAEEnabled;
string PlusProductID;
string PlusVersionNumber;
boolean Primary;
uint32 ProductType;
uint8 QuantumLength;
uint8 QuantumType;
string RegisteredUser;
string SerialNumber;
uint16 ServicePackMajorVersion;
uint16 ServicePackMinorVersion;
uint64 SizeStoredInPagingFiles;
string Status;
uint32 SuiteMask;
string SystemDevice;
string SystemDirectory;
string SystemDrive;
uint64 TotalSwapSpaceSize;
uint64 TotalVirtualMemorySize;
uint64 TotalVisibleMemorySize;
string Version;
string WindowsDirectory;
};

初始化WMI 连接 查询其中各个元素就可以获取信息

代码如下:

 // WMI_Sample.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <atlstr.h>
#include <comutil.h>
#include <comdef.h>
#include <Wbemidl.h> using namespace std; #pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "comsuppw.lib") //===================================================
class CMyWMI{
IWbemLocator *pLoc_;
IWbemServices *pSvc_; void GetInfo(WCHAR* wszQueryInfo,IWbemClassObject *pclsObj);
public:
CMyWMI():pLoc_(NULL),pSvc_(NULL){}
~CMyWMI(){ ClearWMI(); }
bool InitWMI();
bool ClearWMI();
bool QuerySystemInfo(); }; void CMyWMI::GetInfo(WCHAR* wszQueryInfo,IWbemClassObject *pclsObj)
{
if(wszQueryInfo == NULL || NULL == pclsObj)
return;
VARIANT vtProp;
char* lpszText = NULL; HRESULT hr = pclsObj->Get(wszQueryInfo, , &vtProp, , );
lpszText = _com_util::ConvertBSTRToString(V_BSTR(&vtProp));
printf_s("%s\n", lpszText); delete[] lpszText;
VariantClear(&vtProp);
} bool CMyWMI::QuerySystemInfo()
{
HRESULT hres; //定义COM调用的返回
IEnumWbemClassObject* pEnumerator = NULL;
bool bRet = false; try{
hres = pSvc_->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_OperatingSystem"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator); if (FAILED(hres))
{
throw exception("ExecQuery() error.");
} while (pEnumerator)
{
IWbemClassObject *pclsObj;
ULONG uReturn = ; HRESULT hr = pEnumerator->Next(WBEM_INFINITE, ,
&pclsObj, &uReturn);
if( == uReturn)
{
break;
} GetInfo(L"BootDevice",pclsObj);
GetInfo(L"Caption",pclsObj);
GetInfo(L"Manufacturer",pclsObj);
GetInfo(L"CSName",pclsObj);
GetInfo(L"WindowsDirectory",pclsObj);
GetInfo(L"SystemDirectory",pclsObj);
GetInfo(L"TotalVisibleMemorySize",pclsObj);
GetInfo(L"FreePhysicalMemory",pclsObj); pclsObj->Release();
} }catch(exception& e)
{
cout << e.what() << endl;
if(pEnumerator != NULL)
{
pEnumerator->Release();
pEnumerator = NULL;
}
return bRet;
} if(pEnumerator != NULL)
{
pEnumerator->Release();
pEnumerator = NULL;
} bRet = true;
return bRet;
} bool CMyWMI::ClearWMI()
{
bool bRet = false; if( NULL != pSvc_)
pSvc_->Release(); if(pLoc_ != NULL )
pLoc_->Release(); CoUninitialize();
bRet = true;
return bRet;
} bool CMyWMI::InitWMI()
{
HRESULT hres; //定义COM调用的返回
bool bRet = false; try{
hres = CoInitializeEx(, COINIT_MULTITHREADED);
if (FAILED(hres))
{
throw exception("CoInitializeEx() error.");
} hres = CoInitializeSecurity(
NULL,
-, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
); if (FAILED(hres))
{
throw exception("CoInitializeEx() error.");
} hres = CoCreateInstance(
CLSID_WbemLocator,
,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc_); if (FAILED(hres))
{
throw exception("CoCreateInstance() error.");
} // to make IWbemServices calls.
hres = pLoc_->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
, // Locale. NULL indicates current
NULL, // Security flags.
, // Authority (e.g. Kerberos)
, // Context object
&pSvc_ // pointer to IWbemServices proxy
); if (FAILED(hres))
{
throw exception("ConnectServer() error.");
} hres = CoSetProxyBlanket(
pSvc_, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
); if (FAILED(hres))
{
throw exception("CoSetProxyBlanket() error.");
} }catch(exception& e)
{
cout << e.what() << endl;
return bRet;
} bRet = true;
return bRet;
} //=========================================================
int _tmain(int argc, _TCHAR* argv[])
{
CMyWMI myWMI; myWMI.InitWMI();
myWMI.QuerySystemInfo(); return ;
}

python 代码:

#!/usr/bin/env python
# -*- coding: cp936 -*- import wmi
import os
import sys
import platform
import time def sys_version():
c = wmi.WMI ()
#获取操作系统版本
for sys in c.Win32_OperatingSystem():
print "Version:%s" % sys.Caption.encode("UTF8"),"Vernum:%s" % sys.BuildNumber
print sys.OSArchitecture.encode("UTF8")#系统是32位还是64位的
print sys.NumberOfProcesses #当前系统运行的进程总数
print sys.WindowsDirectory
print sys.SystemDirectory
print "system total memory: " ,sys.TotalVisibleMemorySize
print "system free memory: " ,sys.FreePhysicalMemory def main():
sys_version() if __name__ == '__main__':
main()
#print platform.system()

效果图

主机性能监控之wmi 获取系统信息及内存性能信息的更多相关文章

  1. 主机性能监控之wmi 获取磁盘信息

    标 题: 主机性能监控之wmi 获取磁盘信息作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990541.html 欢迎转帖 请保持文本完整并注明出处 仅 ...

  2. 主机性能监控之wmi 获取进程信息

    标 题: 主机性能监控之wmi 获取进程信息作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990499.html 欢迎转帖 请保持文本完整并注明出处 仅 ...

  3. dotnet 通过 WMI 获取系统信息

    本文告诉大家如何通过 WMI 获取系统信息 通过 Win32_OperatingSystem 可以获取系统信息 var mc = "Win32_OperatingSystem"; ...

  4. 获取本机内存使用信息、DataTable占用内存空间

    相当于windows系统中的任务管理器,功能是通过系统的API实现的本机的监视,代码如下 using System;using System.Collections.Generic;using Sys ...

  5. PowerShell 通过 WMI 获取系统信息

    本文告诉大家如何通过 WMI 使用 Win32_OperatingSystem 获取设备厂商 通过下面代码可以获取 系统版本和系统是专业版还是教育版 Get-WmiObject Win32_Opera ...

  6. PHP怎么获取系统信息和服务器详细信息

    https://zhidao.baidu.com/question/1435990326608475859.html 获取系统类型及版本号: php_uname() (例:Windows NT COM ...

  7. GetSystemInfo 和 GlobalMemoryStatus获取系统信息,内存信息

    // GetSystemInfo.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #in ...

  8. 获取CPU和内存呢信息

    #include <stdio.h> #include <stdlib.h> #include <winsock.h> #pragma comment(lib, & ...

  9. Citrix 服务器虚拟化之十三 Xenserver虚拟机内存优化与性能监控

    Citrix 服务器虚拟化之十三   Xenserver虚拟机内存优化与性能监控 XenServer的DMC通过自动调节运行的虚拟机的内存,每个VM分配给指定的最小和最大内存值之间,以保证性能并允许每 ...

随机推荐

  1. spi、iic、can高速传输速度与选择

    uart: 无限制,常用9600.115200bps等保证双方通信速度相同. iic: 通讯速率400Kbps can: 一般为1Mbps SPI: 通信速率 fosc/4其传输速度可达几Mb/s 缺 ...

  2. Kubernetes Kubelet安全认证连接Apiserver

    Kubelet使用安全认证连接Apiserver,可以用Token或证书连接.配置步骤如下. 1,生成Token命令 head -c /dev/urandom | od -An -t x | tr - ...

  3. spring boot项目使用外部tomcat启动失败总结

    1. springboot的tomcat使用外部提供的. dependency> <groupId>org.springframework.boot</groupId> ...

  4. json null

    { "ResourceId": 0, "JsonKey": "Account", "GroupId": 21, &quo ...

  5. exchang2010OWA主界面添加修改密码选项

    原文链接:http://www.mamicode.com/info-detail-1444660.html exchange邮箱用户可以登录OWA修改密码,当AD用户密码过期或者重置密码勾选了“用户下 ...

  6. ActiveMQ(1)---初识ActiveMQ

    消息中间件的初步认识 什么是消息中间件? 消息中间件是值利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,可以在分布式架构下扩展进 ...

  7. C# Excel添加超链接

    操作当前单元格(关键代码就两行) Range range = (Range)ExSheet.Cells[i + 2, j + 1];                                   ...

  8. 【转】修改mysql数据库的用户名和密码

    修改mysql数据库的用户名和密码 更改密码 mysql -u root -p Enter password:*** mysql>use mysql; 选择数据库 Database change ...

  9. windows系统下将nginx作为系统服务启动

    1. 准备工作 下载安装nginx,并记住安装目录 官网下载 下载winsw,下载地址 2. winsw设置 将winsw可执行程序复制到nginx安装目录下,并重命名为nginx-service 新 ...

  10. 下载安装 STS(Spring Tool Suite),推荐对应 Eclipse 版本号,适用于Windows32位(xp、2003)

    sts下载地址:https://spring.io/tools/sts/legacy 虽然sts内置了版本对应的eclipse,仍推荐使用当前环境下稳定使用的eclipse版本. Start 找到ec ...