Extract OCI Monitoring Metrics Using REST APIs

In my previous post I showed you how you can create custom metrics in order to monitor a Standby database. Those steps detailed how to ingest (post) metrics to the OCI Monitoring service.



In this post I’ll show you how you can list metric definitions and how you can extract metric data from the Monitoring service. This metric extraction is really useful when you need to integrate the Monitoring service with 3rd party tools.

Step 1 – Prerequisites

Please follow all the prerequisites executed in the previous blog.

Step 2 – List Metrics

In this step we are going to create the Python script that list our custom metric.

Copy the code below and paste it into a file name list_metric.py

#!/usr/bin/python3

# This is an automatically generated code sample.
# To make this code sample work in your Oracle Cloud tenancy,
# please replace the values for any parameters whose current values do not fit
# your use case (such as resource IDs, strings containing EXAMPLE or unique_id, and
# boolean, number, and enum parameters with values not fitting your use case).

import oci

# Create a default config using DEFAULT profile in default location
# Refer to
# https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm#SDK_and_CLI_Configuration_File
# for more info
config = oci.config.from_file()


# Initialize service client with default config file
monitoring_client = oci.monitoring.MonitoringClient(config)


# Send the request to service, some parameters are not required, see API
# doc for more info
list_metrics_response = monitoring_client.list_metrics(
    compartment_id="<YOUR COMPARTMENT ID>",
    list_metrics_details=oci.monitoring.models.ListMetricsDetails(
        name="<YOUR METRIC NAME>",
        namespace="<YOUR CUSTOM NAMESPACE>"))


# Get the data from response
print(list_metrics_response.data)

Amend the inputs needed depending on your METRIC and OCI configuration:

  • <YOUR METRIC NAME>
  • <YOUR CUSTOM NAMESPACE>
  • <YOUR COMPARTMENT ID>

Let’s execute our script:

$ ./list_metric.py
[{
  "compartment_id": "<YOUR COMPARTMENT ID>",
  "dimensions": {
    "server_id": "<YOUR SERVER ID>"
  },
  "name": "<YOUR METRIC NAME>",
  "namespace": "<YOUR CUSTOM NAMESPACE>",
  "resource_group": null
}]

Step 3 – Query Metric Data

In this step we are going to create a Python script that query metric data.

Copy the code below and paste it into a file name query_metric.py

#!/usr/bin/python3

# This is an automatically generated code sample.
# To make this code sample work in your Oracle Cloud tenancy,
# please replace the values for any parameters whose current values do not fit
# your use case (such as resource IDs, strings containing EXAMPLE or unique_id, and
# boolean, number, and enum parameters with values not fitting your use case).

import oci
from datetime import datetime

# Create a default config using DEFAULT profile in default location
# Refer to
# https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm#SDK_and_CLI_Configuration_File
# for more info
config = oci.config.from_file()


# Initialize service client with default config file
monitoring_client = oci.monitoring.MonitoringClient(config)


# Send the request to service, some parameters are not required, see API
# doc for more info
summarize_metrics_data_response = monitoring_client.summarize_metrics_data(
    compartment_id="<YOUR COMPARTMENT ID>",
    summarize_metrics_data_details=oci.monitoring.models.SummarizeMetricsDataDetails(
        namespace="<YOUR CUSTOM NAMESPACE>",
        query="<YOUR METRIC NAME>[1m].mean()",
        start_time=datetime.strptime(
            "2023-10-11T14:38:22.574Z",
            "%Y-%m-%dT%H:%M:%S.%fZ"),
        end_time=datetime.strptime(
            "2023-10-11T17:27:08.471Z",
            "%Y-%m-%dT%H:%M:%S.%fZ"),
        resolution="5m"),
    compartment_id_in_subtree=False)

# Get the data from response
# Get the data from response
string = ' '.join([str(item) for item in summarize_metrics_data_response.data])
json_object = json.loads(string)
print(json_object["aggregated_datapoints"])

Amend the inputs needed depending on your METRIC and OCI configuration:

  • <YOUR CUSTOM NAMESPACE>
  • <YOUR COMPARTMENT ID>
  • <YOUR METRIC NAME>

Let’s execute our script:

$ ./query_metric.py
[{'timestamp': '2023-10-11T14:41:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T14:46:00+00:00', 'value': 0.016666667}, {'timestamp': '2023-10-11T14:51:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T14:56:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T15:01:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T15:06:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T15:11:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T15:16:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T15:21:00+00:00', 'value': 0.016666667}, {'timestamp': '2023-10-11T15:26:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T15:31:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T15:36:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T15:41:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T15:46:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T15:51:00+00:00', 'value': 0.016666667}, {'timestamp': '2023-10-11T15:56:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T16:01:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T16:06:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T16:11:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T16:16:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T16:21:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T16:26:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T16:31:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T16:36:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T16:41:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T16:46:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T16:51:00+00:00', 'value': 0.033333333}, {'timestamp': '2023-10-11T16:56:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T17:01:00+00:00', 'value': 0.016666667}, {'timestamp': '2023-10-11T17:06:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T17:11:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T17:16:00+00:00', 'value': 0.0}, {'timestamp': '2023-10-11T17:21:00+00:00', 'value': 0.016666667}]

Now that we have extracted our metric data set, we can spool it off to a local file or ingest it into a 3rd party tool.

I also want to highlight another blog from Kay Singh about Monitoring metric extraction.



Hope this blog shows you how to extract Monitoring metric data from OCI.

Thanks,
Alfredo