跟我从头学WMI(一),介绍WMI

介绍WMI的基础结构

一.   概览:

企业面临的挑战

不同的系统安装有不同的代理,管理着不同域

通常一个系统上有安装多种代理,每个代理都有自己的协议和数据格式。

Desktop Management Interface (DMI)was designed to report only hardware-oriented data

每一种不同的协议都有自己特有的API和数据格式

Directory Service tools use Lightweight Directory Access Protocol (LDAP) to communicate with Microsoft Active Directory.

Microsoft SQL Server. Enterprise Manager uses COM to talk to SQL.

Managing Performance Monitoring requires access to the registry.

挑战是:如何开发出一种简便易用的统一接口,方便用户的管理

 

二.   企业管理的挑战

1、WMI的目标

WMI的目标

Enterprise Manageability

WMI在操作系统中内置了管理基础结构

Enable System and Application Observation and Control

隐藏了复杂的、分裂的、多样的系统管理技术(比如win32,WDM,DMI等)

  • 一致的执行
  • 一个单一的访问点

Make Windows the Best-Managed Operating System

    WMI支持最新的微软产品,第三方厂商也开发出各种基于wmi的工具

2、 什么是WMI

使可扩展的基于架构模式的管理简易化

The model is based on the Common Information Model (CIM), which forms part of the WBEM standard owned by the Distributed Management Task Force (DMTF).

提供了丰富的事件和查询管理服务集

简化易用的WQL,是一种类SQL的语言,能够查询特殊的数据

Remote-enable

提供远程的数据能力

Scriptable

提供一个scripting api接口

WMI历史

Nt 4   1.01 

Sms 2.0 1.1

Win2000 1.5

Winxp/2003  2.0

3、 常规管理任务

查看系统状态:安装200个客户端,服务器上的共享是否正常,是否在客户计算机上有冲突的服务,是否有足够的内存和硬盘等等

监视:要清楚事件日志,定义一个最大的尺寸,要注意一个远程计算机上的特定错误,要适时地获得一些系统在备份操作时的性能数据等

注册表:要重设远程计算机的注册表,使其适应一个新的应用程序的运行

事件:知道特定的计算机刷新dhcp地址,在一台服务器上有太多的尝试登陆,当远程服务器的服务停止时发送一个邮件。

举例:

查看计算机上的共享

For each obj in _

 GetObject("WinMgmts:").instancesOf("win32_share")

 strResult = strResult & obj.name & vbcrlf

Next

Wscript.echo strResult

运行的进程

For each obj in _

 GetObject("WinMgmts:").instancesOf("win32_process")

 strResult = strResult & obj.name & vbcrlf

Next

Wscript.echo strResult

返回网卡情况

For each obj in _

 GetObject("WinMgmts:").instancesOf("win32_networkadapter")

 strResult = strResult & obj.name & vbcrlf

Next

Wscript.echo strResult

教室里的计算机的内存情况,需要编辑

Dim system(5)

system(0) = "Localhost"

system(1) = "Localhost"

system(2) = "Localhost"

system(3) = "Localhost"

system(4) = "Localhost"

For i = 0 to Ubound(system)

Set oSysSet = GetObject("WinMgmts://" & _

system(i)).instancesOf("win32_computerSystem")

For Each oSys in oSysSet

strResult = strResult & oSys.name & " has " & _

oSys.totalPhysicalmemory & " Bytes" & vbcrlf

Next

Next

Wscript.echo strResult

 

 三.   WMI的概览

1、WMI基础结构

WMI架构

WMI使用CIMCommon Information Model)来描述在windows 系统中管理的对象。

WMI可以被描述成三层

Consumers(消费者) layer

是应用程序,使得数据可以被WMI所管理。比如scriptacitvex MOM SMS

它们均使用相同的underlying 接口,基于com 对象,隐藏起复杂性,应用程序不需要直接知道系统是如何驱动数据的。

CIM

