달력

2

« 2014/2 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
아래 스크립트와 동일하게 프로세스의 CPU 사용률을 가져오기 위한 VB 스크립트입니다. 기존 방법들과 다른 점은 Refresher라는 메서드를 이용합니다. 아래 사이트를 확인해 보면, 단순히 Win32_PerfFormattedData_PerfProc_Process 클래스를 쿼리하는 경우에는 정확한 값이 나오지 않을 수 있다고 합니다.

http://blogs.technet.com/b/heyscriptingguy/archive/2005/04/21/why-does-my-performance-monitoring-script-keep-returning-the-same-incorrect-values.aspx



'Instance: $Data/Property[@Name='Instance']$
'PerfValue: $Data/Property[@Name='PerfValue']$

strProcessName = "svchost"

Set oAPI = CreateObject("MOM.ScriptAPI")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")

Set colItems = objRefresher.AddEnum(objWMIService, "Win32_PerfFormattedData_PerfProc_Process").objectSet

objRefresher.Refresh
Wscript.Sleep 1000
objRefresher.Refresh

For Each Item In colItems
  strInstance = Item.Name
  strPerfValue = Item.PercentProcessorTime

  tmpString = ""
  If InStr(strInstance,"#") <> 0 Then
    tmpString = Left(strInstance,InStr(strInstance,"#")-1)
  End If

  If Ucase(strInstance) = Ucase(strProcessName) OR Ucase(tmpString) = Ucase(strProcessName) Then
    If tmpString <> "" Then
      strInstance = tmpString&"_"&Item.IDProcess
    Else
      strInstance = strInstance&"_"&Item.IDProcess
    End If

    WScript.Echo strInstance, strPerfValue

'    Set propertyBag = oAPI.CreatePropertyBag()
'    propertyBag.AddValue "Instance", strInstance
'    propertyBag.AddValue "PerfValue", strPerfValue
'    oAPI.AddItem(propertyBag)
  End If
Next

Set colItems = Nothing
'oAPI.ReturnItems



 
:
Posted by 커널64
2014. 2. 20. 10:44

typeperf 명령을 통한 성능 수집 VB 스크립트 Etc.2014. 2. 20. 10:44

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



 
:
Posted by 커널64