Fehu
Fehu

Reputation: 401

Get the server's cpu utilization

I've searched around and got this code that apparently works for everyone:

Set objWMIService2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 
Set colItems = objWMIService2.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'") 
For Each objItem In colItems 
    Response.Write( "CPU Usage Percentage: " & objItem.PercentProcessorTime & "%" )
Next

Executing it I get a completely blank page, not an error.

Actually the os is virtualized on the server, is this the problem? And if so, there's a workaround?

Upvotes: 0

Views: 1726

Answers (1)

langstrom
langstrom

Reputation: 1702

What's the error you're getting?

Here's what I'm using for the same thing (stripped down a bit). It shows load per physical processor.

strComputer = "."
Dim arrProcessors : ReDim arrProcessors(2,0)

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colCPUSystems = objWMIService.ExecQuery("Select AddressWidth,DataWidth,NumberOfCores,Name,MaxClockSpeed,LoadPercentage from Win32_Processor")
proc = 0
For Each objProc in colCPUSystems
    arrProcessors(0,proc) = objProc.NumberOfCores
    arrProcessors(1,proc) = objProc.MaxClockSpeed
    arrProcessors(2,proc) = objProc.LoadPercentage
    proc = proc + 1
    ReDim Preserve arrProcessors(2,proc)
    strOSBits = objProc.AddressWidth
    strHWBits = objProc.DataWidth
    strProcessorCores = objProc.NumberOfCores
    strProcessor = objProc.Name
    strProcessorSpeed = objProc.MaxClockSpeed
Next

For proc = 0 To UBound(arrProcessors,2)-1
    intLoad = arrProcessors(2,proc)
    intFree = 100-intLoad

    strProcessorInfo = strProcessorInfo & "Processor " & proc+1 _
    & ": " & arrProcessors(0,proc) & " Cores : Load " & intLoad & "%" & vbCrLf 
Next 

WScript.Echo strProcessorInfo

Upvotes: 1

Related Questions