473,241 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Monitoring Server performance from .NET

Hi,

I have a test box which I would like to monitor CPU usage and run queue
during the day.

I don't want to buy any 3rd party tool, if I can do it easily, as I only
need to monitor the box's performance over a week.

I thought I could just create a .Net app or service with a timer that gets
back this data every so many minutes..

Can anyone help? Has anyone any example code..

Many thanks

JonesG
Nov 21 '05 #1
2 2486
You can use the System.Diagnostics namespace and the PerformanceCounter
class.

From MSDN;

[Visual Basic]
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Diagnostics

_

Public Class App

Private Shared PC As PerformanceCounter
Private Shared BPC As PerformanceCounter
Public Shared Sub Main()

Dim samplesList As New ArrayList()

SetupCategory()
CreateCounters()
CollectSamples(samplesList)
CalculateResults(samplesList)
End Sub 'Main

Private Shared Function SetupCategory() As Boolean
If Not
PerformanceCounterCategory.Exists("AverageCounter6 4SampleCategory") Then

Dim CCDC As New CounterCreationDataCollection()

' Add the counter.
Dim averageCount64 As New CounterCreationData()
averageCount64.CounterType = PerformanceCounterType.AverageCount64
averageCount64.CounterName = "AverageCounter64Sample"
CCDC.Add(averageCount64)

' Add the base counter.
Dim averageCount64Base As New CounterCreationData()
averageCount64Base.CounterType = PerformanceCounterType.AverageBase
averageCount64Base.CounterName = "AverageCounter64SampleBase"
CCDC.Add(averageCount64Base)

' Create the category.
PerformanceCounterCategory.Create("AverageCounter6 4SampleCategory",
"Demonstrates usage of the AverageCounter64 performance counter type.",
CCDC)

Return True
Else
Console.WriteLine("Category exists -
AverageCounter64SampleCategory")
Return False
End If
End Function 'SetupCategory
Private Shared Sub CreateCounters()
' Create the counters.

PC = New PerformanceCounter("AverageCounter64SampleCategory ",
"AverageCounter64Sample", False)

BPC = New PerformanceCounter("AverageCounter64SampleCategory ",
"AverageCounter64SampleBase", False)
PC.RawValue = 0
BPC.RawValue = 0
End Sub 'CreateCounters
Private Shared Sub CollectSamples(samplesList As ArrayList)

Dim r As New Random(DateTime.Now.Millisecond)

' Loop for the samples.
Dim j As Integer
For j = 0 To 99

Dim value As Integer = r.Next(1, 10)
Console.Write((j + " = " + value))

PC.IncrementBy(value)

BPC.Increment()

If j Mod 10 = 9 Then
OutputSample(PC.NextSample())
samplesList.Add(PC.NextSample())
Else
Console.WriteLine()
End If
System.Threading.Thread.Sleep(50)
Next j
End Sub 'CollectSamples
Private Shared Sub CalculateResults(samplesList As ArrayList)
Dim i As Integer
For i = 0 To (samplesList.Count - 1) - 1
' Output the sample.
OutputSample(CType(samplesList(i), CounterSample))
OutputSample(CType(samplesList((i + 1)), CounterSample))

' Use .NET to calculate the counter value.
Console.WriteLine((".NET computed counter value = " +
CounterSampleCalculator.ComputeCounterValue(CType( samplesList(i),
CounterSample), CType(samplesList((i + 1)), CounterSample))))

' Calculate the counter value manually.
Console.WriteLine(("My computed counter value = " +
MyComputeCounterValue(CType(samplesList(i), CounterSample),
CType(samplesList((i + 1)), CounterSample))))
Next i
End Sub 'CalculateResults


'++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
' Description - This counter type shows how many items are processed,
on average,
' during an operation. Counters of this type display a ratio of
the items
' processed (such as bytes sent) to the number of operations
completed. The
' ratio is calculated by comparing the number of items processed
during the
' last interval to the number of operations completed during the
last interval.
' Generic type - Average
' Formula - (N1 - N0) / (D1 - D0), where the numerator (N)
represents the number
' of items processed during the last sample interval and the
denominator (D)
' represents the number of operations completed during the last
two sample
' intervals.
' Average (Nx - N0) / (Dx - D0)
' Example PhysicalDisk\ Avg. Disk Bytes/Transfer
'++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
Private Shared Function MyComputeCounterValue(s0 As CounterSample, s1 As
CounterSample) As [Single]
Dim numerator As [Single] = CType(s1.RawValue, [Single]) -
CType(s0.RawValue, [Single])
Dim denomenator As [Single] = CType(s1.BaseValue, [Single]) -
CType(s0.BaseValue, [Single])
Dim counterValue As [Single] = numerator / denomenator
Return counterValue
End Function 'MyComputeCounterValue
' Output information about the counter sample.
Private Shared Sub OutputSample(s As CounterSample)
Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "+++++++++++")
Console.WriteLine("Sample values - " + ControlChars.Lf +
ControlChars.Cr)
Console.WriteLine((" BaseValue = " + s.BaseValue))
Console.WriteLine((" CounterFrequency = " + s.CounterFrequency))
Console.WriteLine((" CounterTimeStamp = " + s.CounterTimeStamp))
Console.WriteLine((" CounterType = " + s.CounterType))
Console.WriteLine((" RawValue = " + s.RawValue))
Console.WriteLine((" SystemFrequency = " + s.SystemFrequency))
Console.WriteLine((" TimeStamp = " + s.TimeStamp))
Console.WriteLine((" TimeStamp100nSec = " + s.TimeStamp100nSec))
Console.WriteLine("++++++++++++++++++++++")
End Sub 'OutputSample
End Class 'App
--
Gerry O'Brien [MVP]
Visual Basic .NET(VB.NET)


