Skip to content

CMLでリンクの遅延などを一覧表示するPythonスクリプト

以前にPython で CML2 上のリンク情報を取得する (virl2-client 利用版)というメモを書きました。リンクのLatencyなどの値はLink conditioning として取得することが出来ます。今回は全リンクのBandwidth,Latency,Loss,Jitterを表示するサンプルをメモします。

検証環境

対象 バージョン
macOS Sequoia 15.5
Python 3.13.5

サンプルスクリプト

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from rich import box
from rich.console import Console
from rich.table import Table
from virl2_client import ClientLibrary

URL = "https://10.0.0.1"
USER = "admin"
PASS = "password"

cml = ClientLibrary(URL, USER, PASS, ssl_verify=False)
cml.is_system_ready(wait=True)

console = Console()
table = Table(title="CML All Links", box=box.MINIMAL_DOUBLE_HEAD)
table.add_column("Lab")
table.add_column("Link ID")
table.add_column("Node-A")
table.add_column("Node-B")
table.add_column("Bandwidth")
table.add_column("Latency")
table.add_column("Loss")
table.add_column("Jitter")

for lab in cml.all_labs():
    for link in lab.links():
        bandwidth = str(link.get_condition().get("bandwidth", ""))
        latency = str(link.get_condition().get("latency", ""))
        loss = str(link.get_condition().get("loss", ""))
        jitter = str(link.get_condition().get("jitter", ""))
        sstart = ""
        send = ""
        if bandwidth != "" or latency != "" or loss != "" or jitter != "":
            sstart = "[bold red]"
            send = "[/bold red]"
        table.add_row(
            f"{sstart}{lab.title}{send}",
            f"{sstart}{link.id}{send}",
            f"{sstart}{link.node_a.label} {link.interface_a.label}{send}",
            f"{sstart}{link.node_b.label} {link.interface_b.label}{send}",
            f"{sstart}{bandwidth}{send}",
            f"{sstart}{latency}{send}",
            f"{sstart}{jitter}{send}",
            f"{sstart}{loss}{send}",
        )
console.print(table)

実行例

実行例は以下の通りです。

> python sample.py
SSL Verification disabled
                                                         CML All Links
            ╷                                      ╷                  ╷                  ╷           ╷         ╷      ╷
  Lab       │ Link ID                              │ Node-A           │ Node-B           │ Bandwidth │ Latency │ Loss │ Jitter
 ═══════════╪══════════════════════════════════════╪══════════════════╪══════════════════╪═══════════╪═════════╪══════╪════════
  Topology1 │ 0cb9f0c0-511b-45c0-9817-6a51676bb2d1 │ dev4 Ethernet0/1 │ dev5 Ethernet0/0 │ 10203040
  Topology1 │ 43ccc981-b4e1-4942-9e87-5b96c5a46cce │ dev1 Ethernet0/1 │ dev2 Ethernet0/0 │ 10        │         │      │
  Topology1 │ 7a4d7fbf-7b27-4802-9430-e016b6ba5d6a │ dev2 Ethernet0/1 │ dev3 Ethernet0/0 │           │ 20      │      │
  Topology1 │ b382f412-92ef-4677-8748-0bcb9b2c54e4 │ dev3 Ethernet0/1 │ dev4 Ethernet0/0 │           │         │      │
            ╵                                      ╵                  ╵                  ╵           ╵         ╵      ╵

Bandwidth,Latency,Loss,Jitterの値が設定されている場合、行を赤色で表示します。

image