Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Monitoring processor usage - thoughts

Closed Thread
 
Thread Tools Display Modes
Old 12-22-2006   #1 (permalink)
Junior Member


  rockmoose is offline

Monitoring processor usage - thoughts

Hi all,

I wonder if you guys have any thoughts about the following.

I set up a scheduled tasks that every minute polls for the processorload.
The server is not very busy atm, but when this script runs from the scheduled task we get a small spike which in turns make the statistics unreliable.

But this is not always the case, for instance if I start the scheduled task manually consecutive times, I don't get the same spikes in processor usage.

(spike = appx 50%, when the gwmi poll gets done in ps via scheduled task)


My questions are;
When running a powershell script like this as a scheduled tasks,
will a lot of cpu be consumed for starting up powershell? or maybe for initializing the database connections? will this scew my statistics?

rockmoose


Below is the script, (I enclose it in [php] tags, hopefully it will make it formatted...)
This is how I call it from the scheduled task:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe "D:\tasks\Monitor\processor.ps1"


# monitors processor usage

$processor Get-WmiObject -class Win32_Processor -Property DeviceID,LoadPercentage
for($i 0$i –lt $processor.Count$i++)
{
    
#@($computer["Name"],$processor[$i].DeviceID,$processor[$i].LoadPercentage)
    
    
$cn = new-object system.data.SqlClient.SqlConnection("Data Source=srvname;Integrated Security=SSPI;Initial Catalog=dbname");
    
$cmd = new-object system.Data.Sqlclient.SqlCommand("monitor_performanceCounterValue_add"$cn);

    
$cmd.CommandType = [System.Data.CommandType]"StoredProcedure"
    
$cmd.Parameters.AddWithValue("@object","Processor") | Out-Null
    $cmd
.Parameters.AddWithValue("@instance",$processor[$i].DeviceID) | Out-Null
    $cmd
.Parameters.AddWithValue("@counter","LoadPercentage") | Out-Null
    $cmd
.Parameters.AddWithValue("@computer","srvname") | Out-Null
    $cmd
.Parameters.AddWithValue("@value",[decimal]$processor[$i].LoadPercentage) | Out-Null

    $cn
.Open()
    
$cmd.ExecuteNonQuery() | Out-Null
    $cn
.Close()


Old 12-22-2006   #2 (permalink)
Junior Member


  rockmoose is offline

Just tested some more.

If I execute the scheduled task manually by rightclicking and choosing "run", I get 0-10% processor load.
The seconds after when the scheduled task is run according to schedule the processor load reports 30-60%.

So I don't really know where all these cpu-cycles are consumed.
The nt-scheduler?
The powershell engine?
.NET framework?
Db-connections?

............................

Ok, when starting the script, taskmanager shows powershell using 15-20% cpu for a brief moment.
Maybe the moment of the poll is slightly different when running the scheduled task manually contra scheduled.. ?


This might not be an ideal use of ps, but on of my first attempts :-)
---- pretty lousy when coming to think of it ----

rockmoose
Old 12-22-2006   #3 (permalink)
RichS
Guest


 

RE: Monitoring processor usage - thoughts

Try running it from another machine. WMI can connect to a remote machine.
Use the -computername parameter to specify the machine.

That way the additional CPU usage of starting PowerShell won't impact your
results
--
Richard Siddaway

Please note that all scripts are supplied "as is" and with no warranty


"rockmoose" wrote:

>
> Hi all,
>
> I wonder if you guys have any thoughts about the following.
>
> I set up a scheduled tasks that every minute polls for the
> processorload.
> The server is not very busy atm, but when this script runs from the
> scheduled task we get a small spike which in turns make the statistics
> unreliable.
>
> But this is not always the case, for instance if I start the scheduled
> task manually consecutive times, I don't get the same spikes in
> processor usage.
>
> (spike = appx 50%, when the gwmi poll gets done in ps via scheduled
> task)
>
>
> My questions are;
> When running a powershell script like this as a scheduled tasks,
> will a lot of cpu be consumed for starting up powershell? or maybe for
> initializing the database connections? will this scew my statistics?
>
> rockmoose
>
>
> Below is the script, (I enclose it in
> PHP code:
> --------------------
> tags, hopefully it will make it formatted...)
> This is how I call it from the scheduled task:
> C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe "D:\tasks\Monitor\processor.ps1"
>
>
>
> PHP code:
> --------------------
> # monitors processor usage
>
> $processor = Get-WmiObject -class Win32_Processor -Property DeviceID,LoadPercentage
> for($i = 0; $i –lt $processor.Count; $i++)
> {
> #@($computer["Name"],$processor[$i].DeviceID,$processor[$i].LoadPercentage)
>
> $cn = new-object system.data.SqlClient.SqlConnection("Data Source=srvname;Integrated Security=SSPI;Initial Catalog=dbname");
> $cmd = new-object system.Data.Sqlclient.SqlCommand("monitor_performanceCounterValue_add", $cn);
>
> $cmd.CommandType = [System.Data.CommandType]"StoredProcedure"
> $cmd.Parameters.AddWithValue("@object","Processor") | Out-Null
> $cmd.Parameters.AddWithValue("@instance",$processor[$i].DeviceID) | Out-Null
> $cmd.Parameters.AddWithValue("@counter","LoadPercentage") | Out-Null
> $cmd.Parameters.AddWithValue("@computer","srvname") | Out-Null
> $cmd.Parameters.AddWithValue("@value",[decimal]$processor[$i].LoadPercentage) | Out-Null
>
> $cn.Open()
> $cmd.ExecuteNonQuery() | Out-Null
> $cn.Close()
>
> }
> --------------------
>
>
> --
> rockmoose
> ------------------------------------------------------------------------
> rockmoose's Profile: http://vista64.net/forums/member.php?userid=1022
> View this thread: http://vista64.net/forums/showthread.php?t=30140
>
>

