Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

cpu.ps1 - powershell script by ActiveXperts Software

cpu.ps1 checks CPU usage on a host.

Use cpu.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select cpu.ps1. Configure the required parameter, or press 'Load a working sample'.

In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.


cpu.ps1 script code

#################################################################################
# ActiveXperts Network Monitor PowerShell script.
# For more information about ActiveXperts Network Monitor, visit the ActiveXperts 
# Network Monitor web site at http://www.activexperts.com
#################################################################################
# Script
#     Cpu.ps1
# Description:
#     Checks CPU usage on a (remote) computer.
# Declare Parameters:
#     1) strHost (string) - Hostname or IP address of the computer you want to monitor
#     2) strCpu (string) - Either "_Total" or CPU "0", "1", or ...
#     3) nMaxCpuUsage (int) - Maximum allowed CPU usage (%)
#     4) strAltCredentials (string, optional) - Specify an empty string to use Network Monitor service credentials.
#         To use alternate credentials, enter a server that is defined in Server Credentials table.
#         (To define Server Credentials, choose Tools->Options->Server Credentials)
# Usage:
#     .\Cpu.ps1 '<Hostname | IP>' '<[_Total|0|1|2|...]>' <nMaxCpuUsage> '[alt-credentials]'
# Sample:
#     .\Cpu.ps1 'localhost' '_Total' 60
#################################################################################

# -- Declare Parameters
param( [string]$strHost = '', [string]$strCpu = '', [int]$nMaxCpuUsage = -1, [string]$strAltCredentials = '' )

# -- Use _activexperts.ps1 with common functions
. 'C:\Program Files\ActiveXperts\Network Monitor\Scripts\Monitor (ps1)\_activexperts.ps1'


#################################################################################
# // --- Main script ---
#################################################################################

# -- Clear screen and clear error
cls
$Error.Clear()

# -- Validate parameters, return on parameter mismatch
if( $strHost -eq '' -or $strCpu -eq '' -or $nMaxCpuUsage -lt 0 )
{
  $res = 'UNCERTAIN: Parameter error - Usage: .\CPU.ps1 "<Hostname | IP>" <[_Total|0|1|2|...]> <nMaxCpuUsage> "[alt-credentials]"'
  echo $res
  exit
}

# -- Declare local variables by assigning an initial value to it
$strExplanation = ''
$objAltCredentials = $null

# If alternate credentials are specified, retrieve the alternate login and password from the ActiveXperts global settings
if( $strAltCredentials -ne '' )
{
  # Get the Alternate Credentials object. Function "AxGetCredentials" is implemented in "activexperts.ps1"
  if( ( AxGetCredentials $strHost  $strAltCredentials  ([ref]$objAltCredentials) ([ref]$strExplanation) ) -ne $AXSUCCESS )
  {
    echo $strExplanation
    exit
  }
}

# -- Get WMI object
if( $objAltCredentials -eq $null )
{
  $nCpuLoadPercentage = ( Get-WmiObject -ComputerName $strHost -ErrorVariable Error -ErrorAction SilentlyContinue -class Win32_PerfFormattedData_PerfOS_Processor -filter ("name='" + $strCpu + "'") ).PercentProcessorTime 
}
else
{
  $nCpuLoadPercentage = ( Get-WmiObject -ComputerName $strHost -ErrorVariable Error -ErrorAction SilentlyContinue -Credential $objAltCredentials -class Win32_PerfFormattedData_PerfOS_Processor -filter ("name='" + $strCpu + "'") ).PercentProcessorTime 
}

# If anything went wrong Powershell sets the error in the global array $Error
if( $Error -ne '' )
{
  $res = 'UNCERTAIN: ' + $Error
  echo $res
  exit
} 

if( $nCpuLoadPercentage -gt $nMaxCpuUsage )
{
  $res = 'ERROR: '
}
else
{
  $res = 'SUCCESS: '
}

# -- Print script result
$res = $res + 'CPU usage=[' + $nCpuLoadPercentage + '%], maximum allowed=[' + $nMaxCpuUsage + '%] DATA:' + $nCpuLoadPercentage
echo $res
exit


#################################################################################
# // --- Catch script exceptions ---
#################################################################################

trap [Exception]
{
  $res = 'UNCERTAIN: ' + $_.Exception.Message
  echo $res
  exit
}