Execute Event Scripts in an Event Policy
Overview of the event-script Event Policy Action
Event scripts are custom scripts that an event policy can execute when it's triggered. You can write event scripts in Python, Stylesheet Language Alternative syntaX (SLAX), or Extensible Stylesheet Language Transformations (XSLT). Event scripts can perform any function available through Junos XML or Junos XML protocol remote procedure calls (RPCs).
For example, you can create event scripts that inspect system state, collect troubleshooting data, run operational commands and inspect the output, and determine the next appropriate action. This process can be repeated until the source of the problem is determined. The script can then report the source of the problem or automatically correct the issue by executing commands or changing the device configuration. By enabling programmatic responses to recurring or critical events, event scripts help reduce manual intervention, speed problem resolution, and improve operational consistency.
When an event policy invokes an event script, the device passes the event details to the event script in the form of XML inputs. Additionally, you can define your own set of arguments to pass to an event script. The arguments can optionally include variables that incorporate data from the triggering event or a correlated event. The event policy configuration can direct any event script output to a file and upload the file to a specified URL.
Configure an Event Policy to Invoke an Event Script
You can configure an event policy that executes event scripts and uploads the output
files from those scripts to a specified location for analysis. To configure such a
policy, include the following statements at the [edit
event-options] hierarchy level:
[edit event-options] policy policy-name { events [ events ]; then { event-script filename { arguments { argument-name argument-value; } destination destination-name; output-filename filename; output-format (text | xml); } } }
In the events statement, you can list multiple events. If any of the
listed events occurs, the eventd process executes the actions configured under the
then hierarchy. The event policy actions can include executing
one or more event scripts, which you configure by including the event-script
filename statement. The eventd process runs the
scripts in the order in which they appear in the configuration.
The scripts that you reference in the event-script statement must be
located in the /var/db/scripts/event directory on the device or
in the /config/scripts/event directory in flash memory.
Furthermore, you must configure the event scripts at the [edit event-options
event-script file] hierarchy level. For more information, see Store and Enable Junos Automation Scripts.
If the scripts are located in flash memory, you must configure the
load-scripts-from-flash statement at the [edit
system scripts] hierarchy level.
For detailed information about the requirements and restrictions when executing Python automation scripts on devices running Junos OS, see Requirements for Executing Python Automation Scripts on Junos Devices.
When an event policy invokes an event script, the device passes the event details to
the event script. You can additionally configure the arguments
statement to pass arguments to an event script as name-value pairs. The argument
values can include variables that contain information about the triggering event or
other received events. The event script can then reference this information during
execution. For detailed information about passing arguments to event scripts, see
Configuring an Event Policy to Pass Arguments to an Event Script.
Event Script Output File
When an event policy executes an event script in response to an event, the event
script can write output to a file. The default and only format for event script
output is XML. The system uploads the output file to the location specified in the
destination statement. The destination name must be configured
at the [edit event-options destinations] hierarchy level. For more
information, see Event Policy File Archiving.
Event script output is always in XML format even if you configure the
output-format text statement under the [edit
event-options policy policy-name then event-script
filename] hierarchy.
An event script output file uses the following naming convention:
hostname_YYYYMMDD_HHMMSS_output-filename_index-numberThe filename includes the hostname and timestamp to ensure that it is unique. The
output-filename string is the value
that you configure for the output-filename statement. If a device
triggers an event policy multiple times in a 1-second period, it appends an index
number to subsequent output files to ensure the filename is unique. The index number
range is 001 through 999.
For example, suppose you have an event policy action with
output-filename configured as rpd-messages on
device r1. If the event policy triggers 3 times in 1 second, the filenames would be
similar to the following:
-
r1_20210623_132333_rpd-messages
-
r1_20210623_132333_rpd-messages_001
-
r1_20210623_132333_rpd-messages_002
For the destination and output-filename statements,
there are four configuration scenarios:
-
Omit both the
destinationandoutput-filenamestatements—This option makes sense when the event script has no output. For example, the event script might execute onlyrequestcommands that have no associated output. -
Include both the
destinationandoutput-filenamestatements—If you configure theoutput-filenamestatement, you must also configure thedestinationstatement. The device redirects the script output to the file and uploads the file to the specified destination. -
Include the
destinationstatement in the configuration—You can omit theoutput-filenamestatement in the configuration and specify an output filename in the event script instead. The device uploads the output file to the configured destination. -
Include the
destinationandoutput-filenamestatements and also specify an output filename directly within the event script—If you do this, the output filename specified in the configuration overrides the output filename specified in the event script.
To specify the event script's output filename in the script itself, include the
<event-script-output-filename> element in the event
script. The element must be the first child tag within the
<output> parent tag. The event policy extracts the
filename from the event script, and the event script writes the output filename to
STDOUT. The XML syntax to use in the event script is:
<output> <event-script-output-filename>filename</event-script-output-filename> </output>
For example, suppose device1 has the following event policy. The
policy executes event script script1.xsl and uploads the output
file to the mgmt-archives destination.
[edit event-options]
policy policy1 {
events EVERY_HOUR;
then {
event-script script1.xsl {
destination mgmt-archives;
}
}
}
destinations {
mgmt-archives {
archive-sites {
"scp://admin@device2.example.com//tmp" password "$ABC123"; ## SECRET-DATA***
}
}
}
The event script writes the following output to STDOUT:
<event-script-output-filename>/var/tmp/cmd.txt</event-script-output-filename>
The system uploads the /var/cmd.txt file to the /tmp directory on device2. The system also renames the destination file to use the standard filenaming convention for event script output files.
device1_YYYYMMDD_HHMMSS_cmd.txt_index-number