Old 12-22-2006   #4 (permalink)
/\/\o\/\/ [MVP]
Guest


 

RE: Monitoring processor usage - thoughts

you can also use performancecounters for this (also remote)

http://mow001.blogspot.com/2005/12/g...info-from.html

Greetings /\/\o\/\/


"RichS" wrote:

> Try running it from another machine. WMI can connect to a remote machine.
> Use the -computername parameter to specify the machine.
>
> That way the additional CPU usage of starting PowerShell won't impact your
> results
> --
> Richard Siddaway
>
> Please note that all scripts are supplied "as is" and with no warranty
>
>
> "rockmoose" wrote:
>
> >
> > Hi all,
> >
> > I wonder if you guys have any thoughts about the following.
> >
> > I set up a scheduled tasks that every minute polls for the
> > processorload.
> > The server is not very busy atm, but when this script runs from the
> > scheduled task we get a small spike which in turns make the statistics
> > unreliable.
> >
> > But this is not always the case, for instance if I start the scheduled
> > task manually consecutive times, I don't get the same spikes in
> > processor usage.
> >
> > (spike = appx 50%, when the gwmi poll gets done in ps via scheduled
> > task)
> >
> >
> > My questions are;
> > When running a powershell script like this as a scheduled tasks,
> > will a lot of cpu be consumed for starting up powershell? or maybe for
> > initializing the database connections? will this scew my statistics?
> >
> > rockmoose
> >
> >
> > Below is the script, (I enclose it in
> > PHP code:
> > --------------------
> > tags, hopefully it will make it formatted...)
> > This is how I call it from the scheduled task:
> > C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe "D:\tasks\Monitor\processor.ps1"
> >
> >
> >
> > PHP code:
> > --------------------
> > # monitors processor usage
> >
> > $processor = Get-WmiObject -class Win32_Processor -Property DeviceID,LoadPercentage
> > for($i = 0; $i –lt $processor.Count; $i++)
> > {
> > #@($computer["Name"],$processor[$i].DeviceID,$processor[$i].LoadPercentage)
> >
> > $cn = new-object system.data.SqlClient.SqlConnection("Data Source=srvname;Integrated Security=SSPI;Initial Catalog=dbname");
> > $cmd = new-object system.Data.Sqlclient.SqlCommand("monitor_performanceCounterValue_add", $cn);
> >
> > $cmd.CommandType = [System.Data.CommandType]"StoredProcedure"
> > $cmd.Parameters.AddWithValue("@object","Processor") | Out-Null
> > $cmd.Parameters.AddWithValue("@instance",$processor[$i].DeviceID) | Out-Null
> > $cmd.Parameters.AddWithValue("@counter","LoadPercentage") | Out-Null
> > $cmd.Parameters.AddWithValue("@computer","srvname") | Out-Null
> > $cmd.Parameters.AddWithValue("@value",[decimal]$processor[$i].LoadPercentage) | Out-Null
> >
> > $cn.Open()
> > $cmd.ExecuteNonQuery() | Out-Null
> > $cn.Close()
> >
> > }
> > --------------------
> >
> >
> > --
> > rockmoose
> > ------------------------------------------------------------------------
> > rockmoose's Profile: http://vista64.net/forums/member.php?userid=1022
> > View this thread: http://vista64.net/forums/showthread.php?t=30140
> >
> >

Old 12-22-2006   #5 (permalink)
Rob Campbell
Guest


 

