Use Event and Remote Execution Details in Event Scripts
Event policy actions can include executing one or more event scripts. When an event policy executes an event script, the event process forwards the event details to the script. These event details can be captured, evaluated, and sent to log files as required. In addition, any configured remote execution details are also forwarded to the event script.
Two types of event details are returned: triggered events and received events. Triggered events record the details of the event that triggered the policy. Received events record the details of events that happened before the triggering event. Trigger event details are always forwarded to event scripts. Received event details are only present when an event policy is triggered for correlated events.
Remote execution details, which include the hostname, username,
and passphrase for one or more remote hosts, enable an event script
to invoke remote procedure calls on remote hosts without encoding
the connection information directly in the event script. You configure
remote execution details at the [edit event-options event-script
file filename remote-execution]
hierarchy
level. When you include remote execution details in the configuration
instead of in the individual event scripts, the information is captured
in a single location and the passphrase is encrypted. This is not
the case in an event script file.
Event details and remote execution details are forwarded to SLAX and XSLT event scripts as XML in the following format:
<event-script-input> <junos-context> ... </junos-context> <trigger-event> <id>event-id</id> <type>event-type</type> <generation-time>timestamp</generation-time> <process> <name>process-name</name> <pid>pid</pid> </process> <hostname>hostname</hostname> <message>message-string</message> <facility>facility-string</facility> <severity>severity-string</severity> <attribute-list> <attribute> <name>attribute-name</name> <value>attribute-value</value> </attribute> </attribute-list> </trigger-event> <received-events> <received-event> <id>event-id</id> <type>event-type</type> <generation-time>timestamp</generation-time> <process> <name>process-name</name> <pid>pid</pid> </process> <hostname>hostname</hostname> <facility>facility-string</facility> <severity>severity-string</severity> <attribute-list> <attribute> <name>attribute-name</name> <value>attribute-value</value> </attribute> </attribute-list> </received-event> </received-events> <remote-execution-details> <remote-execution-detail> <remote-hostname>hostname</remote-hostname> <username>username</username> <passphrase>passphrase</passphrase> </remote-execution-detail> </remote-execution-details> </event-script-input>
For information about the <junos-context>
element, see Global Parameters and Variables in Junos OS Automation Scripts.
Python event scripts must import the Junos_Trigger_Event
and Junos_Received_Events
objects to
access details about the trigger event and received events. Junos_Trigger_Event
and Junos_Received_Events
are lxml.etree _Element
objects and contain
the same hierarchy and tag names as the <trigger-event>
and <received-events>
hierarchies
in the SLAX and XSLT script input.
Python event scripts can extract the necessary event
details from the objects using lxml methods such as xpath()
and find()
, findall()
, and findtext()
. For example:
from junos import Junos_Trigger_Event from junos import Junos_Received_Events id = Junos_Trigger_Event.xpath('//trigger-event/id')[0].text name = Junos_Trigger_Event.xpath('//trigger-event/process/name')[0].text message = Junos_Trigger_Event.xpath('//trigger-event/message')[0].text
Python event scripts must import Junos_Remote_Execution_Details
to access the remote execution details configured at the [edit
event-options event-script file filename remote-execution]
hierarchy level. Junos_Remote_Execution_Details
is a generator function that produces a sequence of remote devices,
which makes it easy to iterate over multiple configured hosts. You
can reference the hostname, username, and passphrase for a configured
remote host by using the host
, user
, and passwd
properties
as in the following code:
from junos import Junos_Remote_Execution_Details for remote in Junos_Remote_Execution_Details(): hostname = remote.host username = remote.user passphrase = remote.passwd