Regular expressions in Embedded Event Manager applets

From CT3

Jump to: navigation, search

By Ivan Pepelnjak

Cisco IOS Embedded Event Manager (EEM) release 3.0 (first available in IOS release 12.4(22)T) added support for variable logic for EEM applets including regular expressions. The regular expression matching is performed with the action regexp EEM applet command.

In this tutorial, you’ll learn how to use the action regexp command to extract information from the printouts generated by IOS CLI show commands. Our sample applet will display the IP address configured on the Loopback 0 interface.

This topic and numerous other EEM topics are covered in the EEM Workshop

Contents

Initial applet

To simplify the debugging process, we’ll run the sample EEM applet from the CLI interface using the event manager run command. The applet will execute the show ip interface brief | include Loopback 0 command and display the results of the command:

event manager applet showIP
 event none sync yes
 action 100 cli command "show ip interface brief | include Loopback0"
 action 199 puts "$_cli_result"
Notes:
  • The action cli command returns the output generated by the IOS CLI command in the $_cli_result variable.
  • Similar to the Tcl EEM policies, the puts command writes the text to the current line (console or VTY session) if the EEM applet executes within the context of a line (for example, when started with the event manager run command). Otherwise, its output is sent to the logging subsystem.

When executed, the applet produces the following output:

Client#event manager run showIP
Loopback0                  10.0.0.1        YES manual up                    up
Client#

Client#
The $_cli_result variable contains the command printout as well as the router prompt (Client#).

Matching the IP address

The action regexp pattern string command uses Tcl-like regular expressions to match its string argument with a pattern. The part of the string that is matched can be stored in an EEM applet variable. To extract the IP address from the show command output, we’ll use a simple pattern that matches digits or dots. The results of the match will be stored in the result variable.

event manager applet showIP
 event none sync yes
 action 100 cli command "show ip interface brief | include Loopback0"
 action 110 regexp " [0-9.]+ " $_cli_result result
 action 199 puts "IP address=*$result*"
Notes:
  • The [0-9.]+ pattern matches one or more (the + sign) digits (0-9) or the dot.
  • Dot used in a regular expression usually matches any character. However, when used within angle brackets, dot matches only the dot character.
  • Whitespace is used before and after the [0-9.]+ pattern to ensure it matches an IP address and not a part of an interface name (for example FastEthernet1/0.2).
  • Following the Tcl syntax, do not use the $ sign in front of an EEM applet variable name when you store a value in it. The $ sign is used only when you want to retrieve the variable’s value.

The modified applet produces the following output when the Loopback interface has an IP address:

Client#event manager run showIP
IP address=* 10.0.0.1 *

However, when the IP address is removed from the Loopback interface, the pattern match requested with the action regexp command fails and the result variable is not initialized. The action puts command tries to use a non-existing variable and fails.

Client#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Client(config)#interface loop 0
Client(config-if)#no ip address
Client(config-if)#^Z
Client#
%SYS-5-CONFIG_I: Configured from console by console
Client#event manager run showIP

Client#
%HA_EM-3-FMPD_UNKNOWN_ENV: fh_parse_var: could not find environment variable: result
%HA_EM-3-FMPD_ERROR: Error executing applet showIP statement 199

Detecting failed regexp match

The action regexp reports the status of the matching operation in the $_regexp_result EEM applet variable. You can use this variable in an action if command to check the success of the regular expression match.

We’ll modify our applet to display an error message if the IP address has not been configured on the Loopback interface:

event manager applet showIP
 event none sync yes
 action 100 cli command "show ip interface brief | include Loopback0"
 action 110 regexp " [0-9.]+ " $_cli_result result
 action 150 if $_regexp_result eq 1
 action 160  puts "IP address=*$result*"
 action 170 else
 action 180  puts "No IP address configured"
 action 190 end
The action if, action else and action end commands are used to implement the if-then-else block.

The modified applet correctly reports the IP address configured on the interface or the lack of the IP address:

Client#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Client(config)#interface loop 0
Client(config-if)#no ip address
Client(config-if)#^Z
Client#event manager run showIP
No IP address configured

Client#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
Client(config)#int loop 0
Client(config-if)#ip addr 10.0.0.1 255.255.255.255
Client(config-if)#^Z
Client#
Client#event manager run showIP
IP address=* 10.0.0.1 *

Initialize the result variables

Instead of testing the value of the $_regexp_result variable you could initialize the output variables to default values prior to the action regexp command ensuring that they will have a value even if the regular expression match fails. In the next version of our applet, we’ll set the default IP address to none and replace the default value with the actual IP address in the action regexp command.

event manager applet showIP
 event none sync yes
 action 100 cli command "show ip interface brief | include Loopback0"
 action 110 set result "none"
 action 120 regexp " [0-9.]+ " $_cli_result result
 action 130 puts "IP address=*$result*"
Notes
  • The action set command sets the value of the specified EEM applet variable.
  • You shall not use then $ sign in front of the variable name.

The applet yet again correctly handles both cases:

Client#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
Client(config)#int loop 0
Client(config-if)#ip addr 10.0.0.1 255.255.255.255
Client(config-if)#^Z
Client#
Client#event manager run showIP
IP address=* 10.0.0.1 *
Client#
Client#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Client(config)#interface loop 0
Client(config-if)#no ip address
Client(config-if)#^Z
Client#event manager run showIP
IP address=*none*

Extracting parts of the matching pattern

We had to use whitespaces around our IP address-matching pattern to ensure it matches only an IP address, not a subinterface name. However, we’d like to extract only the IP address value, not the surrounding whitespaces. To perform the value extraction, we have to enclose parts of the regular expression that we want to extract in brackets. The extracted values are stored in additional variables specified in the action regexp command.

We’ll modify our applet to store the IP address extracted from the show command in the IP_address variable which will have a default value none.

event manager applet showIP
 event none sync yes
 action 100 cli command "show ip interface brief | include Loopback0"
 action 110 set IP_address "none"
 action 120 regexp " ([0-9.]+) " $_cli_result result IP_address
 action 130 puts "IP address=*$IP_address*"

The applet extracts just the interface IP address and displays it:

Client#event manager run showIP
IP address=*10.0.0.1*

Summary

  • The action label regexp pattern string command can be used to match a string with a regular expression patter.
  • The $_regexp_result variable contains the status of the match: if the string matched the pattern, the variable is set to 1.
  • The action regexp command can be extended with a variable name (action label regexp pattern string variable) that receives the matching substring.
  • If the regular expression match fails, the output variable is not set. If it has not been initialized previously, its subsequent use will cause the EEM applet to abort.
  • You should always preset the value of output variables used in the action regexp command with the action set command or check the value of the $_regexp_result variable.
  • If you want to extract parts of the matched pattern, enclose them in brackets and add extra output variables to the action regexp command. These variables should be initialized prior to the action regexp command with the action set command to ensure they have a well-defined value.
Personal tools

CT3

Main menu