Use Correlated Events to Trigger an Event Policy
Configure an event policy to execute when two or more correlated events occur.
How to Represent Triggering and Correlating Events in an Event Policy
In event script arguments and supported event policy statements such as the
execute-commands statement, you can use event policy variables
to differentiate between a triggering event and a correlating event.
You configure triggering events and correlating events in the following statements
at the [edit event-options policy policy-name]
hierarchy level:
- Triggering event—Configured in the
eventsstatement - Correlating event—Configured in the
within seconds eventsstatement
You can use event policy variables of the following forms to represent triggering and correlating events:
-
{$$.attribute-name}—The double dollar sign ($$) notation represents the event that triggers the policy. When combined with an attribute name, the variable resolves to the value of the attribute associated with the triggering event. For example,{$$.interface-name}resolves to the interface name associated with the triggering event. -
{$event.attribute-name}—The single dollar sign with the event name ($event) notation represents the most recent event that matchesevent. When combined with an attribute name, the variable resolves to the value of the attribute associated with that event. For example, when a policy issues theshow interfaces {$COSD_CHAS_SCHED_MAP_INVALID.interface-name}command, the{$COSD_CHAS_SCHED_MAP_INVALID.interface-name}variable resolves to the interface name associated with the most recentCOSD_CHAS_SCHED_MAP_INVALIDevent cached by the event process. -
{$*.attribute-name}—The dollar sign with the asterisk ($*) notation represents the most recent event that matches any of the correlating events. The variable resolves to the value of the attribute associated with most recent event that matches any of the correlated events specified in the policy configuration.
In event policies, you can reference specific events by using event policy variables. Consider the following event policy:
[edit event-options]
policy p1 {
events [ e1 e2 e3 ];
within 60 events [ e4 e5 e6 ];
then {
execute-commands {
commands {
"show interfaces {$$.interface-name}";
"show interfaces {$e4.interface-name}";
"show interfaces {$*.interface-name}";
}
output-filename command-output.txt;
destination some-dest;
}
}
}
In the show interfaces {$$.interface-name} command, the value of the
interface-name attribute of event e1,
e2, or e3 is substituted for the
{$$.interface-name} variable.
In the show interfaces {$e4.interface-name} command, the value of
the interface-name attribute of the most recent e4
event is substituted for the {$e4.interface-name} variable.
In the show interfaces {$*.interface-name} command, the value of the
interface-name attribute of the most recent
e4, e5, or e6 event is
substituted for the {$*.interface-name} variable. If one of
e1, e2, or e3 occurs within
60 seconds after e4, e5, or e6,
the value of the interface-name attribute for that correlating
event (e4, e5, or e6) is
substituted for the {$*.interface-name} variable. If the
correlating event does not have an interface-name attribute, the
software does not execute the show interfaces {$*.interface-name}
command.
If e1 occurs within 60 seconds of both e4 and
e5, then the {$*.interface-name} variable uses
the value of the interface-name attribute for e4.
The policy uses e4 because the event process (eventd) searches for
correlating events in sequential order as configured in the within
statement. In this case, the order is e4 > e5
> e6.
Example: Correlating Events Based on Time Interval
The following event policy issues a set of commands and uploads the resulting output
file to an archive site. The system executes the event policy if one of the trigger
events, event3,
event4, or
event5, occurs within 60 seconds after
one of the correlating events, event1 or
event2, occurs. The pseudocode for the
policy is as follows:
if trigger event is (event3 or event4 or event5)
and
(event1 or event2 has been received within the last 60 seconds)
then {
run a set of commands;
log the output of these commands to a location;
}The event policy destination defines two archive sites. The device attempts to transfer to the first archive site in the list, moving to the next site only if the transfer fails. The event policy configuration is:
[edit event-options]
policy policy1 {
events [ event3 event4 event5 ];
within 60 events [ event1 event2 ];
then {
execute-commands {
commands {
"command";
}
output-filename my_cmd_out;
destination policy1-command-dest;
}
}
}
destinations {
policy1-command-dest {
archive-sites {
scp://robot@my.big.com/a/b;
scp://robot@my.little.com/a/b;
}
}
}
Example: Correlating Events Based on Event Attributes
In the following event policy, the two events are correlated if their event attribute values match. Matching on the attributes of both events ensures that the two events are related. In this case, the interface addresses must match and the physical interface (ifd) names must match.
The RPD_KRT_IFDCHANGE error occurs when the routing protocol process
(rpd) sends a request to the kernel to change the state of an interface and the
request fails. The RPD_RDISC_NOMULTI error occurs when an interface
is configured for router discovery but the interface does not support IP multicast
operations as required.
[edit event-options]
policy policy1 {
events rpd_rdisc_nomulti;
within 500 events rpd_krt_ifdchange;
attributes-match {
rpd_rdisc_nomulti.interface-address equals rpd_krt_ifdchange.address;
rpd_rdisc_nomulti.interface-name starts-with rpd_krt_ifdchange.ifd-index;
}
then {
... actions ...
}
}
Example: Correlating Events Based on Time Interval and Event Attributes
In the following example, an organization wishes to record all failed configurations
that resulted in a loss of connectivity to the management network. The organization
uses a real-time performance monitoring (RPM) test setup that verifies the
reachability of the management network every minute. The operators always commit
changes using the commit confirmed CLI command. The commit
confirmed command enables the configuration to automatically roll back
if the configuration changes impact connectivity.
The event policy executes an event script, save-rollback.slax, to copy the failed configuration overridden by the automatic rollback to a specific location. The event script copies the /config/juniper.conf.1.gz rollback file to the /var/tmp directory for storage and later examination.
The event policy trigger event is the UI_COMMIT_NOT_CONFIRMED event. This event occurs when the device automatically rolls back the commit confirmed. When the system triggers a commit confirmed rollback, the UI_COMMIT_NOT_CONFIRMED event occurs twice. The first event message indicates the rollback is in progress, and the second event message indicates the rollback is complete. Thus, the event policy must correlate the two UI_COMMIT_NOT_CONFIRMED events so that the system doesn't trigger the event policy twice. In this example, the second UI_COMMIT_NOT_CONFIRMED event must occur within 50 seconds after the first event.
To determine if the configuration resulted in a loss of connectivity to the
management network, the event policy also correlates the UI_COMMIT_NOT_CONFIRMED
event with the PING_TEST_FAILED event from the RPM probe. A PING_TEST_FAILED event
occurs when the RPM test to the management network fails. In this example, the
UI_COMMIT_NOT_CONFIRMED event must occur within 60 seconds of the PING_TEST_FAILED
event. To specify the RPM probe test that generated the event, the event policy
includes the attributes-match statement and only triggers when the
event's test-owner attribute matches Connectivity
and the event's test-name attribute matches
Management.
The event policy configures separate within statements for the
PING_TEST_FAILED event and the first UI_COMMIT_NOT_CONFIRMED event. By using
multiple within statements, both conditions must occur for the
event policy to trigger. Each within statement requires a different
time interval. Similarly, both attributes-match conditions must be
true for the policy to trigger.
The event policy configuration is as follows:
event-options {
policy faulty-rollback {
events ui_commit_not_confirmed;
within 60 events ping_test_failed;
within 50 events ui_commit_not_confirmed;
attributes-match {
ping_test_failed.test-owner matches "^Connectivity$";
ping_test_failed.test-name matches "^Management$";
}
then {
event-script save-rollback.slax;
}
}
event-script {
file save-rollback.slax;
}
}The save-rollback.slax event script is as follows:
version 1.2;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
import "../import/junos.xsl";
match / {
/* Get system time with hyphens instead of spaces */
var $modified-time = translate( $localtime, " ", "-" );
/* Use time to differentiate saved rollback versions */
var $filename = "saved-rollback-" _ $modified-time _ ".gz";
/* Copy the file */
var $file-copy-rpc = <file-copy> {
<source> "/config/juniper.conf.1.gz";
<destination> "/var/tmp/" _ $filename;
}
var $results = jcs:invoke( $file-copy-rpc );
/* Report any errors or success */
if( $results/..//xnm:error ) {
for-each( $results/..//xnm:error ) {
expr jcs:syslog( "external.error", "File Copy Error: ", message );
}
}
else {
expr jcs:syslog( "external.info", "Faulty rollback configuration saved." );
}
}When the device running Junos OS triggers the event policy, the device saves the rollback configuration file in the /var/tmp directory with a new filename that includes the timestamp.
user@host> file list /var/tmp/saved* /var/tmp/saved-rollback-Wed-Aug-19-09:02:55-2026.gz