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

How to Get a Dynamic OID Suffix and Convert into SNMP Custom String Sensor

Votes:

0

Hi,

I'm a newbie here and still trying to learn PRTG :)

I have an equipment that generates an alert with a certain OID but the last digit (sequence number) is generated dynamically.

What I want to achieve:

  1. Walk the incomplete OID to get the last digit suffix
  2. If there is no result on the walk, do nothing
  3. If there is a result on the walk, build the complete OID
  4. Query the complete OID and fetch the resulting text string (alarm description)
  5. Create a sensor, set it to down status and notify the alarm description

As far as I know, there are no existing sensors for this kind of workflow, and this is only achievable through scripting.

If anyone could point me in the right direction - ideas, what tools to use, hints, tips and what sensor to use to achieve the above would be appreciated. Thank you!

script snmp walk

Created on Sep 20, 2022 1:15:12 PM

Last change on Sep 21, 2022 5:35:28 AM by  Felix Wiesneth [Paessler Support]



Best Answer

Accepted Answer

Votes:

1

Hi Felix,

Thank you for the pointers. I managed to achieve what I wanted :)

I followed your advice.

I used NETSNMP to do the SNMP walk. I modified the script using this template https://kb.paessler.com/en/topic/73131 Credits goes to Dariusz Gorka :)

What the script does:

  1. Does the snmpwalk of the OID of interest
  2. Retrieves the number of instances (in my case, the number of alarms)
  3. Outputs the string (in my case, the alarm description)

Since the script outputs the number of instances, I just enabled alerting based on limits, simply setting the upper error limit to 0. The moment that there is a device alarm, the sensor will go down :)

Also, since the script outputs the string, I am also able to see the alarm description on the 'Message' of PRTG. The message, however includes the OID and "STRING:" because of the output of the SNMP walk. I don't have any idea how to remove them and just retaining the string itself to make the message more neat. I tried several output options on the snmpwalk.exe command but I am not able to get it. Maybe others will have an idea on how to do this??? Here's a sample output of the string when I ran the PS script:

<text>Alarm Messages: .1.3.6.1.4.1.272.4.46.5.1.54 = STRING: "this is a test alarm"</text>

Below is the modified script. This is confirmed to be working based on my own tests.

Feedbacks are most welcome!

#    ____  ____  ____________
#   / __ \/ __ \/_  __/ ____/
#  / /_/ / /_/ / / / / / __  
# / ____/ _, _/ / / / /_/ /  
#/_/   /_/ |_| /_/  \____/                         
#    NETWORK MONITOR
#-------------------
#
# This script checks the current alarms of a target device with dynamic OID suffix using snmpwalk
#

# Parameter "-hostaddr" for the nCompass IP address
# Parameter "-community" for the SNMP v2c Community
# Parameter "-port" for the SNMP port of the device
# Parameter "-timeout" for the timeout of the request
# Parameter "-troubleshoot" for the troubleshooting of this script
param(
    [string]$hostaddr = "localhost",
    [string]$community = "public",
    [string]$port = "161",
    [string]$timeout = "5",
    [int]$troubleshoot = 0
)

$regex_string = "(STRING:..)([a-zA-Z0-9- ])"

[string[]] $walkresult = @(C:\usr\bin\snmpwalk.exe -Cc -Ln -On -v 2c -c $community $hostaddr":"$port ".1.3.6.1.4.1.272.4.46.5.1.54" -t $timeout 2>&1)

if ($troubleshoot -eq 1){
$walkresult_trouble = $walkresult -join ''
}

if (!($walkresult  -like "*= String: *")){
    $alarms = 0
} else {
    $alarms = $walkresult.count }


    [string]$walkresult_result = $walkresult -join ''


write-host "<prtg>"

write-host "<result>"
write-host "<channel>Number of Current Alarms</channel>"
write-host "<value>$($alarms)</value>"
write-host "</result>"

if($troubleshoot -eq 0){
    if (!($alarms -eq 0)){
        write-host "<text>Alarm Messages: $($walkresult_result)</text>"
    }
}

if ($troubleshoot -eq 1){
write-host "<text>$($walkresult_trouble)</text>"
}

write-host "</prtg>"

Created on Sep 21, 2022 1:35:15 PM

Last change on Sep 21, 2022 2:10:49 PM by  Felix Wiesneth [Paessler Support]



3 Replies

Votes:

1

Hi Spraken,

Welcome to PRTG and to the work with SNMP! :)

