Application Instrumentation with WMI

Post on 15-Jun-2015

3.319 views 9 download

Tags:

description

Presentation to the Lake County .NET User's Group on "Application Instrumentation with WMI" by Nick Schweitzer. 8/28/2010

Transcript of Application Instrumentation with WMI

Where architects code and coders architect

Application Instrumentation with

WMI

22

About Nick Schweitzer Senior Consultant for SpiderLogic in

Milwaukee Milwaukee Area Consultant for 12+ years B.S. in Computer Engineering from MSOE

Around the Internets: thecodingmonkey.net (Geek/Technical) nickschweitzer.net (Life/Politics) flickr.com/photos/schweitn (Amateur

Photography) twitter.com/NickSchweitzer (Real Time) facebook.com/NickSchweitzer (Meta)

33

Agenda What is WMI? WMI Architecture Querying System State with WMI Instrumenting .NET Applications with

WMI WMI Tools Cool Uses for WMI WMI Related Projects and Resources

44

Warning

This is Not Bleeding Edge Technology

You Could Have Been Doing this For the Last 8 Years… If You Knew It Was There.

55

What is WMI? Windows Management

Instrumentation Microsoft’s Implementation of WBEM

(Web-Based Enterprise Management)

Allows Machine and Software State to be Exposed to Local and Remote Queries for Enhanced Enterprise Management

66

Why Instrument for WMI? Provides Many Methods to Query

Application State .NET Query (System.Management

namespace) Powershell MOM/SCOM Monitoring Visual Studio Analyzer

Inter-process Signaling & Communication Useful for Monitoring and Debugging

Services w/o User Interfaces Watchdog Processes and

Redundant/Failover Services

77

88

Common Information Model (CIM) Language Independent Model for

Representing Enterprise Objects Supports Inheritance & Object

Relationships Supports Schemas for Objects

CIM_* (Core/Common Classes) Win32_* (Windows Implementation

Specific Classes) Can Create User Defined Schemas

99

WMI Namespaces Independent of .NET Namespaces

Best Practice is to create a Root Namespace for Company and Sub-Namespaces for applications or services

User Access Security can be set at a Namespace level on each machine.

Win32_* Objects in ROOT\CIMV2 Used for Querying Machine &

Operating System state

1010

WMI Query Language (WQL) SQL Subset Used to Query WMI Repository Get Name of All Running Services

SELECT DisplayName FROM Win32_Service WHERE State = 'Running'

Get Local Computer Name SELECT Name FROM Win32_ComputerSystem

Get List of All Local Groups SELECT * FROM Win32_Group WHERE LocalAccount = True

1111

Subscribing to Events with WQL Can Be Notified of the Following:

Instance Creation and Deletion Property Changes Custom Events

Example – New Process Notification SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'

WITHIN Keyword Defines Polling Frequency in Seconds

1212

Demo: WQL in Powershell

1313

.NET Instrumentation with WMI Attribute Based Two Sets of Attributes in One

Assembly System.Management.Instrumentation Both sets can be used together in

one application. Both sets cannot be used together

in one class. Requires Assembly Registration

1414

Original WMI Attributing (v1) Properties Instrumented by Default Resulting WMI objects can only be

used for Query Can Only Instrument Properties that

are Value Types Cannot Expose Methods Available for Use in .NET 1.0 and later

1515

Custom WMI Classes (v1) InstrumentationClass

Marks a class as being Instrumented All properties are Instrumented by

Default Can also derive from Instance class

instead IgnoreMember

Marks a property as being not Instrumented

Required for properties that cannot be Instrumented

Instrumentation.Publish() Makes Object Available to WMI

Repository

1616

Custom WMI Events Only Available in v1 InstrumentationClass

InstrumentationType = Event Call Instrumentation.Fire() to

Raise Event Derive class from BaseEvent

Call Fire() method to Raise Event Events Can Only Reference WMI

Primitives

1717

WMI Registration (v1) Instrumented Attribute

Assembly Level Attribute Specify Namespace

DefaultManagementProjectInstaller

Use InstallUtil.exe to register Namespace and CIM Schema

1818

