SCOM을 통해 성능 수집을 할 때 보통 WMI를 쿼리해서 수집하는데, 프로세스 CPU 사용률에 대한 값이 영 믿을만한 값이 아닌 것 같아 다른 방법이 없을까..하고 찾아보던 중 성능 카운터를 쿼리하는 typeperf라는 명령이 있더군요...
아래 스크립트는 이 typeperf 명령 결과를 이용해 화면에 출력하거나 SCOM의 성능 카운터로 저장하는 스크립트입니다.
'Instance: $Data/Property[@Name='Instance']$
'PerfValue: $Data/Property[@Name='PerfValue']$
strProcessName = "svchost"
Set oAPI = CreateObject("MOM.ScriptAPI")
strCmdProcID = "typeperf "&chr(34)&"\Process(*)\ID Process"&chr(34)&" -sc 1"
strCmdCPUPerf = "typeperf "&chr(34)&"\Process(*)\% Processor Time"&chr(34)&" -sc 1"
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObjectProcID = objShell.Exec(strCmdProcID)
Set objExecObjectCPUPerf = objShell.Exec(strCmdCPUPerf)
strComputerName = objShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
i = 0
Do While Not objExecObjectProcID.StdOut.AtEndOfStream
tmpLine = objExecObjectProcID.StdOut.ReadLine()
Select Case i
Case "1"
tmpLine = Replace(tmpLine,"\\"&strComputerName&"\Process(","")
tmpLine = Replace(tmpLine,")\ID Process","")
tmpLine = Replace(tmpLine,chr(34),"")
strHeader = tmpLine
Case "2"
tmpLine = Replace(tmpLine,".000000","")
tmpLine = Replace(tmpLine,chr(34),"")
strResult = tmpLine
End Select
i = i + 1
Loop
ProcIDHeader = Split(strHeader,",")
ProcIDResult = Split(strResult,",")
i = 0
Do While Not objExecObjectCPUPerf.StdOut.AtEndOfStream
tmpLine = objExecObjectCPUPerf.StdOut.ReadLine()
Select Case i
Case "1"
tmpLine = Replace(tmpLine,"\\"&strComputerName&"\Process(","")
tmpLine = Replace(tmpLine,")\% Processor Time","")
tmpLine = Replace(tmpLine,chr(34),"")
strHeader = tmpLine
Case "2"
tmpLine = Replace(tmpLine,chr(34),"")
strResult = tmpLine
End Select
i = i + 1
Loop
ProcCPUHeader = Split(strHeader,",")
ProcCPUResult = Split(strResult,",")
For i = 0 to Ubound(ProcIDHeader)
tmpProcIDHeader = ""
If InStr(ProcIDHeader(i),"#") <> 0 Then
tmpProcIDHeader = Left(ProcIDHeader(i),InStr(ProcIDHeader(i),"#")-1)
End If
If Ucase(ProcIDHeader(i)) = Ucase(strProcessName) or Ucase(tmpProcIDHeader) = Ucase(strProcessName) Then
WScript.Echo ProcIDHeader(i),ProcIDResult(i),ProcIDHeader(i),ProcCPUResult(i)
' Set propertyBag = oAPI.CreatePropertyBag()
' propertyBag.AddValue "Instance", ProcIDHeader(i)
' propertyBag.AddValue "PerfValue", ProcCPUResult(i)
' oAPI.AddItem(propertyBag)
End If
Next
'oAPI.ReturnItems