标 题: 主机性能监控之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. 减小delphi体积的方法

    1.关闭RTTI反射机制  自从Delphi2010中引入了新的RTTI反射机制后,编译出来的程序会变得很大,这是因为默认情况下 Delphi2010 给所有类都加上了反射机制.而我们的工程并不每每都 ...

  2. WINDOWS NT操作系统的注册表文件

    WINDOWS NT操作系统的注册表文件 WINDOWS NT注册表文件分为系统文件和用户文件两类. 系统设置和缺少用户 配置数据存放在系统C:\Windows\System32\config文件夹下 ...

  3. day2模块初识别

    sys_mod.py import sys print(sys.argv) #['C:/Users/Administrator/desktop/s17/day2/sys_mod.py'] 打印脚本的绝 ...

  4. MySQL SQL审核平台 inception+archer2.0(亲测)

    docker run -d --privileged -v `pwd`/archer_data:/data -p 9306:3306 --name archer --hostname archer - ...

  5. Linux背背背(1)

    目录: 1.文件目录 2.基本语法 3.centos7之后的版本中查看ip地址 4.~ 5.修改时间 ------------------------------------------------- ...

  6. std::set

      std::set 不重复key 默认less排序 代码 #include <iostream> #include <set> class Person { public: ...

  7. 用GDB调试程序(二)

    GDB的命令概貌——————— 启动gdb后,就你被带入gdb的调试环境中,就可以使用gdb的命令开始调试程序了,gdb的命令可以使用help命令来查看,如下所示: /home/hchen> g ...

  8. 字符串格式化:f-strings

    字符串格式化一般使用: {}.format 和 %s 那么python 3.6以后新加的一个功能就是: value=“zhang”f“string{value}” # 他的主要功能就是对于我们的f或F ...

  9. random的常用方式

    Python中的random模块用于生成随机数 1.random.random() #用于生成一个0~1的随机浮点数:0<=n<1.0 >>> import random ...

  10. sql server driver ODBC驱动超时