Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WMI - Processor Uage 3

Status
Not open for further replies.

Ajb2528

Technical User
Feb 22, 2002
270
GB
Hi All!!

I am trying to implement a server system monitoring tool using WMI and cannot seem to get a percentage processor usage (i.e. - how 'busy' the cpu is). I have tried the follwing in WMI but it appears to be very sporadic - usualy returning 0% but sometimes 20-30%. Has anyone got any ideas on how to return a reasonable figure for this?


Regards,

Alan
 
Rick,

I am connecting to a remote server - running Windows200 Advanced Server. The code is below:

Try
Dim connection As New ConnectionOptions
connection.Username = userNameBox.Text
connection.Password = passwordBox.Text
connection.Authority = "ntlmdomain:QCG"

Dim scope As New ManagementScope( _
"\\PROD-SQL\root\CIMV2", connection)
scope.Connect()

Dim query As New ObjectQuery( _
"SELECT * FROM Win32_OperatingSystem")

Dim searcher As New ManagementObjectSearcher(scope, query)
Dim iPos As Integer

For Each queryObj As ManagementObject In searcher.Get()
txtOp.Text = "Computer name: " & queryObj("CSname").ToString() & vbCrLf
sResult = queryObj("name").ToString()
iPos = InStr(sResult, "|")
sResult = Microsoft.VisualBasic.Left(sResult, iPos - 1)
txtOp.Text += "Operating System: " & sResult & vbCrLf
txtOp.Text += "Version: " & queryObj("Version").ToString() & vbCrLf
txtOp.Text += "Service Pack " & queryObj("ServicePackMajorVersion") & vbCrLf
txtOp.Text += "Windows Directory: " & _
queryObj("WindowsDirectory").ToString() & vbCrLf
Next

Dim search As New ManagementObjectSearcher("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor")

Dim info As ManagementObject
For Each info In search.Get()

txtResult.Text = "DPC Rate: " & info("DPCRate") & vbCrLf
txtResult.Text += "DPCs Queued Per Second: " & info("DPCsQueuedPersec") & vbCrLf
txtResult.Text += "Interrupts Per Second: " & info("InterruptsPersec") & vbCrLf
txtResult.Text += "Name: " & info("Name") & vbCrLf
txtResult.Text += "Percent DPC Time: " & info("PercentDPCTime") & vbCrLf
txtResult.Text += "Percent Idle Time: " & info("PercentIdleTime") & vbCrLf
txtResult.Text += "Percent Interrupt Time: " & info("PercentInterruptTime") & vbCrLf
txtResult.Text += "Percent Privileged Time: " & _
info("PercentPrivilegedTime") & vbCrLf
txtResult.Text += "Percent Processor Time: " & info("PercentProcessorTime") * 10 & vbCrLf
txtResult.Text += "Percent User Time: " & info("PercentUserTime")

Next

Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
Catch unauthorizedErr As System.UnauthorizedAccessException

MessageBox.Show("Connection error (user name or password might be incorrect): " & unauthorizedErr.Message)
End Try

End Sub

Regards,

Alan
 
it works fine for me

on a windows 2000 server (standard)
and a windows 2003 server (standard)

you just don't need to multiply it with 10 since it already gives you the percentage I put a timer on it and queried it every second and it worked like a charm even on a 2 proc system.



Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Christiaan/Rick,

Thanks for the replies. What I have decided to do is combine using WMI with using the performance counters that I came across in .NET. These seem to work (in my setup anyway) better than the WMI versions

Regards,

Alan
 
and can you tell me how you did that.



Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Christiaan,

I dont know how to post a project onto this website, so if you can give me an EMail or some other idea on how to post a complete project I will happily do so!

Regards,

Alan
 
Chrissie isn't asking to see the whole project, he was just asking to see the code that you came up with when you said "using the performance counters that I came across in .NET" as it will benefit future readers of this post.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
yep just the code to get the processor timer and perhaps the properties to set the performance counter.

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Christiaan,

Here is the code that I used...

Imports System.Diagnostics

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Dim oCounter As New System.Diagnostics.PerformanceCounter

Dim sServerName As String = "servername"
Dim sInstanceName As String = "_Total"
Dim sCategoryName As String = "Processor"
Dim sCounterName As String = "% Processor Time"


oCounter.MachineName = sServerName
oCounter.CategoryName = sCategoryName
oCounter.CounterName = sCounterName
oCounter.InstanceName = sInstanceName
oCounter.NextValue()

Dim iRet As Double = oCounter.NextValue

txtOutput.Text = iRet


oCounter.Close()

End Sub

You will need to add a reference in your project to:

System.Management dll

While we are on the subject, I found a great 'scrolling' graph component on the Code Project web site. You can find it at:


Regards,

Alan
 
mmm, I tried that yesterday and din't get any result at least not the same as with wmi-method you showed above. Have to work on it a little more. Allthough I'm pretty sure the performancecounter uses wmi.

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Sorry for being rude, thanks for the code and have a star.

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Christiaan,

I have also found that, on occasions, I get a zero result. I have put this down to network traffic. I use a timer interval rate of 100. Also, I have found a tool (from Microsoft) that generates WMI Code in C#, VB.Net and VBScript. You can download it from:


Regards,

Alan
 
Thanks again. I added it to my favorites list.

the performancecounter only gives me a result every 5 minutes aprox. with a timer on 2000 the rest are 0. while wmi gives me better results.

Oh well, that's life.

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Christiaan,

I found an example (in VBScript) to get the processor time using WMI. I converted this to VB.Net and I have found that the results are really good! The code is attached below.

Regards,

Alan

Try

Dim search1 As New ManagementObjectSearcher("SELECT * FROM Win32_PerfRawData_PerfOS_Processor")
Dim search2 As New ManagementObjectSearcher("SELECT * FROM Win32_PerfRawData_PerfOS_Processor")
Dim info1 As ManagementObject
Dim info2 As ManagementObject
Dim PercentProcessorTime As Integer

Dim N1 As Long
Dim N2 As Long
Dim D1 As Long
Dim D2 As Long

For Each info1 In search1.Get()
N1 = info1("PercentProcessorTime")
D1 = info1("Timestamp_Sys100NS")
Next

System.Threading.Thread.Sleep(1000)

For Each info2 In search2.Get()
N2 = info2("PercentProcessorTime")
D2 = info2("Timestamp_Sys100NS")
Next

' CounterType - PERF_100NSEC_TIMER_INV
' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
PercentProcessorTime = (1 - ((N2 - N1) / (D2 - D1))) * 100
txtCPU.Text = PercentProcessorTime
Tracker1.Value = PercentProcessorTime
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top