WMI的核心,%SystemRoot%\System32\WBEM\Repository 其中的cim.rep,是一个仓库,包含了所有的类定义等等信息。

Windows Management Service就像一个CIM仓库的代理人,这个角色被描述成Common Information Model Object

Manager (CIMOM).%SystemRoot%\System32\WBEM\WinMgmt.exe.

MOF

Management Object Format 用来定义在CIM中的类,实例和名称空间。

%SystemRoot%\System32\Mofcomp.exe

Providers

Providers are pieces of software that communicate directly with the managed objects by using their native application programming interfaces (APIs).

2、管理对象

管理对象:

一个管理对象可以是任何系统管理者感兴趣的item,包括deviceapplication,整个系统或服务。

管理应用程序

在一个有WMI能力的系统上,应用程能被自动控制。

                  

四.   常用的信息模式

1、对象模式

WMI提供了一个一致和可扩展的对象表述,用来管理

WMI是一个分类系统,被管理对象依类形被划分。

比如win32_share类用于共享对象,包含文件夹,打印机等等

Win32_networkadapter类是网卡,可以使10/100m以太网卡,令牌环网网卡等

子类是从父类导出的,WMI采用了一个类和子类的层次结构。在日常的管理中,最为常见的是叶子(leaf)类。

属性:对类的描述

比如:Win32_NetworkAdapter class is defined with the properties that reflect the

characteristics of interest for network cards. These include items such as Name,

Manufacturer, and MACAddress. Similarly, Win32_Process has Name,

Creationdate, and MaximumWorkingSetSize properties to describe processes

大多数的类定义了一个或多个key properties,是唯一的描述,比如win32_networkadapterdevice idwin32_sharename

另一种重要的属性是关联属性,该属性被描述成和其他类的属性有关联。

Qualifiers(限定)

