none
get-counter Show any processes greater than 20% CPU usage.

    Question

  • I created the script below to show any processes greater than 20% CPU usage.

    The script was created with a variable ($procpercent) which sets the "greater than" condition. The problem is that the script runs and it shows all the proccesses where the CPU usage is greater than "0" percent, instead of just the processes that are using more than 20%.

    Seems like the "$_.cookedvalue " variable is not set to a numeral value in "where {$_.cookedvalue -gt $procpercent}", but Im not sure how to make that work.

    Any help is appreciated.

    #global variables
    
    [int]$procpercent = 20
    
    $servers = $args[0]
    
    
    $customtable = @{Expression={$_.InstanceName};Label="Process_Name";width=9}, @{Expression={$_.CookedValue/$env:NUMBER_OF_PROCESSORS -as [int]};Label="CPU_usage_%"}
    
    
    foreach ($servers in $servers){
    
    write-host "   " -background "green"
    
    Write-Host $servers
    
    $Proc = Get-counter -ComputerName $servers "\Process(*)\% processor time"
    
    $procresult = $Proc.CounterSamples | where {$_.cookedvalue -gt $procpercent} | where {$_.instanceName -ne "idle"} | where {$_.instanceName -ne "_total"} | Format-Table $customtable -auto
    
    if ($procresult -eq $null){
    
    write-host "No Processes with CPU usage greater than $procpercent %"
    
    } else {
    
    $procresult
    
    }
    
    
    
    }
    
    
    Monday, August 30, 2010 8:21 PM

Answers

  • I figured out my problem.

    The code above, works perfectly on single CPU systems.

    On multi CPU systems, the process CPU time needs to be divided into the number of CPUs.

    this works now:

    =============================================================

    if ($env:NUMBER_OF_PROCESSORS -eq 2){
    $procpercent = 50                  #represents 25 % of the CPU usage on a 2 CPU system
    } elseif ($env:NUMBER_OF_PROCESSORS -eq 4){
    $procpercent = 100                 #represents 25 % of the CPU usage on a 4 CPU system
    }

    $servers = $args[0]


    $customtable = @{Expression={$_.InstanceName};Label="Process_Name";width=9}, @{Expression={$_.CookedValue/$env:NUMBER_OF_PROCESSORS -as [int]};Label="CPU_usage_%"}


    foreach ($server in $servers){
    write-host "            " -background "green"
    Write-Host $server
    $Proc = Get-counter -ComputerName $server "\Process(*)\% processor time"
    $procresult = $Proc.CounterSamples | where {$_.cookedvalue -ge $procpercent} | where {$_.instanceName -ne "idle"} | where {$_.instanceName -ne "_total"}
    if ($procresult -eq $null){

    $procpercentconvert = $procpercent/$env:NUMBER_OF_PROCESSORS
    write-host "No Processes with CPU usage greater than $procpercentconvert %"
    } else {
    $procresult | Format-Table $customtable -auto
    }

    }

    =============================================

    • Marked as answer by lgpana Tuesday, August 31, 2010 5:55 PM
    Tuesday, August 31, 2010 5:55 PM

All replies

  • This should work, I have removed the FT from the $procresult variable, and moved it within the else

     

    #global variables

    [int]$procpercent = 20

    $servers = $args[0]


    $customtable = @{Expression={$_.InstanceName};Label="Process_Name";width=9}, @{Expression={$_.CookedValue/$env:NUMBER_OF_PROCESSORS -as [int]};Label="CPU_usage_%"}


    foreach ($servers in $servers){

    write-host "   " -background "green"

    Write-Host $servers

    $Proc = Get-counter -ComputerName $servers "\Process(*)\% processor time"
     
    $procresult = $Proc.CounterSamples | where {$_.cookedvalue -gt $procpercent} | where {$_.instanceName -ne "idle"} | where {$_.instanceName -ne "_total"}


    if ($procresult -eq $null){

    write-host "No Processes with CPU usage greater than $procpercent %"

    } else {

    $procresult | Format-Table $customtable -auto

    }
    }

    Tuesday, August 31, 2010 12:46 AM
  • that did not work.

    This is the result that Im getting:

    ========================

    Server1

    Process_Name             CPU_usage_%
    ------------                      -----------
    splunkd                                   13
    tomcat_site1                            25

    ========================

    Based on the script above, the "splunkd" process should not be listed.

    Tuesday, August 31, 2010 12:22 PM
  • foreach ($servers in $servers) change it to foreach ($server in $servers)

    and change your reference from $servers to $server in your loop

     

    Tested on 2k3 and XP with Powershell v2.0

    Tuesday, August 31, 2010 4:02 PM
  • I figured out my problem.

    The code above, works perfectly on single CPU systems.

    On multi CPU systems, the process CPU time needs to be divided into the number of CPUs.

    this works now:

    =============================================================

    if ($env:NUMBER_OF_PROCESSORS -eq 2){
    $procpercent = 50                  #represents 25 % of the CPU usage on a 2 CPU system
    } elseif ($env:NUMBER_OF_PROCESSORS -eq 4){
    $procpercent = 100                 #represents 25 % of the CPU usage on a 4 CPU system
    }

    $servers = $args[0]


    $customtable = @{Expression={$_.InstanceName};Label="Process_Name";width=9}, @{Expression={$_.CookedValue/$env:NUMBER_OF_PROCESSORS -as [int]};Label="CPU_usage_%"}


    foreach ($server in $servers){
    write-host "            " -background "green"
    Write-Host $server
    $Proc = Get-counter -ComputerName $server "\Process(*)\% processor time"
    $procresult = $Proc.CounterSamples | where {$_.cookedvalue -ge $procpercent} | where {$_.instanceName -ne "idle"} | where {$_.instanceName -ne "_total"}
    if ($procresult -eq $null){

    $procpercentconvert = $procpercent/$env:NUMBER_OF_PROCESSORS
    write-host "No Processes with CPU usage greater than $procpercentconvert %"
    } else {
    $procresult | Format-Table $customtable -auto
    }

    }

    =============================================

    • Marked as answer by lgpana Tuesday, August 31, 2010 5:55 PM
    Tuesday, August 31, 2010 5:55 PM