cisco · EEM Python Module · 12 стр.

Cisco EEM Python Module User Manual: Scripts, Actions & Configuration (EN)

Оригинальное название: Cisco EEM Python Module User Manual: Scripts, Actions & Configuration

Читать онлайн

Похожие инструкции

Содержание

Текст документа

EEM Python Package

EEM Python Module Embedded Event Manager (EEM) policies support Python scripts. Python scripts can be executed as part of EEM actions in EEM applets. Prerequisites for the EEM Python Module, on page 1 Information About EEM Python Module, on page 1 How to Configure the EEM Python Policy, on page 4 Additional References EEM Python Module, on page 9 Feature Information for EEM Python Module, on page 10 Prerequisites for the EEM Python Module Guest Shell must be working within the container. Guest Shell is not enabled by default. For more information see the Guest Shell feature.

Information About EEM Python Module Python Scripting in EEM Embedded Event Manager (EEM) policies support Python scripts. You can register Python scripts as EEM policies, and execute the registered Python scripts when a corresponding event occurs. The EEM Python script has the same event specification syntax as the EEM TCL policy. Configured EEM policies run within the Guest Shell. Guest Shell is a virtualized Linux-based environment, designed to run custom Linux applications, including Python for automated control and management of Cisco devices. The Guest Shell container provides a Python interpreter.

EEM Python Package The EEM Python package can be imported to Python scripts for running EEM-specific extensions. EEM Python Module 1

Стр. 1

Python-Supported EEM Actions

Note The EEM Python package is available only within the EEM Python script (The package can be registered with EEM, and has the EEM event specification in the first line of the script.) and not in the standard Python script (which is run using the Python script name). The Python package includes the following application programming interfaces (APIs): • Action APIs—Perform EEM actions and have default parameters. • CLI-execution APIs—Run IOS commands, and return the output. The following are the list of CLI-execution APIs:

• eem_cli_open() • eem_cli_exec() • eem_cli_read() • eem_cli_read_line() • eem_cli_run() • eem_cli_run_interactive() • eem_cli_read_pattern() • eem_cli_write() • eem_cli_close() • Environment variables-accessing APIs—Get the list of built-in or user-defined variables. The following are the environment variables-accessing APIs: • eem_event_reqinfo ()-Returns the built-in variables list. • eem_user_variables()-Returns the current value of an argument. Python-Supported EEM Actions The Python package (is available only within the EEM script, and not available for the standard Python script) supports the following EEM actions:

• Syslog message printing • Send SNMP traps • Reload the box • Switchover to the standby device • Run a policy • Track Object read • Track Object Set • Cisco Networking Services event generation EEM Python Module Python-Supported EEM Actions EEM Python Module 2

Стр. 2

EEM CLI Library Command Extensions

The EEM Python package exposes the interfaces for executing EEM actions. You can use the Python script to call these actions, and they are forwarded from the Python package via Cisco Plug N Play (PnP) to the action handler. EEM Variables An EEM policy can have the following types of variables: • Event-specific built-in variables—A set of predefinied variables that are populated with details about the event that triggered the policy. The eem_event_reqinfo () API returns the builtin variables list. These variables can be stored in the local machine and used as local variables. Changes to local variables do not reflect in builtin variables.

• User-defined variables—Variables that can be defined and used in policies. The value of these variables can be referred in the Python script. While executing the script, ensure that the latest value of the variable is available. The eem_user_variables() API returns the current value of the argument that is provided in the API. EEM CLI Library Command Extensions The following CLI library commands are available within EEM for the Python script to work: • eem_cli_close()—Closes the EXEC process and releases the VTY and the specified channel handler connected to the command. • eem_cli_exec—Writes the command to the specified channel handler to execute the command.

Then reads the output of the command from the channel and returns the output. • eem_cli_open—Allocates a VTY, creates an EXEC CLI session, and connects the VTY to a channel handler. Returns an array including the channel handler. • eem_cli_read()—Reads the command output from the specified CLI channel handler until the pattern of the device prompt occurs in the contents read. Returns all the contents read up to the match. • eem_cli_read_line()—Reads one line of the command output from the specified CLI channel handler. Returns the line read.

• eem_cli_read_pattern()—Reads the command output from the specified CLI channel handler until the pattern that is to be matched occurs in the contents read. Returns all the contents read up to the match. • eem_cli_run()—Iterates over the items in the clist and assumes that each one is a command to be executed in the enable mode. On success, returns the output of all executed commands and on failure, returns error. • eem_cli_run_interactive()—Provides a sublist to the clist which has three items. On success, returns the output of all executed commands and on failure, returns the error.

Also uses arrays when possible as a way of making things easier to read later by keeping expect and reply separated. • eem_cli_write()—Writes the command that is to be executed to the specified CLI channel handler. The CLI channel handler executes the command. EEM Python Module EEM Variables EEM Python Module 3

Стр. 3

Registering a Python Policy

How to Configure the EEM Python Policy For the Python script to work, you must enable the Guest Shell. For more information, see the Guest Shell chapter. Registering a Python Policy SUMMARY STEPS 1. enable 2. configure terminal 3. event manager directory user policy path 4. event manager policy policy-filename 5. exit 6. show event manager policy registered 7. show event manager history events DETAILED STEPS Procedure EEM Python Module How to Configure the EEM Python Policy Command or Action Purpose Step 1 enable Example: Device> enable Enables privileged EXEC mode. • Enter your password if prompted. Step 2 configure terminal Example:

Device# configure terminal Enters global configuration mode. Step 3 event manager directory user policy path Example: Device(config)# event manager directory user policy flash:/user library _ Specifies a directory to use for storing user library files or user-defined EEM policies. Note You must have a policy in the specified path. For example, in this step, the eem script.py policy is available in the _ flash:/user library folder or path. _ Step 4 event manager policy policy-filename Example: Device(config)# event manager policy eem script.py _ Registers a policy with EEM. • The policy is parsed based on the file extension.

If the file extension is .py, the policy is registered as Python policy. • EEM schedules and runs policies on the basis of an event specification that is contained within the policy itself. When the event manager policy command is EEM Python Module 4

Стр. 4