Extract OCI Resource Status Using APIs

In this blog, I want to show you a way to extract every resource in a compartment along with their status using a Python script and OCI APIs.

There are several pre-reqs that you need to fulfil in order to be able to connect using APIs to OCI. Follow the same pre-reqs as in my previous blog about Metric Extensions.

Step 1 – Prerequisites



Once you have created an OCI user, setup API Keys and setup your Python host; then, go ahead and create a Python script similar to the one below.

#!/usr/bin/python3

# This is a sample python script that searches resources in a compartment
# Run this script on the client that you want to monitor.
# Command: python script_name.py

import oci,subprocess,os,datetime,json
from pytz import timezone

# using default configuration file (~/.oci/config)
from oci.config import from_file
config = from_file()

# initialize service client with default config file

search_client = oci.resource_search.ResourceSearchClient(config)
query = f"query all resources where compartmentId = '<compartment OCID>' && lifeCycleState != 'AVAILABLE' && lifeCycleState != 'ACTIVE' && lifeCycleState != 'Assigned' && lifeCycleState != 'Running' && lifeCycleState != 'Succeeded' && lifeCycleState != 'Deleted'"
search_response = search_client.search_resources(
    search_details=oci.resource_search.models.StructuredSearchDetails(
        type="Structured",
        query=query,
    ),
    limit=1000,
)
print(f"Compartment has {len(search_response.data.items)} resources")

json_format = json.loads(str(search_response.data.items))

# Iterate through the JSON array
for item in json_format:
    print(item["display_name"], item["lifecycle_state"])

As you can see from the script. We are using OCI Search in order to get all resources in a compartment (<compartment OCID>) and also we all filtering the search to only show resources that are either down, terminated or failed.

Once you get the list of resources in your compartment you can create a text/csv file and share the list with all the interested parties.

Another option is the ability to combine this with the Monitoring Service and create a Metric Extension (ME). The metric extension will hold the list of resources that have that specific lifecycle_state. Once the date is contained in the ME you can send notifications or create a dashboard showing these resources.

Thanks,
Alfredo

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