[VBS]프로세스의 CPU 사용률 모니터 - SCOM SystemCenter2010. 9. 29. 14:17
Windows의 성능 카운터 상에서 보여지는 프로세스의 CPU 사용률은 각 코어에 대한 사용률의 합이 된다. 예를들어, 4-코어인 경우 최대 400%까지 CPU 사용률이 나올 수 있어 GUI로 구현하게 되면 오탐이 발생하게 된다.
VBS VBScript SCOM System Center Operations Manager
' 프로세스 CPU 사용률(%) 모니터(두 가지 상태)
' 수집 간격(분) x 수집 횟수 시간 동안 스크립트 실행됨(실행 주기 및 제한 시간 설정 시 유의)
' 수집 시간동안 임계값을 연속해서 초과하는 경우 상태는 Bad
' 비정상 상태 정의: Property[@Name='Status'] 같음 Good
' 정상 상태 정의: Property[@Name='Status'] 같음 Bad
' 경고 설명: $Data/Context/Property[@Name='Message']$
' 모니터링 설정
Interval = 1 ' 수집 간격(분)
Sampling = 1 ' 수집 횟수
strProcess = "notepad" ' 대상 프로세스
PerfValue = 30 ' 임계값(%)
If UCASE(RIGHT(strProcess,3)) = "EXE" Then
strProcess = LEFT(strProcess,LEN(strProcess) - 4)
End If
ProcessorCount = GetNumProcessors
Dim oAPI, oBag
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()
i = 0
Summary = 0
Do While ( i < Sampling )
i = i + 1
ResultValue = GetCPUPerf(strProcess)
If ResultValue >= PerfValue Then
Summary = Summary + 1
End If
If Sampling = 1 Then
Exit Do
End If
WScript.Sleep Interval*60000
Loop
If cInt(Summary) = cInt(Sampling) Then
Status = "Bad"
Message = "프로세스 " & strProcess & "의 CPU 사용률이 설정된 임계값(" & PerfValue & "%)을 초과하였습니다. 프로세스 " & strProcess & "의 CPU 사용률은 " & RoundDown(ResultValue) & "%입니다."
Else
Status = "Good"
Message = "프로세스 " & strProcess & "의 CPU 사용률이 설정된 임계값(" & PerfValue & "%) 이하입니다. 프로세스 " & strProcess & "의 CPU 사용률은 " & RoundDown(ResultValue) & "%입니다."
End If
Call oBag.AddValue("Message",Message)
Call oBag.AddValue("Status",Status)
Call oBag.AddValue("CPU Usage",RoundDown(ResultValue) & "%")
Call oAPI.Return(oBag)
Function GetCPUPerf(ProcessName)
Set WMI_Service = GetObject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2")
sObjectPath = "Win32_PerfRawData_PerfProc_Process.Name=" & chr(34) & ProcessName & chr(34)
Set perf_instance1 = WMI_Service.Get( sObjectPath )
N1 = perf_instance1.PercentProcessorTime
D1 = perf_instance1.TimeStamp_Sys100NS
WScript.Sleep(1000)
Set perf_instance2 = WMI_Service.get( sObjectPath )
N2 = perf_instance2.PercentProcessorTime
D2 = perf_instance2.TimeStamp_Sys100NS
If ( 0 = (D2-D1) ) then
GetCPUPerf = 0
Else
PercentProcessorTime = ((N2 - N1) / (D2 - D1)) * 100
GetCPUPerf = PercentProcessorTime / ProcessorCount
End If
Set WMI_Service = Nothing
End Function
Function RoundDown(Value)
If InStr(Value, ".") Then
RoundDown = Left(Value, InStr(Value, ".") + 3)
Else
RoundDown = Value
End If
End Function
Function GetNumProcessors
Set WMI_Service = GetObject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2")
Set colItems = WMI_Service.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objItem In colItems
On Error Resume Next
GetNumProcessors = objItem.NumberOfLogicalProcessors
If Err.number <> 0 Then
GetNumProcessors = objItem.NumberOfProcessors
End If
On Error GoTo 0
Next
End Function