らくがきちょう

なんとなく

APIC-EM の API にアクセスしてみる

APIC-EM は GUI 上からデバイスを管理出来ますが、APIC-EM の API へアクセスすることによって管理されている機器の情報を利用することが出来ます。今回は Python を使って APIC-EM の API へアクセスしてみます。

検証環境

検証環境は以下を使いました。トポロジー以前の記事と同じものを使いました。

ブラウザ上から API を確認する

ブラウザから API の詳細を確認するには、まずホーム画面の右上にある API をクリックします。

f:id:sig9:20160530191452p:plain

Swagger の画面に遷移します。今回、利用する Inventory → network-device をクリックします。

f:id:sig9:20160530191501p:plain

すると、network-device API の詳細が表示されます。

f:id:sig9:20160530191508p:plain

Python から API を呼び出してみる

Python のプログラム全体は以下の通りです。

#!/usr/bin/env python

import requests
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

controller_url = 'https://ADDRESS/api/v1/'
auth = {
    'username': 'USERNAME',
    'password': 'PASSWORD'
}

ticket = requests.post(
    controller_url + 'ticket',
    data=json.dumps(auth),
    headers={'content-type': 'application/json'},
    verify=False
)

service_ticket = ticket.json()['response']['serviceTicket']
get_device = requests.get(
    controller_url + 'network-device',
    headers={'X-Auth-Token': service_ticket},
    verify=False
)

print json.dumps(
    get_device.json(),
    indent=4
)

解説

HTTP リクエストの送信には request モジュールを、JSON の取り扱いに json モジュールを使うので import しておきます。

import requests
import json

そのまま実行すると APIC-EM の既定で利用されている自己証明書に関する警告が表示されてしまう為、下記のようにして警告を無効化しています。

from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

APIC-EM へアクセスするのに必要な URL、ユーザ名、パスワードを定義します。API のベースになる URL は「https:/ADDRESS/api/v1/」です (ADDRESS は APIC-EM のアドレス)。

controller_url = 'https://ADDRESS/api/v1/'
auth = {
    'username': 'USERNAME',
    'password': 'PASSWORD'
}

API を呼び出す為には認証し、トークンを取得しておく必要があります。トークンを取得する為の URL は「https://ADDRESS/api/v1/ticket」です。自己証明書でもアクセスを続行する為に verify=False を指定しています。

ticket = requests.post(
    controller_url + 'ticket',
    data=json.dumps(auth),
    headers={'content-type': 'application/json'},
    verify=False
)

認証に成功するとトークン (serviceTicket) を取得出来ますので、service_ticket に代入しておきます。

service_ticket = ticket.json()['response']['serviceTicket']

次は network-device API を呼び出します (GET します)。トークンは X-Auth-Token ヘッダーに格納しておきます。

get_device = requests.get(
    controller_url + 'network-device',
    headers={'X-Auth-Token': service_ticket},
    verify=False
)

API 呼び出しが成功すれば、後は結果を表示するだけです。

print json.dumps(
    get_device.json(),
    indent=4
)

実行例

