Regular Expressions in Custom SNMP Probes

InterMapper probes can break up a string retrieved from a device using a regular expression into separate variables. This tech note describes how to do this:

A customer had a piece of equipment that returned the following information in sysDescr.0:


     FW TR6-3.1.4Rt_F213E4, 2.4GHz, 0dBi ext. antenna

They wanted to display several interesting values from this string, including the firmware version ("FW"), the frequency, and the antenna used.

They created a probe that retrieved sysDescr.0 and then parsed out those strings with the following commands in the <snmp-device-variables> section of the probe:


    
    sysDescr,   1.3.6.1.2.1.1.1.0,                                          DEFAULT,     "system description"
    firmware,   "$sysDescr" =~ "^FW ([^,]+), (.+)Hz, (.+) antenna" ;"${1}", CALCULATION, "Firmware"
    frequency,  "${2}",                                                     CALCULATION, "Frequency"
    antenna,    "${3}",                                                     CALCULATION, "Antenna"
    . . .
    

Here is an annotated description of the above four lines:

  1. Retrieve sysDescr.0 (OID of 1.3.6.1.2.1.1.1.0) and assign it to the variable $sysDescr
  2. Set the value of $firmware based on the calculation. There are many things going on in this line:
    • The "=~" operator indicates that the $sysDescr variable should be parsed using the regular expression string that follows.
    • This regular expression breaks the string at the comma characters. The "[^,]" matches any single character that isn't a comma; adding a "+" forms a pattern that matches multiple non-comma characters.
    • Parentheses around a pattern serve to memorize a string. Each pair of paren's matches a string whose value is placed in variables numbered ${1}, ${2}, ${3}, etc.
    • The semicolon followed by "${1}" indicates that the entire CALCULATION should return the value of ${1} as a string.
    • The variable $firmware thus gets assigned the value of ${1}
  3. Assign the variable $frequency with the result of the second match (${2})
  4. Assign the variable $antenna with the result of the third match (${3})