RE: Monitoring processor usage - thoughts

For that kind of monitoring interval, I'd think you want to write the script
as loop with a one minute sleep interval, then start one instance and leave
it running.

"rockmoose" wrote:

>
> Hi all,
>
> I wonder if you guys have any thoughts about the following.
>
> I set up a scheduled tasks that every minute polls for the
> processorload.
> The server is not very busy atm, but when this script runs from the
> scheduled task we get a small spike which in turns make the statistics
> unreliable.
>
> But this is not always the case, for instance if I start the scheduled
> task manually consecutive times, I don't get the same spikes in
> processor usage.
>
> (spike = appx 50%, when the gwmi poll gets done in ps via scheduled
> task)
>
>
> My questions are;
> When running a powershell script like this as a scheduled tasks,
> will a lot of cpu be consumed for starting up powershell? or maybe for
> initializing the database connections? will this scew my statistics?
>
> rockmoose
>
>
> Below is the script, (I enclose it in
> PHP code:
> --------------------
> tags, hopefully it will make it formatted...)
> This is how I call it from the scheduled task:
> C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe "D:\tasks\Monitor\processor.ps1"
>
>
>
> PHP code:
> --------------------
> # monitors processor usage
>
> $processor = Get-WmiObject -class Win32_Processor -Property DeviceID,LoadPercentage
> for($i = 0; $i –lt $processor.Count; $i++)
> {
> #@($computer["Name"],$processor[$i].DeviceID,$processor[$i].LoadPercentage)
>
> $cn = new-object system.data.SqlClient.SqlConnection("Data Source=srvname;Integrated Security=SSPI;Initial Catalog=dbname");
> $cmd = new-object system.Data.Sqlclient.SqlCommand("monitor_performanceCounterValue_add", $cn);
>
> $cmd.CommandType = [System.Data.CommandType]"StoredProcedure"
> $cmd.Parameters.AddWithValue("@object","Processor") | Out-Null
> $cmd.Parameters.AddWithValue("@instance",$processor[$i].DeviceID) | Out-Null
> $cmd.Parameters.AddWithValue("@counter","LoadPercentage") | Out-Null
> $cmd.Parameters.AddWithValue("@computer","srvname") | Out-Null
> $cmd.Parameters.AddWithValue("@value",[decimal]$processor[$i].LoadPercentage) | Out-Null
>
> $cn.Open()
> $cmd.ExecuteNonQuery() | Out-Null
> $cn.Close()
>
> }
> --------------------
>
>
> --
> rockmoose
> ------------------------------------------------------------------------
> rockmoose's Profile: http://vista64.net/forums/member.php?userid=1022
> View this thread: http://vista64.net/forums/showthread.php?t=30140
>
>

Old 12-22-2006   #6 (permalink)
Junior Member


  rockmoose is offline

> "Try running it from another machine. WMI can connect to a remote machine.
Use the -computername parameter to specify the machine.
"

