Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

vmware.ps1 - powershell script by ActiveXperts Software

vmware.ps1 checks a counter on a VMware host or a VMware Virtual Machine.

Use vmware.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select vmware.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.


vmware.ps1 script code

#################################################################################
# ActiveXperts Network Monitor PowerShell script, © ActiveXperts Software B.V.
# For more information about ActiveXperts Network Monitor, visit the ActiveXperts 
# Network Monitor web site at http://www.activexperts.com
#################################################################################
# Script
#     VMware.ps1
# Description:
#     This script checks a counter on a VMware server or a Virtual Machine running on a VMware server
#     The following Counters are available:
#     100:  CPU usage in percent (Use Context: NO)
#     200: 	Memory usage in percent (Use Context: NO)
#     201: 	Available memory in MB (Use Context: NO)
#     202: 	Memory used in MB (Use Context: NO)
#     300: 	Received packets during last interval (Use Context: YES)
#     301: 	Transmitted packets during last interval (Use Context: YES)
#     350: 	Current Network Receiving rate in kbps (Use Context: YES)
#     351: 	Current Network Transmitting rate in kbps (Use Context: YES)
#     400: 	Current Disk usage in percent (Use Context: YES)
#     401: 	Available disk space in Mb (Use Context: YES)
#     402: 	Used disk space in Mb (Use Context: YES)
#     500: 	Determine whether virtual machine is running (Use Context: NO)
#           Values:
#                 0: Not Running
#                 1: Resetting
#                 2: Running
#                 3: Shutting Down
#                 4: Standby
#                 5: Unknown
#     501: 	Determine whether virtual machine is powered (Use Context: NO)
#           Values:
#                 0: Off
#                 1: On
#                 2: Suspended
#     502: 	Determine whether VM Guest Tools is running on Virtual Machine (Use Context: NO)
#           Values:
#                 0: Not Running
#                 1: Running
# Declare Parameters:
#     1) strServer (string)  - Hostname or IP address of the VMware Server you want to monitor (Server name will be used as ServerCredentials aswell)
#              (To define Server Credentials, choose in the Manager application, create a 'new Monitoring Check', Pick the 'VMware ESXi' check and add the credentials.)
#     2) strVM (string) - VirtualMachine to check, leave empty for VMware Server
#     3) nCounterID (int) - The counter you want to check (See Description for ID's)
#     4) strContext (string) - The context, leave empty if context is not allowed (See Description)
#     5) strOperator (string) - The operator e.g.: -eq, -lt, -gt, -ge, -le, -ne
#     6) nValue (int) - The Value
# Usage:
#     .\VMware.ps1 '<Hostname | IP>' '<VM>' <counterID> '<context>' '<operator>' <value> 
# Sample:
#     .\VMware.ps1 'esx01' '' 100 '' '-lt' 70
#################################################################################

# -- Declare Parameters
param( [string]$strServer = '', [string]$strVM = '', [int]$nCounterID = -1, [string]$strContext = '', [string]$strOperator = '', [int]$nValue = -1 )

# -- 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 $strVM -eq '' -or $nCounterID -lt 0 -or  $strOperator -eq '' -or $nValue -lt 0 )
{
  $res = 'UNCERTAIN: Parameter error - Usage: .\VMware.ps1 "<Hostname | IP>" "<VM>" <counterID> "<context>" "<operator>" <value>'
  echo $res
  exit
}

$objVMware = new-object -comobject AxNetwork.VMware
$objServerCredentials = new-object -comobject ActiveXperts.NMVMwareCredentials

# Check CounterID
if( -not ( AxIsCounterIDValid( $nCounterID ) ) )
{
  $res = 'UNCERTAIN: Invalid Counter specified please see description of this check to see all the available counters'
  echo $res
  exit
}

# Check Operator
if( -not ( AxIsOperatorValid( $strOperator ) ) )
{
  $res = 'UNCERTAIN: Invalid Operator specified[' + $strOperator + ']. Please use one of the following operators: -eq, -lt, -gt, -ge, -le, -ne'
  echo $res
  exit
}

# Check if Context is allowed or required
if( $objVMware.isContextAllowed( $nCounterID ) -and $strContext -eq '' )
{
  $res = 'UNCERTAIN: Context expected for Counter[' + $nCounterID + ']'
  echo $res
  exit
}
elseif( ( -not $objVMware.isContextAllowed( $nCounterID ) ) -and $strContext -ne '' )
{
  $res = 'UNCERTAIN: Context is not allowed for Counter[' + $nCounterID + ']'
  echo $res
  exit
}

# Get credentials and check if credentials exists
$strAccount = $objServerCredentials.GetLogin( $strServer )
$strPassword = $objServerCredentials.GetPassword( $strServer )

if( $strAccount -eq '' )
{
  $res  = 'UNCERTAIN: No VMware credentials defined for [' + $strServer + ']. In the Manager application, create a "new Monitoring Check", Pick the "VMware ESXi" check and add the credentials.'
  echo $res
  exit
}

# Initialize the VMware object
$objVMware.Initialize()

if( $objVMware.LastError -ne 0 )
{
  $res = 'UNCERTAIN: Unable to Initialize VMware object, reason: ' + $objVMware.LastError + ' : ' + $objVMware.GetErrorDescription( $objVMware.LastError )
  echo $res
  exit
}

$objVMware.Server = $strServer
$objVMware.ServerAccount = $strAccount
$objVMware.ServerPassword = $strPassword

# Connect to VMware Server
$objVMware.Connect()

if( $objVMware.LastError -ne 0 )
{
  $objVMware.Disconnect()
  $objVMware.Shutdown()
  
  $res = 'UNCERTAIN: Unable to Connect to server[' + $strServer + '], reason: ' + $objVMware.LastError + ' : ' + $objVMware.GetErrorDescription( $objVMware.LastError )
  echo $res
  exit
}

# Get counter value
$nVMValue = $objVMware.GetPerfData( $strVM, $nCounterID, $strContext )

if( $objVMware.LastError -ne 0 )
{
  $objVMware.Disconnect()
  $objVMware.Shutdown()

  $res = 'UNCERTAIN: Unable to retrieve counter value of counter [' + $nCounterID + '] of VM [' + $strVM + '] on server [' + $strServer + '], reason: ' + $objVMware.LastError + ' : ' + $objVMware.GetErrorDescription( $objVMware.LastError )
  echo $res
  exit
}

$strTranslaterOperator = AxTranslateOperatorToText( $strOperator )
if( $strVM -eq '' )
{
  $strServerClient = $strServer
} 
else
{
  $strServerClient = $strVM
}

# Compare user given value with the Counter value to see if it meets the user given criteria
if( compareValue $strOperator $nVMValue $nValue )
{
  $res = 'SUCCESS: ' + $objVMware.GetCounterDescription($nCounterID) + ' ' + $nVMValue + ' IS ' + $strTranslaterOperator + ' ' + $nValue + ' ON[' + $strServerClient  + ']'
}
else
{
  $res = 'ERROR: ' + $objVMware.GetCounterDescription($nCounterID) + ' ' + $nVMValue + ' IS NOT ' + $strTranslaterOperator + ' ' + $nValue + ' ON[' + $strServerClient  + ']'
}

echo $res

# Disconnect en Shutdown the connection
$objVMware.Disconnect()
$objVMware.Shutdown()


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

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