項目一覧
例: カスタム・エラー メッセージの生成
Junos OS コミット スクリプトは、コミット操作中にカスタム エラー メッセージを生成し、設定がカスタム設定ルールに違反した場合に警告することができます。エラーメッセージを出すと、コミットが失敗します。この例では、特定のステートメントがデバイスコンフィギュレーションに含まれていない場合にカスタムエラーメッセージを生成するコミットスクリプトを作成し、それによってコミット操作を停止します。
必要条件
Junos OS リリース 16.1R3 以降のリリース(Python スクリプトを使用する場合)。
概要とコミット スクリプト
コミットスクリプトを使用して、 description ステートメントが [edit interfaces t1-fpc/pic/port] 階層レベルに含まれていない場合に表示されるカスタムエラーメッセージを記述します。
スクリプトは、XSLT、SLAX、および Python で表示されます。
XSLT 構文
<?xml version="1.0" standalone="yes"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:junos="http://xml.juniper.net/junos/*/junos"
xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm"
xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0">
<xsl:import href="../import/junos.xsl"/>
<xsl:template match="configuration">
<xsl:variable name="interface" select="interfaces/interface"/>
<xsl:for-each select="$interface[starts-with(name, 't1-')]">
<xsl:variable name="ifname" select="."/>
<xsl:if test="not(description)">
<xnm:error>
<xsl:call-template name="jcs:edit-path"/>
<xsl:call-template name="jcs:statement"/>
<message>Missing a description for this T1 interface.</message>
</xnm:error>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
SLAX 構文
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 configuration {
var $interface = interfaces/interface;
for-each ($interface[starts-with(name, 't1-')]) {
var $ifname = .;
if (not(description)) {
<xnm:error> {
call jcs:edit-path();
call jcs:statement();
<message> "Missing a description for this T1 interface.";
}
}
}
}
Python 構文
from junos import Junos_Configuration
import jcs
def main():
root = Junos_Configuration
for element in root.xpath(
"./interfaces/interface[starts-with(name,'t1-')]"):
# Missing description
if element.find('description') is None:
# Emit error message to console
jcs.emit_error("Missing a description for this T1 interface: "
+ element.find('name').text)
if __name__ == '__main__':
main()
構成
プロシージャ
手順
スクリプトのダウンロード、有効化、テスト: コミットスクリプトがエラーメッセージを正しく生成することをテストするには、候補の設定にエラーを誘発する条件が含まれていることを確認します。この例では、T1インターフェイスの設定に description ステートメントが含まれていないことを確認します。
このトピックの例をテストするには、次のようにします。
スクリプトをテキストファイルにコピーし、必要に応じてファイルに description.xsl、 description.slax、 または description.py という名前を付けて、デバイスの /var/db/scripts/commit/ ディレクトリにコピーします。
手記:署名されていないPythonスクリプトは、rootまたはJunos OS
super-userログインクラスのユーザーによって所有されている必要があり、ファイルの所有者のみがファイルへの書き込み権限を持つことができます。設定モードで、
[edit system scripts commit]階層レベルでfileステートメントとスクリプトファイル名を設定します。[edit] user@host# set system scripts commit file description.xsl
スクリプトが Python で記述されている場合は、署名されていない Python スクリプトの実行を有効にします。
[edit] user@host# set system scripts language python
手記:Python 3 を使用して Python スクリプトを実行するように
language python3ステートメントを設定するか、Python 2.7 を使用して Python スクリプトを実行するようにlanguage pythonステートメントを設定します。詳細については、「 言語」を参照してください。すべての T1 インターフェイスの設定に
descriptionステートメントが含まれている場合、テスト目的で既存の T1 インターフェイスの説明を削除します。[edit] user@host# delete interfaces t1–0/0/1 description
commitコマンドを発行して、設定をコミットします。user@host# commit
検証
スクリプトの実行の検証
目的
コミットスクリプトによって生成されたエラーメッセージを確認します。
アクション
commit コマンドの出力を確認します。コミット スクリプトは、description ステートメントを含まない各 T1 インターフェイスに対してエラー メッセージを生成します。エラーが発生すると、コミット プロセスが失敗します。
[edit] user@host# commit [edit interfaces interface t1-0/0/1] 'description' Missing a description for this T1 interface. [edit interfaces interface t1-0/0/2] 'description' Missing a description for this T1 interface. error: 2 errors reported by commit scripts error: commit script failure
エラー・メッセージの XML フォーマット・バージョンを表示するには、 commit check | display xml コマンドを発行します。
[edit interfaces t1-0/0/1]
user@host# commit check | display xml
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/10.0R1/junos">
<commit-results>
<routing-engine junos:style="normal">
<name>re0</name>
<xnm:error>
<edit-path>
[edit interfaces interface t1-0/0/1]
</edit-path>
<statement>
description
</statement>
<message>
Missing a description for this T1 interface.
</message>
</xnm:error>
<xnm:error>
<edit-path>
[edit interfaces interface t1-0/0/2]
</edit-path>
<statement>
description
</statement>
<message>
Missing a description for this T1 interface.
</message>
</xnm:error>
<xnm:error xmlns="http://xml.juniper.net/xnm/1.1/xnm"
xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm">
<message>
2 errors reported by commit scripts
</message>
</xnm:error>
<xnm:error xmlns="http://xml.juniper.net/xnm/1.1/xnm"
xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm">
<message>
commit script failure
</message>
</xnm:error>
</routing-engine>
</commit-results>
<cli>
<banner>[edit interfaces]</banner>
</cli>
</rpc-reply>
コミット スクリプト処理の詳細なトレースを表示するには、 commit check | display detail コマンドを発行します。
[edit interfaces t1-0/0/1]
user@host# commit check | display detail
2009-06-15 15:56:09 PDT: reading commit script configuration
2009-06-15 15:56:09 PDT: testing commit script configuration
2009-06-15 15:56:09 PDT: opening commit script '/var/db/scripts/commit/error.xsl'
2009-06-15 15:56:09 PDT: reading commit script 'error.xsl'
2009-06-15 15:56:09 PDT: running commit script 'error.xsl'
2009-06-15 15:56:09 PDT: processing commit script 'error.xsl'
[edit interfaces interface t1-0/0/1]
'description'
Missing a description for this T1 interface.
[edit interfaces interface t1-0/0/2]
'description'
Missing a description for this T1 interface.
2009-06-15 15:56:09 PDT: 2 errors from script 'error.xsl'
error: 2 errors reported by commit scripts
error: commit script failure