성능 데이터 모니터링

WMI를 사용하여 성능 라이브러리의 개체에서 프로그래밍 방식으로 시스템 카운터 데이터에 액세스할 수 있습니다. 이는 Perfmon 유틸리티의 시스템 모니터에 표시되는 것과 동일한 성능 데이터입니다. 사전 설치된 성능 카운터 클래스를 사용하여 스크립트 또는 C++ 애플리케이션에서 성능 데이터를 가져옵니다.

이 항목에서 다루는 섹션은 다음과 같습니다.

WMI 성능 클래스

예를 들어, 시스템 모니터의 "NetworkInterface" 개체의 경우 원시 데이터는 Win32_PerfRawData_Tcpip_NetworkInterface 클래스로, 미리 계산되거나 형식이 지정된 데이터는 Win32_PerfFormattedData_Tcpip_NetworkInterface 클래스로 WMI에 표현됩니다. Win32_PerfRawDataWin32_PerfFormattedData서 파생된 클래스는 refresher 개체와 함께 사용해야 합니다. 원시 데이터 클래스에서 C++ 애플리케이션 또는 스크립트는 계산을 수행하여 Perfmon.exe와 동일한 출력을 가져와야 합니다. 형식이 지정된 데이터 클래스는 미리 계산된 데이터를 제공합니다. C++ 애플리케이션에서 데이터를 가져오는 방법에 대한 자세한 내용은 C++에서 성능 데이터 액세스를 참조하세요. 스크립팅은 스크립트에서 성능 데이터 액세스스크립트에서 WMI 데이터 새로 고침을 참조하세요.

다음 VBScript 코드 예제에서는 Win32_PerfFormattedData_PerfProc_Process를 사용하여 유휴 프로세스의 성능 데이터를 가져옵니다. 스크립트는 Process 개체의 % Processor Time 카운터에 대해 Perfmon에 표시되는 것과 동일한 데이터를 표시합니다. SWbemObjectEx.Refresh_를 호출하면 새로 고침 작업이 수행됩니다. 기준을 얻으려면 데이터를 한 번 이상 새로 고쳐야 합니다.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
set PerfProcess = objWMIService.Get(_
    "Win32_PerfFormattedData_PerfProc_Process.Name='Idle'")

While (True)
    PerfProcess.Refresh_     
    Wscript.Echo PerfProcess.PercentProcessorTime
    Wscript.Sleep 1000
Wend

성능 카운터 클래스는 통계 데이터도 제공할 수 있습니다. 자세한 내용은 통계 성능 데이터 가져오기를 참조하세요.

성능 데이터 공급자

WMI에는 로컬 시스템에서 그리고 원격으로 시스템 성능을 모니터링하는 공급자가 설치되어 있습니다. WmiPerfClass 공급자는 Win32_PerfRawDataWin32_PerfFormattedData서 파생된 클래스를 만듭니다. WmiPerfInst 공급자는 원시 클래스와 형식이 지정된 클래스 모두에 데이터를 동적으로 제공합니다.

형식이 지정된 성능 데이터 클래스 사용

다음 VBScript 코드 예제에서는 메모리, 디스크 파티션 및 서버 작업 큐에 관한 성능 데이터를 가져옵니다. 그런 다음, 스크립트는 이러한 값이 허용 범위 내에 있는지 확인합니다.

스크립트는 다음 WMI 공급자 개체 및 스크립팅 개체를 사용합니다.

Set objCimv2 = GetObject("winmgmts:root\cimv2")
Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")

' Add items to the SWbemRefresher
' Without the SWbemRefreshableItem.ObjectSet call,
' the script will fail
Set objMemory = objRefresher.AddEnum _
    (objCimv2, _ 
    "Win32_PerfFormattedData_PerfOS_Memory").ObjectSet
Set objDiskQueue = objRefresher.AddEnum _
    (objCimv2, _
    "Win32_PerfFormattedData_PerfDisk_LogicalDisk").ObjectSet
Set objQueueLength = objRefresher.AddEnum _
    (objCimv2, _
    "Win32_PerfFormattedData_PerfNet_ServerWorkQueues").ObjectSet

' Initial refresh needed to get baseline values
objRefresher.Refresh
intTotalHealth = 0
' Do three refreshes to get data
For i = 1 to 3
    WScript.Echo "Refresh " & i
    For each intAvailableBytes in objMemory
        WScript.Echo "Available megabytes of memory: " _
            & intAvailableBytes.AvailableMBytes
        If intAvailableBytes.AvailableMBytes < 4 Then
            intTotalHealth = intTotalHealth + 1
        End If
    Next
    For each intDiskQueue in objDiskQueue
        WScript.Echo "Current disk queue length " & "Name: " _
            & intDiskQueue.Name & ":" _
            & intDiskQueue.CurrentDiskQueueLength
        If intDiskQueue.CurrentDiskQueueLength > 2 Then
            intTotalHealth = intTotalHealth + 1
        End If
    Next
    For each intServerQueueLength in objQueueLength
        WScript.Echo "Server work queue length: " _
            & intServerQueueLength.QueueLength
        If intServerQueueLength.QueueLength > 4 Then
            intTotalHealth = intTotalHealth + 1                       
        End If
    Wscript.Echo "  "
    Next
    If intTotalHealth > 0 Then
        Wscript.Echo "Unhealthy."
    Else
        Wscript.Echo "Healthy."
    End If
    intTotalHealth = 0
    Wscript.Sleep 5000
' Refresh data for all objects in the collection
    objRefresher.Refresh
Next

원시 성능 데이터 클래스 사용

다음 VBScript 코드 예제에서는 로컬 컴퓨터에서 원시, 현재 백분율 프로세서 시간을 가져와 백분율로 변환합니다. 이 예제에서는 Win32_PerfRawData_PerfOS_Processor 클래스의 PercentProcessorTime 속성에서 원시 성능 데이터를 가져오는 방법을 보여줍니다.

백분율 프로세서 시간 값을 계산하려면 수식을 찾아야 합니다. CounterType 한정자 표의 PercentProcessorTime속성에 대한 CounterType 한정자에서 이 값을 조회합니다. 카운터 형식에서 상수 이름을 찾아 수식을 가져옵니다.

Set objService = GetObject( _
    "Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")

For i = 1 to 8
    Set objInstance1 = objService.Get( _
        "Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
    N1 = objInstance1.PercentProcessorTime
    D1 = objInstance1.TimeStamp_Sys100NS

'Sleep for two seconds = 2000 ms
    WScript.Sleep(2000)

    Set perf_instance2 = objService.Get( _
        "Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
    N2 = perf_instance2.PercentProcessorTime
    D2 = perf_instance2.TimeStamp_Sys100NS
' Look up the CounterType qualifier for the PercentProcessorTime 
' and obtain the formula to calculate the meaningful data. 
' CounterType - PERF_100NSEC_TIMER_INV
' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100

    PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100
    WScript.Echo "% Processor Time=" , Round(PercentProcessorTime,2)
Next

WMI 사용