Monitoring API を使用したインスタンスからのメトリックの抽出
Python クライアントまたは cURL コマンドを使用して実行できる REST API 操作によって IBM Cloud Monitoring インスタンスからメトリックを抽出することができます。
Python クライアントを使用したメトリックの取得
Python クライアントの使用方法については、Python クライアントの使用を参照してください。
次のコードは、Monitoring インスタンスからメトリックを取得するために使用できる Python スクリプトの構造を示しています。
# Reference the Python client
from sdcclient import IbmAuthHelper, SdMonitorClient
# Add the monitoring instance information that is required for authentication
URL = <MONITORING-ENDPOINT>
APIKEY = <IAM_APIKEY>
GUID = <GUID>
ibm_headers = IbmAuthHelper.get_headers(URL, APIKEY, GUID)
# Instantiate the Python client
sdclient = SdMonitorClient(sdc_url=URL, custom_headers=ibm_headers)
# Specify the ID for keys, and ID with aggregation for values
metrics = [
{"id": "cpu.used.percent", "aggregations": {"time": "timeAvg", "group": "avg"}}
]
# Add a data filter or set to None if you want to see "everything"
filter = None
# Time window:
# - for "from A to B": start is equal to A, end is equal to B (expressed in seconds)
# - for "last X seconds": start is equal to -X, end is equal to 0
start = -600
end = 0
# Sampling time:
# - for time series: sampling is equal to the "width" of each data point (expressed in seconds)
# - for aggregated data (similar to bar charts, pie charts, tables, etc.): sampling is equal to 0
sampling = 60
# Load data
ok, res = sdclient.get_data(metrics, start, end, sampling, filter=filter)
if ok:
print(res)
以下の情報を含める必要があります: <MONITORING-ENDPOINT> <IAM_APIKEY>、および <GUID> これらのデータは、リクエストをモニタリングインスタンスで認証するために必要です。 モニター・インスタンスの情報を取得するには、IAM を使用してユーザー ID またはサービス ID を認証するを参照してください。
サンプル
Python の例については、以下のいずれかの例を参照してください。
メトリック・ディクショナリー
定義済みのメトリクスを参照するには、「 Metrics dictionary 」を参照してください。
モニタリングの IBM Cloud サービスによって定義されている事前定義メトリックを確認するには、クラウド・サービスを参照してください。
データ集約
データ集約については、 データ集約を参照。