イベント スクリプトでイベントとリモート実行の詳細を使用する
イベント ポリシー アクションには、1 つ以上のイベント スクリプトの実行を含めることができます。イベント ポリシーがイベント スクリプトを実行すると、イベント プロセスによってイベントの詳細がスクリプトに転送されます。これらのイベントの詳細は、必要に応じてキャプチャ、評価、ログファイルに送信できます。さらに、構成済みのリモート実行の詳細もイベント スクリプトに転送されます。
トリガーイベントと受信イベントの 2 種類のイベント詳細が返されます。 トリガーされたイベントは 、ポリシーをトリガーしたイベントの詳細を記録します。 受信したイベント には、トリガー イベントの前に発生したイベントの詳細が記録されます。トリガー イベントの詳細は、常にイベント スクリプトに転送されます。受信したイベントの詳細は、相関イベントに対してイベント ポリシーがトリガーされた場合にのみ表示されます。
1つ以上のリモートホストのホスト名、ユーザー名、秘密鍵を含むリモート実行の詳細は、イベントスクリプトで、イベントスクリプトで直接接続情報をエンコードすることなく、リモートホストでリモートプロシージャコールを呼び出すことができます。リモート実行の詳細は、 階層レベルで設定します [edit event-options event-script file filename remote-execution]
。個々のイベントスクリプトではなく、リモート実行の詳細を設定に含めると、情報が1つの場所にキャプチャされ、秘密鍵が暗号化されます。これは、イベント スクリプト ファイルには当てません。
イベントの詳細とリモート実行の詳細は、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
インポートして、トリガー イベントと受信イベントの詳細にアクセスする必要があります。はオブジェクトでありlxml.etree _Element
Junos_Received_Events
、 Junos_Trigger_Event
SLAX および XSLT スクリプト入力のおよび 階層と同じ階層と<received-events>
タグ名<trigger-event>
が含Junos_Received_Events
まれています。
Python イベント スクリプトは、 などの xpath()
find()
findall()
findtext()
lxml メソッドを使用して、オブジェクトから必要なイベントの詳細を抽出できます。例えば:
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
する必要があります。は、リモート デバイスのシーケンスを生成するジェネレータ関数で、設定された複数のホストを繰り返し簡単に実行できます。設定されたリモート ホストのホスト名、ユーザー名、および秘密鍵を参照するには、次のコードに示すように 、 、user
および passwd
プロパティを使用host
します。
from junos import Junos_Remote_Execution_Details for remote in Junos_Remote_Execution_Details(): hostname = remote.host username = remote.user passphrase = remote.passwd