Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

xen.ps1 - powershell script by ActiveXperts Software

xen.ps1 checks a counter on a Xen server or a Virtual Maching running on a Xen server.

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


xen.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
#     Xen.ps1
# Description:
#     This script checks a counter on a Xen server or a Virtual Maching running on a Xen 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)#     
#     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)
#     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 Xen 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 'Xen' check and add the credentials.)
#     2) strVM (string) - VirtualMachine to check, leave empty for Xen 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:
#     .\Xen.ps1 '<server>' '<VM>' <counterID> '<context>' '<operator>' <value> 
# Sample:
#     .\Xen.ps1 'xen02' '' 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: .\Xen.ps1 "<Hostname | IP>" "<VM>" <counterID> "<context>" "<operator>" <value>'
  echo $res
  exit
}

$objXen = new-object -comobject AxNetwork.Xen
$objServerCredentials = new-object -comobject ActiveXperts.NMXenCredentials

# 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( $objXen.isContextAllowed( $nCounterID ) -and $strContext -eq '' )
{
  $res = 'UNCERTAIN: Context expected for Counter[' + $nCounterID + ']'
  echo $res
  exit
}
elseif( ( -not $objXen.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 Xen credentials defined for [' + $strServer + ']. In the Manager application, create a "new Monitoring Check", Pick the "Xen ESXi" check and add the credentials.'
  echo $res
  exit
}

# Initialize the Xen object
$objXen.Initialize()

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

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

# Connect to Xen Server
$objXen.Connect()

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

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

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

  $res = 'UNCERTAIN: Unable to retrieve counter value of counter [' + $nCounterID + '] of VM [' + $strVM + '] on server [' + $strServer + '], reason: ' + $objXen.LastError + ' : ' + $objXen.GetErrorDescription( $objXen.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: ' + $objXen.GetCounterDescription($nCounterID) + ' ' + $nVMValue + ' IS ' + $strTranslaterOperator + ' ' + $nValue + ' ON[' + $strServerClient  + ']'
}
else
{
  $res = 'ERROR: ' + $objXen.GetCounterDescription($nCounterID) + ' ' + $nVMValue + ' IS NOT ' + $strTranslaterOperator + ' ' + $nValue + ' ON[' + $strServerClient  + ']'
}

echo $res

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


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

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