使用 Monitoring Python 用戶端從實例擷取度量值
您可以透過 REST API 作業從 IBM Cloud Monitoring 實例擷取度量值,您可以使用 Python 用戶端或使用 cURL 指令來執行這些 REST API 作業。
使用 Python 用戶端取得度量值
若要瞭解如何使用 Python 用戶端,請參閱 使用 Python 用戶端。
下列程式碼顯示 Python Script 的結構,可用來從 Monitoring 實例擷取度量值:
# 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。
範例
如需 Python 範例,請參閱下列任何範例:
度量字典
若要查看預先定義的度量,請參閱 度量字典。
若要查看已啟用 Monitoring 之 IBM Cloud 服務定義的預先定義度量值,請參閱 雲端服務。
資料聚集
若要瞭解資料聚集,請參閱 資料聚集。