Executing Op Scripts in an Event Policy
Operation scripts (op scripts) are Extensible Stylesheet Transformation (XSLT) scripts that you write and that are run in the command-line interface (CLI). Op scripts can perform any function available through JUNOS XML or JUNOScript remote procedure calls (RPCs). Additionally, you can pass to an op script a set of arguments that you define.
A script can build and run an operational mode command, receive the command output, 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 to you on the CLI.
You can run an op script anytime, simply by issuing the
opfilename.xsloperational mode command, as described in Configuring Op Scripts. You can also configure a policy that causes op scripts to be run and the output of those scripts to be uploaded to a specified location for analysis.To configure such a policy, include the following statements at the
[edit event-options]hierarchy level:[editevent-options]policypolicy-name{events [events];then{event-scriptfilename.xsl {arguments{name value;}output-filenamefilename;destinationdestination-name;}}}In the
eventsstatement, you can list multiple events. If one or more of the listed events occurs, the op script is executed. To view a list of the events that can be referenced in an event policy, issue theset event-options policypolicy-nameevents ?configuration mode command:user@host#set event-options policypolicy-nameevents ?Possible completions:<event>[ Open a set of valuesacct_accounting_ferroracct_accounting_fopen_error...In addition, you can reference internally generated events, which are discussed in Generating Internal Events.
In the
event-scriptstatement, you can specify a script to be executed on receipt of an event. The eventd process runs the scripts in the order in which they appear in the configuration. All scripts that you reference in theevent-scriptstatement must be located in the/var/db/scripts/opdirectory and must be enabled at the[edit system scripts op]hierarchy level. For more information, see Basic Process for Using Op Scripts and Configuring Op Scripts.You can include arguments to the script as name/value pairs. You can include variables in the argument values to allow data from the triggering event to be automatically included in the argument. The eventd process replaces each variable with values contained in the event that triggers the policy. You can use variables of the following forms:
{$$.attribute-name}—The double-dollar-sign notation ($$) represents the event that is triggering a policy. When combined with an attribute name, the variable is replaced by the value of the attribute name in the triggering event. For example,{$$.interface-name}stands for the value of theinterface-nameattribute in the triggering event.{$event.attribute-name}—The{$event.attribute-name}notation represents the most recent event that matches the specified event. The variable is replaced by the value of the attribute name of the most recent event that matchesevent. For example, when you include an argument calledinterfaceand define the value as{$COSD_CHAS_SCHED_MAP_INVALID.interface-name},the{$COSD_CHAS_SCHED_MAP_INVALID.interface-name}variable is replaced by theinterface-nameattribute of the most recentCOSD_CHAS_SCHED_MAP_INVALIDevent cached by the eventd process.For a given event, you can view a list of event attributes that you can reference by issuing the
help syslogeventcommand:user@host>help syslogeventFor example, in the following command output, text in angle brackets (
< >) shows attributes of theCOSD_CHASSIS_SCHEDULER_MAP_INVALIDevent:user@host>help syslog COSD_CHASSIS_SCHEDULER_MAP_INVALIDName: COSD_CHASSIS_SCHEDULER_MAP_INVALIDMessage: Chassis scheduler map incorrectly applied to interface <interface-name>: <error-message>...Another way to view a list of event attributes is to issue the
setattributes-matchevent?configuration mode command at the[edit event-options policypolicy-name]hierarchy level:[edit event-options policypolicy-name]user@host#set attributes-matchevent?For example, in the following command output, the
event.attributelist shows thaterror-messageandinterface-name areattributes of thecosd_chassis_scheduler_map_invalidevent:[edit event-options policy p1]user@host#set attributes-match cosd_chassis_scheduler_map_invalid?Possible completions:<from-event-attribute> First attribute to comparecosd_chassis_scheduler_map_invalid.error-messagecosd_chassis_scheduler_map_invalid.interface-nameIn this
setcommand, there is no space between the event name and the question mark (?).To view a list of all event attributes that you can reference, issue the
setattributes-match?configuration mode command at the[edit event-options policypolicy-name]hierarchy level:[edit event-options policypolicy-name]user@host#set attributes-match ?Possible completions:<from-event-attribute> First attribute to compareacct_accounting_ferroracct_accounting_fopen_error...In the
output-filenamestatement, assign the name of the file to which to write script output for the specified script.The filename format is
hostname_filename_YYYYMMDD_HHMMSS_index-number.For each uploaded file, a hostname and timestamp ensure that the uploaded files have unique filenames. If a policy is triggered multiple times in a 1-second period, an index number is added to ensure the filenames are unique. The index number range is 001 through 999.
For example, on a router named
r1, if you configure the output filename to beifl-events, and this event policy is triggered three times in one second, the files are named:In the
destinationstatement, include the destination name that you configured at the[edit event-options destinations]hierarchy level. For more information, see Defining Destinations for File Archiving.For a configuration example, see Executing an Op Script in Response to an Event.