"Jonesgj" <g@btinternet.com> wrote in message
news:cj**********@hercules.btinternet.com...
Hi,

I have a test box which I would like to monitor CPU usage and run queue
during the day.

I don't want to buy any 3rd party tool, if I can do it easily, as I only
need to monitor the box's performance over a week.

I thought I could just create a .Net app or service with a timer that gets
back this data every so many minutes..

Can anyone help? Has anyone any example code..

Many thanks

JonesG

Nov 21 '05 #2
Jonesgj,
In addition to Gerry's suggestion, you can use WMI (Windows Management
Instrumentation) via the classes in the System.Management namespace.

Here is a recent MSDN article on WMI & .NET:

http://msdn.microsoft.com/vstudio/de...ml/vs04d6a.asp

Hope this helps
Jay

"Jonesgj" <g@btinternet.com> wrote in message
news:cj**********@hercules.btinternet.com...
Hi,

I have a test box which I would like to monitor CPU usage and run queue
during the day.

I don't want to buy any 3rd party tool, if I can do it easily, as I only
need to monitor the box's performance over a week.

I thought I could just create a .Net app or service with a timer that gets
back this data every so many minutes..

Can anyone help? Has anyone any example code..

Many thanks

JonesG

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Ang | last post by:
I was wondering if the CPU is the bottleneck. Hence I used the Performance Monitor to look up some values. Here are the results. Counter Scale Average ...
6
by: freakyfreak | last post by:
Does anyone have any basic, simple scripts of sp's that I can give my computer operators to use to monitor for serious conditions on our sql servers? We are new in the ms-sql arena, a small shop...
7
by: SQLDBA | last post by:
I am in the process of evaluating some SQL Performance Monitoring /DBA tool to purchase (For SQL Server 2000). I have the following list of software that I came across and have to finalize which...
1
by: Pawan | last post by:
Hi, Currently I am have an assignment which has to have some functionality which enables internet usage of client computers from a server machine. Basically this is an upgradation that I have to...
3
by: Ian Frawley | last post by:
Anyone used WMI to get stats back on MS SQL Server? Any good articles anywhere? -- Ian (Freebasing On Boredom.......) BEING IN THERAPY And yet, having therapy is very much like making love...
2
by: DataPro | last post by:
Our shop is expanding use of SQL Server, both 2000 and 2005. We have Litespeed on some boxes to handle the backup/recovery jobs. Can I ask what are considered the best tools for monitoring SQL...
4
by: natG | last post by:
Well folks, I didn't heed the warnings (that excessive monitoring, statistics, etc. can cause a performance hit) and I have been playing around with all kinds of monitors, snapshots, especially...
7
by: =?Utf-8?B?Q2FybG8gRm9saW5p?= | last post by:
Hi, I implemented asynchronous calls to a web resource (using HttpWebRequest) from asp.net 2.0. The request it's made asyncronously (I see that beginGetResponse returns immediately). The number...
2
by: RSL101 | last post by:
HI, I am hoping to get your opinions about "Tivoli Monitoring for Databases". Our enterprise environment is AIX/Red Hat Linux, running a lot of IBM P series and E servers. We need to monitor LUW...
0
by: RSL101 | last post by:
*This maybe dup msg. My earlier post never show up! I like to get your input regarding UDB performance monitoring. Our environment is IBM AIX and some linux running web applications open to US...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.