2017-01-10
06:20 PM
- 最終編集日:
2017-09-20
11:03 AM
、編集者:
Hiromasa Kakehashi
NSOでは、お客様が作成するアプリケーションから NSO のCDB等へアクセスする方法として、以下の様なものを提供しています。
例えば、各NEDもいずれかの方法を使用して、NSOエンジンを経由し、CDB情報の操作やSouthbound機器のコントロールを行っています。また、お客様が作成するパッケージにおいても、同様です。
本記事では、NSO 4.2 より追加された Python API をWrapし直感的に使用出来る、Maagic を紹介致します。
NSOに登録された機器と、その管理アドレスを一覧で出力するアプリケーションを作成します。
テストで使用するNSOは、以下のようにデバイスが登録されています。
admin@ncs# show devices list
NAME ADDRESS DESCRIPTION NED ID ADMIN STATE
-------------------------------------------------------------
c1 10.0.0.1 - cisco-ios southbound-locked
c10 10.0.0.10 - cisco-ios southbound-locked
c2 10.0.0.2 - cisco-ios southbound-locked
c3 10.0.0.3 - cisco-ios southbound-locked
c4 10.0.0.4 - cisco-ios southbound-locked
c5 10.0.0.5 - cisco-ios southbound-locked
c6 10.0.0.6 - cisco-ios southbound-locked
c7 10.0.0.7 - cisco-ios southbound-locked
c8 10.0.0.8 - cisco-ios southbound-locked
c9 10.0.0.9 - cisco-ios southbound-locked
j1 10.0.3.1 - netconf southbound-locked
j2 10.0.3.2 - netconf southbound-locked
j3 10.0.3.3 - netconf southbound-locked
j4 10.0.3.4 - netconf southbound-locked
j5 10.0.3.5 - netconf southbound-locked
j6 10.0.3.6 - netconf southbound-locked
j7 10.0.3.7 - netconf southbound-locked
j8 10.0.3.8 - netconf southbound-locked
nx1 10.0.2.1 - cisco-nx southbound-locked
nx2 10.0.2.2 - cisco-nx southbound-locked
nx3 10.0.2.3 - cisco-nx southbound-locked
nx4 10.0.2.4 - cisco-nx southbound-locked
nx5 10.0.2.5 - cisco-nx southbound-locked
xr1 10.0.1.1 - cisco-ios-xr southbound-locked
xr10 10.0.1.10 - cisco-ios-xr southbound-locked
xr11 10.0.1.11 - cisco-ios-xr southbound-locked
xr12 10.0.1.12 - cisco-ios-xr southbound-locked
xr13 10.0.1.13 - cisco-ios-xr southbound-locked
xr14 10.0.1.14 - cisco-ios-xr southbound-locked
xr15 10.0.1.15 - cisco-ios-xr southbound-locked
xr2 10.0.1.2 - cisco-ios-xr southbound-locked
xr3 10.0.1.3 - cisco-ios-xr southbound-locked
xr4 10.0.1.4 - cisco-ios-xr southbound-locked
xr5 10.0.1.5 - cisco-ios-xr southbound-locked
xr6 10.0.1.6 - cisco-ios-xr southbound-locked
xr7 10.0.1.7 - cisco-ios-xr southbound-locked
xr8 10.0.1.8 - cisco-ios-xr southbound-locked
xr9 10.0.1.9 - cisco-ios-xr southbound-locked
admin@ncs#
方針として、以下のような方法で行います。これは、どの言語によるAPIを使用する場合でも、適用可能です。
(*) Python API では、with ステートメントを使用することで、コード上は省略可能
Python API を使用するには、ncs パッケージを import します。
import ncs
これにより、ncs パッケージ内に含まれるクラスが使えるようになりました。
ncs パッケージ内には、以下のモジュールがあります。
maapi 接続を行うには、ncs.maapi.Maapi クラスのコンストラクタを使用します。デフォルトでの接続先は、同じサーバ上(127.0.0.1)で動作しているNSOインスタンスです。IPCポート番号を変更している場合は、port=xxxxx を引数へ渡し、変更して下さい。
m = ncs.maapi.Maapi()
m = ncs.maapi.Maapi(port=1234) #ポート番号の指定
ncs.maapi.Session クラスを使用します。上記で作成した、maapi 接続を渡す事で、ログイン可能です。(Maapi.start_user_session と同等ですが、Session クラスを使うことで、with ステートメントの使用が可能です)
ncs.maapi.Session(m, 'username', 'context')
データベース(RUNNING/CANDIDATE/STARTUP 等) に対して、実施したい内容によって、以下が使用可能です。
t = ncs.maapi.Maapi.start_read_trans() # Read Only
t = ncs.maapi.Maapi.start_write_trans() # Read/Write
Write トランザクションを開始すると Write ロックが発行されるため、読み込みのみの場合であれば、Read Only トランザクションを使用するべきです。
Maagic API を使うことで、非常にプログラムがシンプルに書けるようになります。以下に同様の動作をするコードを二通りでお見せします。
デバイス c1/c2 のリモートアドレスを表示する
Python APIによる、通常の書き方
print( t.get_elem('/devices/device{c1}/address') )
print( t.get_elem('/devices/device{c2}/address') )
Maagic API を使用した書き方
root = ncs.maagic.get_root(t)
print( root['devices']['device']['c1']['address'] )
print( root['devices']['device']['c2']['address'] )
違いがわかりますでしょうか。前者は、get_elem 関数へ keypath を渡し、そのパスの値を読み込んでいます。複数のデバイスの情報の取得には、keypath を文字列で構成・結合して取得します。
後者では、ncs.maagic.get_root メソッドへ、トランザクションハンドラを渡し、root オブジェクトを取得しています。これは、Python のDictionary を実装しており、上記のように内容を読むことが出来ます。複数のデバイスを情報の取得には、デバイス名部分を変更して取得します。
違いを確認するために、もう一つ例を見てみましょう。
デバイスのリモートアドレスリストの表示
Maagic を使用しない場合は、以下のような記述となります。
カーソルを取得し、next() によって順にリストのキー(デバイス名) を取得しています。SNMPのテーブルを読むように、戻りがなくなったら終了です。
その後、そのキーを使用し、上記例で作成したように、データを読み込んでいます。
cursor = m.cursor(t.th, '/devices/device')
while True:
try:
entryKey = cursor.next()
deviceName = entryKey[0].as_pyval())
print(t.get_elem('/devices/device{{{}}}/address'.format(deviceName)))
except StopIteration:
break
一方、Maagic APIにより、以下のようにデバイスリストを得ることが可能です。
root = ncs.maagic.get_root(t)
for device in root['devices']['device']:
print device['address']
これらは内部的には同等のことを実施していますが、Maagic API を使用する事で、非常に簡素なコーディングで済むことがわかります。
以下が Maagic API を使用して作成し、完成したコードです。
with ステートメントを使用することで、各オブジェクトのクローズは自動的に行われます。
$ cat print-devices
import ncs
with ncs.maapi.Maapi() as m:
with ncs.maapi.Session(m, 'admin', 'context'):
with m.start_read_trans() as t:
root = ncs.maagic.get_root(t)
for dev in root.devices.device:
print("{0:5s}{1:12s}{2:9s}{3:10s}"
.format(
dev['name'],
dev['address'],
dev['device-type']['ne-type'],
dev['device-type']['cli']['ned-id']))
$
以下が実行結果です。
$ python print-devices
c1 10.0.0.1 cli cisco-ios
c10 10.0.0.10 cli cisco-ios
c2 10.0.0.2 cli cisco-ios
c3 10.0.0.3 cli cisco-ios
c4 10.0.0.4 cli cisco-ios
c5 10.0.0.5 cli cisco-ios
c6 10.0.0.6 cli cisco-ios
c7 10.0.0.7 cli cisco-ios
c8 10.0.0.8 cli cisco-ios
c9 10.0.0.9 cli cisco-ios
j1 10.0.3.1 netconf None
j2 10.0.3.2 netconf None
j3 10.0.3.3 netconf None
j4 10.0.3.4 netconf None
j5 10.0.3.5 netconf None
j6 10.0.3.6 netconf None
j7 10.0.3.7 netconf None
j8 10.0.3.8 netconf None
nx1 10.0.2.1 cli cisco-nx
nx2 10.0.2.2 cli cisco-nx
nx3 10.0.2.3 cli cisco-nx
nx4 10.0.2.4 cli cisco-nx
nx5 10.0.2.5 cli cisco-nx
xr1 10.0.1.1 cli cisco-ios-xr
xr10 10.0.1.10 cli cisco-ios-xr
xr11 10.0.1.11 cli cisco-ios-xr
xr12 10.0.1.12 cli cisco-ios-xr
xr13 10.0.1.13 cli cisco-ios-xr
xr14 10.0.1.14 cli cisco-ios-xr
xr15 10.0.1.15 cli cisco-ios-xr
xr2 10.0.1.2 cli cisco-ios-xr
xr3 10.0.1.3 cli cisco-ios-xr
xr4 10.0.1.4 cli cisco-ios-xr
xr5 10.0.1.5 cli cisco-ios-xr
xr6 10.0.1.6 cli cisco-ios-xr
xr7 10.0.1.7 cli cisco-ios-xr
xr8 10.0.1.8 cli cisco-ios-xr
xr9 10.0.1.9 cli cisco-ios-xr
$
Maagic API では、Dictionary形式のオブジェクト root は、取得時(ncs.maagic.get_root メソッド実行時)に配下のデータを取得はしません。各エントリへのアクセスが発生した場合に、それぞれ取得され、キャッシュされます。
検索バーにキーワード、フレーズ、または質問を入力し、お探しのものを見つけましょう
シスコ コミュニティをいち早く使いこなしていただけるよう役立つリンクをまとめました。みなさんのジャーニーがより良いものとなるようお手伝いします
下記より関連するコンテンツにアクセスできます