473,396 Members | 2,020 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,396 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 2498
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.