Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
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 2 at 11:29
    
yes I think that is a good idea. nagios and icinga are widely used for this use case. –  son_of_fire Jun 3 at 16:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.