New WMI Attributing (v2) Properties Instrumented only if

Attributed Can Create New .NET objects using

WMI Invoke Constructors, Methods,

Change Properties, etc. Can Instrument Properties that are

Reference Types (if also Instrumented)

Available for Use in .NET 3.5 and later.

1919

Custom WMI Classes (v2) ManagementEntity

Marks a Class as being instrumented (equivalent to InstrumentationClass in v1)

ManagementKey Marks a Property as being a unique identifier

ManagementProbe/ManagementConfiguration Marks a Property as being instrumented

MangementBind Marks a Method/Constructor available to WMI for

object creation ManagementTask

Marks a Method available to WMI for invocation InstrumentationManager.Publish()

Makes Object Available to WMI Repository

2020

WMI Registration (v2) WmiConfiguration

Assembly Level Attribute Specify Namespace HostingModel

Use Decoupled for Application Instrumenation

DefaultManagementInstaller Use InstallUtil.exe to register

Namespace and CIM Schema

2121

Runtime WMI Registration Registration Possible w/o InstallUtil.exe Registering All Types in an Assembly

Instrumentation.RegisterAssembly (v1) InstrumentationManager.RegisterAssembly

(v2) InstrumenationManager.UnregisterAssembly

(v2) Registering Individual Classes

InstrumentationManager.RegisterType (v2) InstrumentationManager.UnregisterType

(v2)

2222

Which Attribute System?Question v1 v2

Do you only want read-write access to your objects?

Do you want to fire events?

Do you want to call methods?

Do you want to create objects through WMI?

Do you want to view reference types as properties?

Is your application running under Framework v2.0 or below?If your application does not require v2 WMI Attributes, then don’t use them. The object management is not worth the hassle.

2323

Demo: Instrumenting .NET Applications

2424

WMI in WCF Used to Turn On WCF Diagnostics at

Runtime Must Enable WMI in Configuration

File:

<system.serviceModel> … <diagnostics wmiProviderEnabled="true" /> … </system.serviceModel>

Published in root\ServiceModel AppDomainInfo Object

LogMalformedMessages : Boolean LogMessagesAtServiceLevel : Boolean LogMessagesAtTransportLevel : Boolean

2525

WMI in WCF Continued Using Powershell to Get WCF

Diagnostic Settings Get Diagnostic Settings:

get-wmiobject -class "AppDomainInfo" -namespace "root\servicemodel" -computername "."

Changes Made at Runtime Not Stored in Configuration File – Only Valid During Current Run

2626

WMI “Productivity” Tools Windows Management Instrumentation

Tester Used to Browse and Query WMI Objects

and Schemas wbemtest.exe

WMI Code Creator Utility to Quickly Generate WQL Code in

Multiple Languages Management Strongly Typed Class

Generator Generates Strongly Types Classes for

Querying and Accessing WMI objects mgmtclassgen.exe (Windows SDK)

WMI CIM Studio ActiveX powered tool used to browse

WMI objects on local machine.

2727

Demo: WMI Tools

2828

Custom MMC Snap-Ins Requires MMC 3.0 (Separate Install for Win

XP) Run mmcperf.exe Before Installing a New

Snap-In Can Hook Into Computer Management if

you know the right GUID’s Snap-In DLL’s Registered using

InstallUtil.exe Relatively Easy to Determine Target

Computer to Query Provides Framework and Base Classes for

Creating Uniform Looking MMC Snap-In UI

2929

Demo: MMC Snap-In with .NET

3030

Presentation Resources Presentation Slides and Code

Samples www.thecodingmonkey.net

Microsoft Downloads WMI CIM Studio WMI Code Creator

MMC Extensible Node GUID’s Computer Management GUID List Server Manager GUID List

3131

Other Online Resources Windows Management Infrastructure

Blog http://blogs.msdn.com/wmi/

Linq to WMI Project http://linq2wmi.codeplex.com Not Actively Maintained, and

Somewhat Incomplete

3232

Questions?

Help Make My Presentations Better Please Fill Out an Evaluation

Form! Opinions are Completely

Anonymous (Unless You Don’t Want Them to Be)

Thank You!