2

I'm looking for a function similar to Debug Diagnostics Collector.

Where you can setup a performance (or any counter) trigger (eg. above 50% CPU over 50 seconds). Once condition of a trigger are meet I would like to run a PS1 script.

Has anyone ever done something similar?

Luke
  • 123
  • 4

1 Answers1

1

not sure about the 'over 50 seconds' aspect, but you could poll to see if your CPU is over a certain limit.

Just a quick sketch in powershell...

# checks cpu threshold and runs script in $scriptName variable
function CPUthreshold
{
    # mandatory single variable in function for script name
    Param(
    [Parameter(Mandatory=$true)]
    [string]$scriptName
    )

    # cpu percentage limit
    $limit = 50

    # time to poll CPU limit in seconds
    $pollTimeSec = 60

    # check limit forever!
    while($true){
        # get the win32_processor object to get stats on the CPU
        $cpu =  Get-WmiObject win32_processor

        # check if the CPU is over our limit
        if ($cpu.LoadPercentage -gt $limit)
        {
            # call your script here!
            & $scriptName
        }

        # wait again until the next poll
        Start-Sleep -s $pollTimeSec
    }
}

# call function with script name you want to run
CPUthreshold .\Hello-World.ps1

you could run this in a thread, or background the process as well on the machine you are interested in.

Nathan McCoy
  • 200
  • 2
  • 11
  • 1
    Yes you are correct. I could technically get a PS script to do the sampling. However, in our case we have decided on using Nagios. – Luke Jun 02 '14 at 11:29
  • yes I think that is a good idea. nagios and icinga are widely used for this use case. – Nathan McCoy Jun 03 '14 at 16:00