$ python2.7 network-device.py
{
    "version": "1.0",
    "response": [
        {
            "macAddress": "fa:16:3e:9d:5e:57",
            "upTime": "20:12:28.83",
            "bootDateTime": "2016-05-29 12:46:41",
            "lastUpdateTime": 1464604008472,
            "snmpContact": "",
            "platformId": "IOSv",
            "series": "Cisco 3900 Series Integrated Services Routers G2",
            "interfaceCount": "2",
            "tunnelUdpPort": null,
            "id": "0481cdcc-f2dc-4ee8-9a9c-dd9ed1274a7e",
            "locationName": null,
            "instanceUuid": "0481cdcc-f2dc-4ee8-9a9c-dd9ed1274a7e",
            "family": "Routers",
            "reachabilityStatus": "Reachable",
            "hostname": "IOSv1.example.local",
            "memorySize": "347798400",
            "roleSource": "AUTO",
            "lineCardCount": "1",
            "collectionStatus": "Managed",
            "role": "BORDER ROUTER",
            "location": null,
            "inventoryStatusDetail": "<status><general code=\"SUCCESS\"/></status>",
            "type": "Cisco 3945 Integrated Services Router G2",
            "lineCardId": "079e9cfb-dadf-4fdc-bf38-7c857ad3d774",
            "apManagerInterfaceIp": "",
            "lastUpdated": "2016-05-30 10:26:48",
            "managementIpAddress": "10.100.4.1",
            "serialNumber": "9VFIDI2JBMQRUID38TI4T",
            "reachabilityFailureReason": "",
            "softwareVersion": "15.6(2)T",
            "snmpLocation": "",
            "tagCount": "0"
        },
        {
            "macAddress": "fa:16:3e:e9:36:84",
            "upTime": "20:12:46.40",
            "bootDateTime": "2016-05-29 12:46:42",
            "lastUpdateTime": 1464603986690,
            "snmpContact": "",
            "platformId": "IOSv",
            "series": "Cisco 3900 Series Integrated Services Routers G2",
            "interfaceCount": "3",
            "tunnelUdpPort": null,
            "id": "32617bbf-df08-4267-92f7-06290e601588",
            "locationName": null,
            "instanceUuid": "32617bbf-df08-4267-92f7-06290e601588",
            "family": "Routers",
            "reachabilityStatus": "Reachable",
            "hostname": "IOSv2.example.local",
            "memorySize": "335215488",
            "roleSource": "AUTO",
            "lineCardCount": "1",
            "collectionStatus": "Managed",
            "role": "BORDER ROUTER",
            "location": null,
            "inventoryStatusDetail": "<status><general code=\"SUCCESS\"/></status>",
            "type": "Cisco 3945 Integrated Services Router G2",
            "lineCardId": "164e2b13-b48d-4b72-9a82-c5432c4c6b38",
            "apManagerInterfaceIp": "",
            "lastUpdated": "2016-05-30 10:26:26",
            "managementIpAddress": "10.0.12.2",
            "serialNumber": "9QOH3V7GIP7QXN0IGB88T",
            "reachabilityFailureReason": "",
            "softwareVersion": "15.6(2)T",
            "snmpLocation": "",
            "tagCount": "0"
        },
        {
            "macAddress": "fa:16:3e:34:74:cd",
            "upTime": "20:09:49.39",
            "bootDateTime": "2016-05-29 12:46:42",
            "lastUpdateTime": 1464603959298,
            "snmpContact": "",
            "platformId": "IOSv",
            "series": "Cisco 3900 Series Integrated Services Routers G2",
            "interfaceCount": "3",
            "tunnelUdpPort": null,
            "id": "9e413d48-4d2d-449e-8349-017b6f4af8e9",
            "locationName": null,
            "instanceUuid": "9e413d48-4d2d-449e-8349-017b6f4af8e9",
            "family": "Routers",
            "reachabilityStatus": "Reachable",
            "hostname": "IOSv3.example.local",
            "memorySize": "335215488",
            "roleSource": "AUTO",
            "lineCardCount": "1",
            "collectionStatus": "Managed",
            "role": "BORDER ROUTER",
            "location": null,
            "inventoryStatusDetail": "<status><general code=\"SUCCESS\"/></status>",
            "type": "Cisco 3945 Integrated Services Router G2",
            "lineCardId": "e4c63f3d-35e1-43b8-a02c-6cbc8362d9d9",
            "apManagerInterfaceIp": "",
            "lastUpdated": "2016-05-30 10:25:59",
            "managementIpAddress": "10.0.23.3",
            "serialNumber": "9DKHCVBX27ZP8C3CQHOOW",
            "reachabilityFailureReason": "",
            "softwareVersion": "15.6(2)T",
            "snmpLocation": "",
            "tagCount": "0"
        },
        {
            "macAddress": "fa:16:3e:ae:b4:18",
            "upTime": "20:13:57.92",
            "bootDateTime": "2016-05-29 12:46:43",
            "lastUpdateTime": 1464603930034,
            "snmpContact": "",
            "platformId": "IOSv",
            "series": "Cisco 3900 Series Integrated Services Routers G2",
            "interfaceCount": "3",
            "tunnelUdpPort": null,
            "id": "beb25dc8-5838-44b0-9bc6-fe834c234c2e",
            "locationName": null,
            "instanceUuid": "beb25dc8-5838-44b0-9bc6-fe834c234c2e",
            "family": "Routers",
            "reachabilityStatus": "Reachable",
            "hostname": "IOSv4.example.local",
            "memorySize": "335215488",
            "roleSource": "AUTO",
            "lineCardCount": "1",
            "collectionStatus": "Managed",
            "role": "BORDER ROUTER",
            "location": null,
            "inventoryStatusDetail": "<status><general code=\"SUCCESS\"/></status>",
            "type": "Cisco 3945 Integrated Services Router G2",
            "lineCardId": "a5ba6c8a-733f-4544-b7b0-5ae9136bae8e",
            "apManagerInterfaceIp": "",
            "lastUpdated": "2016-05-30 10:25:30",
            "managementIpAddress": "10.0.34.4",
            "serialNumber": "9JLHM88D6QR7TEO1NADR5",
            "reachabilityFailureReason": "",
            "softwareVersion": "15.6(2)T",
            "snmpLocation": "",
            "tagCount": "0"
        },
        {
            "macAddress": "fa:16:3e:b1:fa:a5",
            "upTime": "20:13:40.61",
            "bootDateTime": "2016-05-29 12:46:44",
            "lastUpdateTime": 1464603837669,
            "snmpContact": "",
            "platformId": "IOSv",
            "series": "Cisco 3900 Series Integrated Services Routers G2",
            "interfaceCount": "2",
            "tunnelUdpPort": null,
            "id": "5a276269-f261-478b-88e0-4228fe8e989a",
            "locationName": null,
            "instanceUuid": "5a276269-f261-478b-88e0-4228fe8e989a",
            "family": "Routers",
            "reachabilityStatus": "Reachable",
            "hostname": "IOSv5.example.local",
            "memorySize": "347798400",
            "roleSource": "AUTO",
            "lineCardCount": "1",
            "collectionStatus": "Managed",
            "role": "BORDER ROUTER",
            "location": null,
            "inventoryStatusDetail": "<status><general code=\"SUCCESS\"/></status>",
            "type": "Cisco 3945 Integrated Services Router G2",
            "lineCardId": "2d514ec5-cd6b-4249-908a-ca3042ae798b",
            "apManagerInterfaceIp": "",
            "lastUpdated": "2016-05-30 10:23:57",
            "managementIpAddress": "10.0.45.5",
            "serialNumber": "9734KHDB8OGRH7GTYVH9J",
            "reachabilityFailureReason": "",
            "softwareVersion": "15.6(2)T",
            "snmpLocation": "",
            "tagCount": "0"
        }
    ]
}