That was my original plan, unfortunately I had som problems with some of the counters, so I opted to run the ps scripts locally and send the results to the db.
(http://www.vista64.net/forums/showthread.php?t=29567)


>"you can also use performancecounters for this (also remote)"

Thank you, is there any difference between the two approaches?
Win32_Processor or Win32_PerfFormattedData_PerfProc_Process

>"For that kind of monitoring interval, I'd think you want to write the script
as loop with a one minute sleep interval, then start one instance and leave
it running."

Ok, I will try that, just need to figure out how to write the sleep & loop function.
But would Thread.Sleep work in a scripting environment?
I'll try it, but does anybody know? or does ps have an inbuilt sleep function?


rockmoose
Old 12-22-2006   #7 (permalink)
Junior Member


  rockmoose is offline

Here is the revised script.
This is working ok now.

# monitors processor usage

$pollIntervallSeconds 10

# initialize variables and db connection
$cn = new-object system.data.SqlClient.SqlConnection("Data Source=srvName;Integrated Security=SSPI;Initial Catalog=dbName");
$cmd = new-object system.Data.Sqlclient.SqlCommand("monitor_performanceCounterValue_add"$cn);

$cmd.CommandType = [System.Data.CommandType]"StoredProcedure"

$cmd.Parameters.Add("@object",[System.Data.SqlDbType]"VarChar",80) | Out-Null
$cmd
.Parameters.Add("@instance",[System.Data.SqlDbType]"VarChar",8) | Out-Null
$cmd
.Parameters.Add("@counter",[System.Data.SqlDbType]"VarChar",32) | Out-Null
$cmd
.Parameters.Add("@computer",[System.Data.SqlDbType]"VarChar",20) | Out-Null
$cmd
.Parameters.Add("@value",[System.Data.SqlDbType]"Decimal") | Out-Null
$cmd
.Parameters["@value"].Precision 11
$cmd
.Parameters["@value"].Scale 2

$cmd
.Parameters["@object"].Value "Processor"
$cmd.Parameters["@counter"].Value "LoadPercentage"
$cmd.Parameters["@computer"].Value "machineName"

# continuosly loop and poll processor load
while($true)
{
    
$processor Get-WmiObject -class Win32_Processor -Property DeviceID,LoadPercentage
    
for($i 0$i –lt $processor.Count; $i++)
    
{
        
$cmd.Parameters["@instance"].Value $processor[$i].DeviceID
        $cmd
.Parameters["@value"].Value = [decimal]$processor[$i].LoadPercentage

        $cn
.Open()
        
$cmd.ExecuteNonQuery() | Out-Null
        $cn
.Close()
    }
    
$processor $null
    Start
-Sleep -s $pollIntervallSeconds

Any opinions on whic method is preferred to get the processor load counter:
Win32_Processor or Win32_PerfFormattedData_PerfProc_Process?

Another thing,
If the process is terminated, is there any way of having something like a "finally" block to close the database connection?

rockmoose
Old 12-22-2006   #8 (permalink)
Lucvdv
Guest


 

Re: Monitoring processor usage - thoughts

On Fri, 22 Dec 2006 12:37:41 +0000, rockmoose
<rockmoose.2j7x6p@no-mx.vista64.net> wrote:

>
> Hi all,
>
> I wonder if you guys have any thoughts about the following.
>
> I set up a scheduled tasks that every minute polls for the
> processorload.
> The server is not very busy atm, but when this script runs from the
> scheduled task we get a small spike which in turns make the statistics
> unreliable.


The CLR forms a (heavy) bit of a load when a .Net application starts.
It seems to be even worse when no other .Net apps are running at the
moment, although that difference could simply be a matter of files being in
disk cache or not.


If I wanted to monitor performance of the local machine, I would write a
small (compiled) app that can be kept running. I would definitely *not*
use the task scheduler to start an application for each sample.

It's like launching the task manager, and looking only at the first CPU use
sample of the graph: always 100%.
Old 12-22-2006   #9 (permalink)
Rob Campbell
Guest


 

Re: Monitoring processor usage - thoughts



Powershell has a start-sleep cmdlet.

"rockmoose" wrote:

>
> -> "Try running it from another machine. WMI can connect to a remote
> machine.
> Use the -computername parameter to specify the machine.--"
>
> -That was my original plan, unfortunately I had som problems with some
> of the counters, so I opted to run the ps scripts locally and send the
> results to the db.
> (http://www.vista64.net/forums/showthread.php?t=29567)
>
> -
> >"you can also use performancecounters for this (also remote)"

>
> -Thank you, is there any difference between the two approaches?
> Win32_Processor or Win32_PerfFormattedData_PerfProc_Process
>
> ->"For that kind of monitoring interval, I'd think you want to write
> the script
> as loop with a one minute sleep interval, then start one instance and
> leave
> it running."
>
> -Ok, I will try that, just need to figure out how to write the sleep &
> loop function.
> But would Thread.Sleep work in a scripting environment?
> I'll try it, but does anybody know? or does ps have an inbuilt sleep
> function?
>
>
> rockmoose
>
>
> --
> rockmoose
> ------------------------------------------------------------------------
> rockmoose's Profile: http://vista64.net/forums/member.php?userid=1022
> View this thread: http://vista64.net/forums/showthread.php?t=30140
>
>

Old 12-22-2006   #10 (permalink)
Junior Member


  rockmoose is offline

" If I wanted to monitor performance of the local machine, I would write a
small (compiled) app that can be kept running. I would definitely *not*
use the task scheduler to start an application for each sample."

Yes, agreed. but one of the strong points about scripting is that you don't need to compile and deploy much.
And the first attempt was, umh, pretty mediocre.
The loop approach in the script seems to work very well.

" Powershell has a start-sleep cmdlet."

Found it thanks!

rockmoose
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Probably a simple question about disk/processor usage lovelyshadeofgreen PowerShell 1 1 Week Ago 05:14 AM
Need some thoughts on what to do! MrBiggz Sound & Audio 4 12-27-2007 03:37 PM
Thoughts So Far Verger Vista installation & setup 3 08-19-2007 06:04 AM
DPCs causing 100% processor usage at random times sebbelcher Vista performance & maintenance 1 07-13-2007 10:02 AM
100% Processor usage - svchost.exe Tim Vista performance & maintenance 3 07-07-2006 11:13 AM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50