比如限定一个描述最大的长度(maxlen=256

Instances(实例)

具体的某个由类派生的东西

方法:类和实例支持方法,和数据库类似

Class      Table

Instance   Row

Property    Attribute

Key Primary    key

Reference       Foreign key

2、CIM schema

CIM schema

架构用于管理和类命名

类的名称必须唯一

当前有三个主要架构被用于CIM

核心和常规架构

核心架构定义最多的抽象类,其他的类都是从抽象类导出。所以,CIM_managedsystemelement是管理对象的起点。

从这里,CIM定义CIM_logicalelement,随后,定义了CIM_Logicaldevice,当然,这些类可以继续细分下去,比如CIM_MediaAccessDevice

有两个schema被定义在WEBM标准:vendor-independentplatform-independent,在这个级别不需要创建新的类。

Extensible schemas

Vendor-specificplatform-specific.

WMI,这些类通常使用win32_schma,这个类是在管理最重要的。这些类从wbem标准常规类中派生出来。并且包含windows的额外信息。

3、 举例:

Cscript CLASSProps.VBS

---------------------------------------------------------------------------------------------

strSuperClass = "CIM_ManagedSystemElement"   'Where to start searching

iFlags = 0                                        'search all subclasses

vClassCount = 0

vPropCount = 0

For Each oSubClass in GetObject("WinMgmts:").SubclassesOf(strSuperClass,iFlags)

If Left(oSubClass.Path_.RelPath,5) = "Win32" Then

           vClassCount = vClassCount + 1

           wscript.Echo oSubClass.Path_.RelPath

           For Each oProp in oSubClass.properties_

                    vPropCount = vPropCount + 1

                    wscript.Echo space(20)& oProp.name,vType

           Next

End If

Next

strMsg = "There were " & cStr(vClassCount)& " Win32 Classes," & vbcrlf

strMsg = strMsg & cStr(vPropCount) & " Properties " & vbcrlf

MsgBox strMsg,vbInformation,"Win32_Classes under " & strSuperClass

------------------------------------------------------------------------------------------------------

上面的例子注意: SubclassesOf方法0代表递归查找,1代表仅仅查1

通过将其改为1

就可以看到在CIM_ManagedSystemElement类下面有2个子类

CIM_PhysicalElement

CIM_LogicalElement

参考:http://msdn.microsoft.com/en-us/library/aa393875(VS.85).aspx

然后逐次替代顶级类,观察下面的子类

Cscript ClassMethods.VBS:注意,并不是所有的类都有方法

Cscript StartCalc.vbs:启动计算器

                  

五.   访问WMI

1、名称空间

名称空间

名称空间是classes的逻辑组,root是层次结构的最高级,类似文件系统

Root The start of the namespace hierarchy.

CIMV2           The namespace for storing objects that relate to the management domain of systems such as computers and their operating systems.

DEFAULT      The namespace where classes are created by default, if no other namespace is specified.

directory         A generic namespace for directory services. WMI creates a sub-namespace called LDAP.

SECURITY      A namespace used to support WMI on Windows 9x computers.

WMI                A namespace for classes that use Windows Driver Model providers. This avoids name conflicts with class names in the CIMV2 namespace.

注意,不要将namespaces和类的继承混淆。

2、WMI providers

WMI providers是标准的com/com+ servers

能够:

  • 处理动态的dll
  • 本地/远程windows nt/2000服务
  • Out-of-process EXEs

Provider roles

Instance provider:提供管理对象的instanceswindows management service

Event provider:写报告到windows management service从被管理对象被生成的时候

Property provider: 找回和编辑属性

Method provider: 调用请求操作

Class provider: 找回,编辑,删除 和枚举特定的class

Provider type

Application-specific provider:是由特定的应用程序提供的。比如sqladmin provider,exchange provider

Built-in provider: WMI SDK标准提供

所有的providers必须注册在wmi repository。这是windows management service知道如何调用那些服务。

For each obj in _

GetObject("WinMgmts:\\server1").instancesOf("__win32Provider")

  strResult = strResult & obj.name & vbcrlf

Next

WScript.Echo strResult

参考:http://msdn.microsoft.com/en-us/library/aa393870(VS.85).aspx

3、WMI的标准provider

WMI 标准 providers

每一个标准provider有一个mof file来构建class,并且注册在名称空间中。标准providerswmiwmi sdk提供。

Win32 provider

The Win32 provider defines the classes used to describe hardware or software available on Win32 systems and the relationships between them.

SNMP Provider

默认没有直接安装,system\wbem\wbemsnnp.exe

安装后在名称空间中形成:root\snmp

Performance Counter Provider

perfomance monitor中实时返回数据

Registry Provider

应用程序使用它读取和修改注册表

Windows Driver Model Provider (WDM)

提供了设备的信息的简要方法

4、MOF

WMI repository的规范能够被描述为一个或多个MOF文件

Managed Object Format Language

是一种编译语言,由DMTFManaged Object Format Language)创建,并给予IDLInterface

Definition language),MOF被用于定义名称空间,类和实例

MOFcomp

名称空间,类,和实例被定义在asciiunicode文本文件中,并被提交到MOF编译器。MOFcomp.exe将文本文件编译为2进制格式。Windows management service使用2进制格式来增加名称空间,类和实例到cim repository.

WMI Repository被编辑在多个MOF文件中,核心的MOF文件安装在%systemroot%\system32\wbem文件夹中。

MOF File                   Classes supported by

Cimwin32.mof             Win32 provider

DSProv.MOF               Directory Service provider

NTEvt.MOF                 NT Event Log provider

RegEvent.MOF            Registry Event provider

Msi.MOF                    MS Installer provider

自动恢复

Windows Management service启动的时候,WMI检查WMI repository的完整性,如果repository被破坏了,WMI自动重建和重编译MOF文件那些列在autorecover mofs key中的。

HKLM\Software\Microsoft\WBEM\Cimom\autorecover mofs

posted @ 2010-08-28 00:24  覆雨翻云  阅读(20687)  评论(0)    收藏  举报