First of all, you're correct. There is no straight out-of-the-box solution for this, but I can forward some ideas how this could be achieved:

  • Creating a sensor when an error occurs, especially if the indexes change over time, would result in many dead sensors over time if you use the default SNMP sensors. So this would not be my personal preferred option
  • I would recommend to do the checks and the SNMP monitoring outside of PRTG. You could create a sensors based on a custom script to utilize NETSNMP for the requests. Here is one template which describes that: https://kb.paessler.com/en/topic/73131
  • Depending on the result of the checks, you can change the status of the sensor to eventually trigger notifications of your choice, or to display the status on your maps (dashboards). In your PRTG interface, you can find the documentation via the Setup > PRTG API > Custom Sensors page
  • As PRTG only stores numerical values and no strings, it highly depends on what you want to achieve:

Hope this helps! If you want to share the final solution, I'm sure that the community will appreciate it.


Kind regards,
Felix Saure, Tech Support Team

Created on Sep 21, 2022 6:29:06 AM by  Felix Saure [Paessler Support]



Accepted Answer

Votes:

1

Hi Felix,

Thank you for the pointers. I managed to achieve what I wanted :)

I followed your advice.

I used NETSNMP to do the SNMP walk. I modified the script using this template https://kb.paessler.com/en/topic/73131 Credits goes to Dariusz Gorka :)

What the script does:

  1. Does the snmpwalk of the OID of interest
  2. Retrieves the number of instances (in my case, the number of alarms)
  3. Outputs the string (in my case, the alarm description)

Since the script outputs the number of instances, I just enabled alerting based on limits, simply setting the upper error limit to 0. The moment that there is a device alarm, the sensor will go down :)

Also, since the script outputs the string, I am also able to see the alarm description on the 'Message' of PRTG. The message, however includes the OID and "STRING:" because of the output of the SNMP walk. I don't have any idea how to remove them and just retaining the string itself to make the message more neat. I tried several output options on the snmpwalk.exe command but I am not able to get it. Maybe others will have an idea on how to do this??? Here's a sample output of the string when I ran the PS script:

<text>Alarm Messages: .1.3.6.1.4.1.272.4.46.5.1.54 = STRING: "this is a test alarm"</text>

Below is the modified script. This is confirmed to be working based on my own tests.

Feedbacks are most welcome!

#    ____  ____  ____________
#   / __ \/ __ \/_  __/ ____/
#  / /_/ / /_/ / / / / / __  
# / ____/ _, _/ / / / /_/ /  
#/_/   /_/ |_| /_/  \____/                         
#    NETWORK MONITOR
#-------------------
#
# This script checks the current alarms of a target device with dynamic OID suffix using snmpwalk
#

# Parameter "-hostaddr" for the nCompass IP address
# Parameter "-community" for the SNMP v2c Community
# Parameter "-port" for the SNMP port of the device
# Parameter "-timeout" for the timeout of the request
# Parameter "-troubleshoot" for the troubleshooting of this script
param(
    [string]$hostaddr = "localhost",
    [string]$community = "public",
    [string]$port = "161",
    [string]$timeout = "5",
    [int]$troubleshoot = 0
)

$regex_string = "(STRING:..)([a-zA-Z0-9- ])"

[string[]] $walkresult = @(C:\usr\bin\snmpwalk.exe -Cc -Ln -On -v 2c -c $community $hostaddr":"$port ".1.3.6.1.4.1.272.4.46.5.1.54" -t $timeout 2>&1)

if ($troubleshoot -eq 1){
$walkresult_trouble = $walkresult -join ''
}

if (!($walkresult  -like "*= String: *")){
    $alarms = 0
} else {
    $alarms = $walkresult.count }


    [string]$walkresult_result = $walkresult -join ''


write-host "<prtg>"

write-host "<result>"
write-host "<channel>Number of Current Alarms</channel>"
write-host "<value>$($alarms)</value>"
write-host "</result>"

if($troubleshoot -eq 0){
    if (!($alarms -eq 0)){
        write-host "<text>Alarm Messages: $($walkresult_result)</text>"
    }
}

if ($troubleshoot -eq 1){
write-host "<text>$($walkresult_trouble)</text>"
}

write-host "</prtg>"

Created on Sep 21, 2022 1:35:15 PM

Last change on Sep 21, 2022 2:10:49 PM by  Felix Wiesneth [Paessler Support]



Votes:

0

Hi Spraken,

Awesome, thanks for sharing! I'll let Dariusz know! :)


Kind regards,
Felix Saure, Tech Support Team

Created on Sep 22, 2022 4:33:31 AM by  Felix Saure [Paessler Support]




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.