What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Reading SNMP Octet String With Custom PowerShell Script Sensor

Votes:

0

The following Sensor can be used to read octet strings via SNMP, which PRTG cannot natively do. For an installation guideline, please check out the Guide for PowerShell-based Custom Sensors.

<#
.Synopsis
   Get the Status bits over SNMP from an 'Hewlett Packard' (HP) USV
.DESCRIPTION
   Get the Status bits over SNMP from an 'Hewlett Packard' (HP) USV

   Getting OID '1.3.6.1.4.1.232.11.2.10.1.0'
   Which returns a 'Octet String'

   Name of File: Get-PrtgSnmpHPUsvStatus.ps1

   This scriptfile must be placed into a subfolder of the PRTG program directory.
   For (.exe, .dll ) batch files (.cmd, .bat), VBS scripts (.vbs), or PowerShell scripts (.ps1).
   This is the following subfolder of the PRTG program directory:
   ("....\Custom Sensors\EXE")

   This Script uses the 'SNMP' Module from Author: Bartosz Bielawski (bartekb)
   https://www.powershellgallery.com/packages/SNMP/1.0.0.1

.NOTES
    Author: Peter Kriegel, Volkswagen AG, Wolfsburg

    Name of File: Get-PrtgSnmpHPUsvStatus.ps1
#>

$ErrorActionPreference = 'Stop'

Try {

    # Processing Script parameter (given Arguments)
    # giving first value of args an meaningful name and copy over the Value
    # we guess args[0] contains the Hostname (IP) %host from PRTG sensor
    $MyHost = $args[0]

    # load SNMP Module
    # taken from https://www.powershellgallery.com/packages/SNMP/1.0.0.1
    # Author: Bartosz Bielawski (bartekb)
    Import-Module -Name 'SNMP' -Force

    # used for SNMP examination of Device (not needful in PRTG sensor use, but leaved here to remember how to...)
    # Invoke-SnmpWalk -IP 192.178.1.10 -Community presslic -Version V1 -UDPport 161 -OIDStart '1.3.6.1.2.1.1.'


    # Get Data from USV
    # this OID returns an Octet String
    # Pretty complicated to decipher if you do not have the guide of the vendor.
    $Data = (Get-SnmpData -IP $MyHost -Community 'presslic' -OID '1.3.6.1.4.1.232.11.2.10.1.0' -Version V1 -UDPport 161).Data

    <#
        https://docs.sharpsnmp.com/tutorials/octet-strings.html

        SNMP data type 'OCTET STRING' and 'Opaque' can be viewed as byte array type in .NET
        Therefore, a problem has been there for a long time,
        “which encoding should be used there if an OctetString instance comes?”.

        In very old revision of #SNMP the answer was ASCII and there was no way in them
        to change it to Unicode or any other encodings.

        Note that it is recommended you use ASCII if possible.
    #>

    # Getting the second Byte (Octet 1) of the OCTET STRING value
    $Octet1 = [System.Text.Encoding]::ASCII.GetBytes($Data)[1]


    <#

        The Octet String is documented in CPQPOWER1.84.MIB (download from 'Hewlett Packard' (HP) support)

        OID '1.3.6.1.4.1.232.11.2.10.1.0'

        Device H/W Condition based on alarm (Octet 1)


        0 = Not available (e.g. Loss/lack of communication between card and device UPS/PDU)
        1 = Other             (unknown status)
        2 = OK 		(device normal operation)
        3 = Degraded 	        (warning alarm)
        4 = Failed 		(critical alarm)

    #>

    # default value for result is: script Error
    $ReturnString = '5:Error in Script' # Error severity

    # mapping (Octet 1) values to PRTG Sensor; Ok, Warning and Error level settings
    # adding meaningful message to the integer value
    Switch ($Octet1) {
        0 { $ReturnString = '4:USV Not available' } # Error severity
        1 { $ReturnString = '2:USV has unknown status' } # Warning severity
        2 { $ReturnString = '1:USV is OK' }
        3 { $ReturnString = '3:USV has Degraded' } # Warning severity
        4 { $ReturnString = '4:USV has Failed' } # Error severity
        Default { '6:SNMP returns wrong Data' } # Error severity
    }

    # report the String Value to PRTG sensor
    Write-Output $ReturnString

    }
Catch {
    Write-Error -ErrorRecord $_ -ErrorAction 'Continue'
    # if an ScriptError occurs, returning meaningful value and message to PRTG sensor
    Write-Output '5:Error in Script' # Error severity

}

If you have any questions regarding the script, please post them here.

octet-string powershell prtg script sensor snmp snmp-sensor

Created on Aug 21, 2020 8:22:57 AM

Last change on Aug 24, 2020 6:04:45 AM by  Stephan Linke [Paessler Support]



Replies

Nobody has replied yet


Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.