Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

snmp.ps1 - powershell script by ActiveXperts Software

snmp.ps1 connects to the SNMP agent, reads the OID value and performs a match.

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


snmp.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
#     Snmp.ps1
# Description: 
#     Connect to the (remote) SNMP agent, read the OID value and match it
#     This function uses ActiveXperts Network Component.
#     ActiveXperts Network Component is automatically licensed when ActiveXperts Network Monitor is purchased.
#     For more information about ActiveXperts Network Component, see: www.activexperts.com/network-component
# Declare Parameters:
#     1) strHost (string) - Host name or IP address of the (remote) SNMP agent
#     2) strCommunity (string) - Community string. Default: 'public'
#     3) strOID (string) - Retrieve value of this OID. 
#     4) strExpectedValue (string) - Match retrieved value against this expected value
# Usage:
#     .\SNMP.ps1 '<Hostname | IP>' '<Community name' '<x.x.x.x.x....>' '<Expected value>'
# Sample:
#     .\SNMP.ps1 'localhost' 'public' '1.3.6.1.2.1.1.5.0' 'My Computer Name'
#################################################################################

# -- Declare Parameters
param( [string]$strHost = '', [string]$strCommunity = '', [string]$strOID = '', [string]$strExpectedValue = '' )

# -- 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 $strCommunity -eq '' -or $strOID -eq '' -or $strExpectedValue -eq '' )
{
  $res = 'UNCERTAIN: Invalid number of parameters - Usage: .\SNMP.ps1 "<Hostname | IP>" "<community name>" "<x.x.x.x.x....>" "<expected value>"'
  echo $res
  exit
}

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

# Create instance of SNMP object
$objSnmpManager = new-object -comobject AxNetwork.SnmpManager
$objConstants = new-object -comobject AxNetwork.NwConstants

# Initialize SNMP object
$objSnmpManager.ProtocolVersion = $objConstants.nwSNMP_VERSION_V2C
$objSnmpManager.Initialize()

if( $objSnmpManager.LastError -ne 0 )
{
  $res = 'UNCERTAIN: Unable to initialize ActiveXperts Network Component SNMP object'
  echo $res
  exit
}  

# Open connection to (remote) SNMP agent
$objSnmpManager.Open( $strHost, $strCommunity )
if( $objSnmpManager.LastError -ne 0 )
{
  $res = 'UNCERTAIN: Unable to connect to agent [' + $strHost + '] in [' + $strCommunity + '] community. Be sure SNMP agent is running'
  echo $res
  exit
}  

$objSnmpObject = $objSnmpManager.Get( $strOID )
if( $objSnmpManager.LastError -ne 0 )
{
  $res = 'UNCERTAIN: Unable to retrieve [' + $strOID + ']. . Be sure SNMP agent is running'
  echo $res
  exit
}

if( $objSnmpObject.value.ToUpper() -eq $strExpectedValue.ToUpper() )
{
  $res = 'SUCCESS: Value read: [' + $objSnmpObject.value + '], expected:[' + $strExpectedValue + '], Type:[' + $objSnmpObject.Type + ']'
}  
else
{
  $res = 'ERROR: Value read: [' + $objSnmpObject.value + '], expected:[' + $strExpectedValue + '], Type:[' + $objSnmpObject.Type + ']'
}  


# Note: type ID#s are described in ActiveXperts Network Component manual, and included in ActiveXperts Network Component samples.

$objSnmpManager.Close()
$objSnmpManager.Shutdown()

echo $res
exit


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

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