Posted Thu, 01 Jan 2015 06:00:00 GMT by Portal Admin

Using Regular Expressions in SNMP Probes

InterMapper SNMP probes can evaulate regular expressions (regex) to parse strings that are returned from a device. This note describes how to use regexes in SNMP probes.

Special Characters in InterMapper Regular Expressions:

  • \\s matches white space (note the "\\" are required to escape the pattern)
  • \\X escapes the character "X" (note the "\\" are required to escape a character)
  • [0-9] matches a single digit
  • [a-z] matches a single lower-case character
  • [A-Z] matches a single upper-case character
  • [a-zA-Z] matches a single alpha character - upper or lower case

Example:

A piece of equipment returns three important pieces of information encoded in a single string with this format:

Code:
Status (123 msec)


The goal was to separate these three parts: "Status", "123", and "msec" into separate variables. Here is a portion of the SNMP probe that did the calculations:

Code:
<parameters>
  StringToMatch = "Status (123 msec)"
</parameters>

<snmp-device-variables>
   -- parse out input of the form "alpha (digits alpha)"
   -- $pat1 is the pattern,
   -- str0 forces the string ($RT110951) to be matched against the pattern pat1
   -- str1, str2, str3 will have the resulting strings

   pat1, "([a-zA-Z]+)\\s*\\(\\s*([0-9]+)\\s*([a-zA-Z]+)\\s*)", CALCULATION, ""
   str0, "$StringToMatch" =~ "$pat1",                          CALCULATION, ""
   str1, "${1}",                                               CALCULATION, ""
   str2, "${2}",                                               CALCULATION, ""
   str3, "${3}",                                               CALCULATION, ""
</snmp-device-variables>

Here's how it works:

  1. The first line (pat1) is the pattern to match. It has several interesting components:
    • ([a-zA-Z]+) - a remembered pattern of 1 or more letters from the class a-z or A-Z
    • \\s* - zero or more whitespace characters (you need the "\\")
    • [(] - a literal "(" - you could also escape it with "\\" - that is "\\(" would work too
    • ([0-9]+) - a remembered pattern of one or more digits (second pattern)
    • \\s* - more white space
    • ([a-zA-Z]+) - a remembered pattern of one or more letters (third pattern)
    • [)] - a literal closing parenthesis
  2. The second line (str0) forces the parameter ($StringToMatch) to be matched against the pattern from line 1. This action sets the values of the three variables ${1}, ${2}, ${3} to the three remembered patterns within the parentheses of the $pat1.
     
  3. The next three lines set the variables str1, str2, str3 to the three remembered patterns. These variables can be used elsewhere in the probe.

Note: The following items all must be enclosed in "..." double-quotes in the <snmp-device-variables> section. This forces the values to be treated as string values.

"$StringToMatch"

"$pat1"

"([a-zA-Z]+)\\s*[(]([0-9]+)\\s*([a-zA-Z]+)[)]"

"${1}"

"${2}"

"${3}"

Example Probe File:

complete probe that shows this technique

You must be signed in to post in this forum.