在事件脚本中使用事件和远程执行详细信息
事件策略操作可以包括执行一个或多个事件脚本。当事件策略执行事件脚本时,事件进程会将事件详细信息转发至脚本。可根据需要捕获、评估这些事件详细信息并发送至日志文件。此外,任何已配置的远程执行详细信息也将转发至事件脚本。
两种类型的活动详细信息将返回:触发的事件和收到的事件。 触发的事件 会记录触发策略的事件的详细信息。 收到的事件 会记录触发事件之前发生的事件的详细信息。触发事件详细信息始终转发至事件脚本。仅当为关联事件触发事件策略时,才会有收到的事件详细信息。
远程执行详细信息(包括一个或多个远程主机的主机名、用户名和直通措辞),允许事件脚本在远程主机上调用远程过程调用呼叫,而无需在事件脚本中直接编码连接信息。您可在 [edit event-options event-script file filename remote-execution] 层级配置远程执行详细信息。当在配置中包含远程执行详细信息,而非单个事件脚本中时,信息会在单个位置捕获,并通过措辞进行加密。事件脚本文件中的情况并非如此。
事件详细信息和远程执行详细信息以以下格式转发至 SLAX 和 XSLT 事件脚本作为 XML:
<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>
有关该 <junos-context> 元素的信息,请参阅 Junos OS 自动化脚本中的全局参数和变量。
Python 事件脚本必须导入Junos_Trigger_Event和对象以访问有关触发事件和收到事件的详细信息。 Junos_Trigger_Event Junos_Received_Events并且是lxml.etree _Element对象,并且包含与 <trigger-event> SLAX 和 XSLT 脚本输入中的和<received-events>层次结构相同的层次结构和标记Junos_Received_Events名称。
Python 事件脚本可以使用 lxml 方法(如 xpath() 和 find()findall()findtext()) 从对象中提取必要的事件详细信息。例如:
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 事件脚本必须导入Junos_Remote_Execution_Details以访问层[edit event-options event-script file filename remote-execution]级配置的远程执行详细信息。 Junos_Remote_Execution_Details 是生成一系列远程设备的生成器功能,可通过多个配置的主机轻松迭代。您可参考配置远程主机的主机名、用户名和密码,方法是使用hostuser以下代码中的 、 和passwd属性:
from junos import Junos_Remote_Execution_Details
for remote in Junos_Remote_Execution_Details():
hostname = remote.host
username = remote.user
passphrase = remote.passwd