IBM Cloud Docs
Extracting metrics from an instance by using the Monitoring Python client

Extracting metrics from an instance by using the Monitoring Python client

You can extract metrics from an IBM Cloud Monitoring instance through REST API operations that you can run by using a Python client or by using a cURL command.

Get metrics by using a Python client

To learn how to use the Python client, see Using the Python client.

The following code shows the structure of a Python script that you can use to retrieve metrics from a Monitoring instance:

# 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)

You must include the following information: <MONITORING-ENDPOINT>, <IAM_APIKEY>, and <GUID> These data is required to authenticate the request with the monitoring instance. To get the monitoring instance information, see Authenticate your user or service ID by using IAM.

Samples

For Python examples, see any of the following examples:

  • Example 1: This sample script shows how to get data by creating a request that has no filter and no segmentation.

  • Example 2: This sample script shows how to get data by creating a request that has a filter and segmentation.

  • Example 3: This sample script shows how to get data by creating a request where you specify the datasource.

Metrics dictionary

To see the pre-defined metrics, see Metrics dictionary.

To see the pre-defined metrics that are defined by IBM Cloud services that are enabled for Monitoring, see Cloud services.

Data aggregation

To learn about data aggregation, see Data Aggregation.