VB 스크립트 - 아래 스크립트와 연계해 해당 PID에 대한 성능 수집 Etc.2013. 2. 21. 00:23
아래 스크립트와 연계해 해당 PID에 기반해 CPU 사용률과 메모리 사용량 - WorkingSet (MB)을 수집하는 스크립트이며, SCOM에서 성능 수집 규칙(컬렉션 방식)으로 사용합니다. 로그 파일 생성(변경) 시간이 10분 이상되었거나 내용이 없는 경우(헤더만 있는 경우)에는 스크립트를 종료합니다.
'==========================================================
'Log File Configuration
'==========================================================
sLogDir = "C:\Dimensiondata\"
sLogFile = "NOTEPAD.Log"
'==========================================================
'==========================================================
'Performance Mapper
'==========================================================
'Object: Process
'Counter: $Data/Property[@Name='Counter']$
'Instance: $Data/Property[@Name='Instance']$
'Value: $Data/Property[@Name='PerfValue']$
'==========================================================
'If Log File doesn't Exist Then Quit
Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists(sLogDir&sLogFile) Then
Else
Set oFSO = Nothing
WScript.Quit
End If
'Check Log File Modified Date/Time
Set oFile = oFSO.GetFile(sLogDir&sLogFile)
FileModified = CDate(oFile.DateLastModified)
TimeGapMin = DateDiff("n",FileModified,Now)
If TimeGapMin > 10 Then
Set oFSO = Nothing
Set oAPI = CreateObject("MOM.ScriptAPI")
Call oAPI.LogScriptEvent("DDK", 2001, 2, "Modified Date of Log File is Older than 10 Minutes. Modified Date of Log File("&sLogDir&sLogFile&") is "&FileModified&".")
Set oAPI = Nothing
WScript.Quit
End If
'Check Log File Content
sData = oFSO.OpenTextFile(sLogDir&sLogFile,1).ReadAll
arrLines = Split(sData,vbCrLf)
LineCount = UBound(arrLines)-1
If LineCount = 0 Then
Set oFSO = Nothing
Set oAPI = CreateObject("MOM.ScriptAPI")
Call oAPI.LogScriptEvent("DDK", 2001, 2, "Log File("&sLogDir&sLogFile&") is Empty.")
Set oAPI = Nothing
WScript.Quit
End If
'Create Array and Insert PID, Performance Data of CPU and WorkingSet into Array
Set objWMIService = GetObject("WINMGMTS:\\.\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_PerfFormattedData_PerfProc_Process")
ProcessCount = colProcess.Count
ReDim tmpProcessID(ProcessCount),tmpProcessCPU(ProcessCount),tmpProcessMB(ProcessCount)
i = 0
For Each ProcInst in colProcess
tmpProcessID(i) = ProcInst.IDProcess
tmpProcessCPU(i) = ProcInst.PercentProcessorTime
tmpProcessMB(i) = Round(ProcInst.WorkingSet/1048576,2)
i = i + 1
Next
Set colProcess = Nothing
Set objWMIService = Nothing
'Get/Return CPU, Memory Performance Data, Except First Line
Set oAPI = CreateObject("MOM.ScriptAPI")
CurrLine = 1
Do While CurrLine <> UBound(arrLines)
tmpLine = Split(arrLines(CurrLine),"|")
tmpCPU = 0
tmpMB = 0
For i = 0 to ProcessCount
If cDbl(tmpLine(1)) = cDbl(tmpProcessID(i)) Then
tmpCPU = tmpProcessCPU(i)
tmpMB = tmpProcessMB(i)
End If
Next
Set propertyBag = oAPI.CreatePropertyBag()
propertyBag.AddValue "Counter", "% Processor Time"
propertyBag.AddValue "Instance", tmpLine(0)&"_"&tmpLine(2)
propertyBag.AddValue "PerfValue", tmpCPU
oAPI.AddItem(propertyBag)
Call oAPI.Return(propertyBag)
Set propertyBag = oAPI.CreatePropertyBag()
propertyBag.AddValue "Counter", "Memory Usage MB"
propertyBag.AddValue "Instance", tmpLine(0)&"_"&tmpLine(2)
propertyBag.AddValue "PerfValue", tmpMB
oAPI.AddItem(propertyBag)
Call oAPI.Return(propertyBag)
CurrLine = CurrLine + 1
Loop
oAPI.ReturnItems
Set oFSO = Nothing
Set oAPI = Nothing