IBM Cloud Docs
使用 Monitoring Python 客户机从实例中抽取度量

使用 Monitoring Python 客户机从实例中抽取度量

您可以通过 REST API 操作从 IBM Cloud Monitoring 实例中抽取度量值,可以使用 Python 客户机或使用 cURL 命令来运行这些操作。

使用 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 认证用户或服务标识

样本

对于 Python 示例,请参阅以下任何示例:

  • 示例 1: 此样本脚本显示如何通过创建没有过滤器且没有分段的请求来获取数据。

  • 示例 2: 此样本脚本显示如何通过创建具有过滤器和分段的请求来获取数据。

度量字典

要查看预定义的度量,请参阅 度量字典

要查看已启用监视的 IBM Cloud 服务所定义的预定义度量,请参阅 云服务

数据聚集

要了解有关数据聚集的信息,请参阅 数据聚集