Python クライアントの使用

Python クライアントを使用して、IBM Cloud Monitoring サービスを管理できます。 このクライアントは、sdcclient とも呼ばれます。

以下の手順は、Python バージョン 3.x に適用されます。

デフォルトのチームに関連付けられているリソースを管理するには、認証方式として IAM を使用します。

チームに関連付けられたリソースを管理するには、そのチームに対してアクティブになっている Monitoring API トークンを使用する必要があります。

ステップ 1. Python クライアントのインストール

前提条件

Python バージョン 3 以降を使用する場合は、以下の Python パッケージをインストールします。

  1. 以下のコマンドを実行して、request パッケージをインストールします。

    pip3 install requests
    
  2. 以下のコマンドを実行して、pyyaml パッケージをインストールします。

    python3 -m pip install pyyaml
    

pip を使用した Python クライアントのインストール

端末から次のコマンドを実行します。

sddclient のバージョンは、0.9.x 以上でなければなりません。

# sddclient version must be at least 0.9.0
pip3 install sdcclient

GitHub repo からの Python クライアントのインストール

以下のステップを実行します。

  1. クライアント・コードをコピーするフォルダーを作成します。

    mkdir python-client && cd python-client
    
  2. リポジトリーを複製します。

    git clone https://github.com/sysdiglabs/sysdig-sdk-python.git
    
  3. 空の Python スクリプトを作成します。

    touch script.py
    
  4. フォルダー python-sdc-client/script.py の内容を検証するとき、python-client がリストされていることを確認します。

    ls
    

ステップ 2 Python スクリプトで Python クライアントを参照する

pip を使用してインストールされた Python クライアントの参照

Python スクリプトに以下のステートメントを追加することによって、Python クライアントを参照できます。

from sdcclient import IbmAuthHelper, SdMonitorClient

GitHub repo のクローンを作成することによってインストールされた Python クライアントの参照

以下の項目を Python スクリプトに追加して、GitHub クローン・パッケージをスクリプトにインポートします。

import sys
sys.path.append("python-sdc-client")
from sdcclient import IbmAuthHelper, SdMonitorClient

ステップ 3。 Monitoring Python クライアントのインスタンス化

オプション 1: IAMを使用してユーザーまたはサービスIDを認証する

IBM Cloud IAM 認証を Python クライアントで使用するには、Monitoring エンドポイント、API キー、および GUID を IBM Cloud Monitoring インスタンスから指定する必要があります。

端末から以下のステップを実行します。

  1. IBM Cloud Monitoring インスタンスの GUID を取得します。 以下のコマンドを実行します。

    ibmcloud resource service-instance <NAME> --output json | jq -r '.[].guid'
    
  2. API キーを取得します。 以下のコマンドを実行して、ユーザー API キーを生成します。

    ibmcloud iam api-key-create KEY_NAME
    
  3. モニタリング・インスタンスが使用可能なリージョンのエンドポイントを取得します。

    使用可能なエンドポイントのリストを表示する方法については、Monitoring エンドポイントを参照してください。

    例えば、us-south で使用可能なインスタンスのエンドポイントは https://us-south.monitoring.cloud.ibm.com です。

  4. 以下の項目を Python スクリプトに追加します。

    from sdcclient import IbmAuthHelper, SdMonitorClient
    
    URL = <MONITORING_ENDPOINT>
    # For example: URL = 'https://us-south.monitoring.cloud.ibm.com'
    
    APIKEY = <IAM_APIKEY>
    
    GUID = <GUID>
    
    ibm_headers = IbmAuthHelper.get_headers(URL, APIKEY, GUID)
    sdclient = SdMonitorClient(sdc_url=URL, custom_headers=ibm_headers)
    

    説明

    <MONITORING_ENDPOINT> は、モニタリング・インスタンスが利用可能なエンドポイントに置き換える必要があります。

    <IAM_APIKEY> を有効なIAM APIキーに置き換える必要がある。 詳細はこちら

    <GUID> は、前のステップで取得したモニタリング・インスタンスのGUIDに置き換える必要があります。

これで、sdclient を使用して、IAM を使用することにより認証されるアクションを実行できるようになりました。

エラー 400 Client Error: Bad Request for url: https://iam.cloud.ibm.com/identity/token が表示された場合は、API キーを確認してください。 渡している値が無効です。

オプション 2: Monitoring API トークンで認証する

Monitoring エンドポイントと、Python クライアントを使用して管理するチームに関連付けられている Monitoring API トークンを指定する必要があります。

端末から以下のステップを実行します。

  1. Monitoring API トークンを取得します

  2. モニタリング・インスタンスが使用可能なリージョンのエンドポイントを取得します。

    使用可能なエンドポイントのリストを表示する方法については、Monitoring エンドポイントを参照してください。

    例えば、us-south で使用可能なインスタンスのエンドポイントは https://us-south.monitoring.cloud.ibm.com です。

  3. 以下の項目を Python スクリプトに追加します。

    from sdcclient import IbmAuthHelper, SdMonitorClient
    
    URL = <MONITORING_ENDPOINT>
    # For example: URL = 'https://us-south.monitoring.cloud.ibm.com'
    
    MONITOR_TOKEN = <MONITOR_TOKEN>
    # For example: MONITOR_TOKEN = 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
    
    sdclient = SdMonitorClient(token=MONITOR_TOKEN,sdc_url=URL)
    

    説明

    <MONITORING_ENDPOINT> は、モニタリング・インスタンスが利用可能なエンドポイントに置き換える必要があります。

    <MONITOR_TOKEN> をMonitoring APIトークンに置き換える必要があります。

これで、sdclient を使用して、IAM を使用することにより認証されるアクションを実行できるようになりました。

サンプル1。 Python IAM API キーを使用するスクリプト

#!/usr/bin/env python3

import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
from sdcclient import IbmAuthHelper, SdMonitorClient

# Parse arguments.
def usage():
   print('usage: %s <ENDPOINT_URL> <API_KEY> <INSTANCE_GUID>' % sys.argv[0])
   print('ENDPOINT_URL: IBM Cloud endpoint URL (e.g. https://us-south.monitoring.cloud.ibm.com')
   print('API_KEY: IBM Cloud IAM API key. This key is used to retrieve an IAM access token.')
   print('INSTANCE_GUID: GUID of an IBM Cloud Monitoring.')
   sys.exit(1)

if len(sys.argv) != 4:
   usage()

URL = sys.argv[1]
APIKEY = sys.argv[2]
GUID = sys.argv[3]


# Instantiate the client
ibm_headers = IbmAuthHelper.get_headers(URL, APIKEY, GUID)
sdclient = SdMonitorClient(sdc_url=URL, custom_headers=ibm_headers)

サンプル2。 Python Monitoring API トークンを使用するスクリプト

#!/usr/bin/env python3

import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))

from sdcclient import IbmAuthHelper, SdMonitorClient

# Parse arguments.
def usage():
   print('usage: %s <ENDPOINT_URL> <MONITOR_TOKEN> <INSTANCE_GUID>' % sys.argv[0])
   print('ENDPOINT_URL: IBM Cloud endpoint URL (e.g. https://us-south.monitoring.cloud.ibm.com')
   print('MONITOR_TOKEN: token that is associated to a team.')
   sys.exit(1)

if len(sys.argv) != 3:
   usage()

URL = sys.argv[1]
MONITOR_TOKEN = sys.argv[2]

# Instantiate the client
sdclient = SdMonitorClient(token=MONITOR_TOKEN,sdc_url=URL)

参照