Integrate EM Metrics With Third-Party Tools Using REST APIs

I want to share how to use Enterprise Manager’s (EM) REST APIs to query metric data and integrate with 3rd party tools.

First of all, always follow the documentation. Below is the link to the EM’s REST API functionality.



Using the EM’s REST API functionality we can query the EM repository using a web service. Let’s follow below steps.

a) Create a new EM user who is going to have the privileges to query the EM’s repository. Navigate to Setup -> Security -> Administrators.

Administrators navigation

b) Create a new Repository User.

Administrator creation

c) I’m going to create a new Administrator named ABC.

Administrator creation

d) Create a new Named Credential. Navigate to Setup -> Security -> Named Credentials.

Named Credential creation

e) Click on Create.

Named Credential creation

f) I’m going to create NC_ABC Named Credential.

Named Credential creation

g) Enable the repository REST APIs in EM. For that execute below emctl command.

emctl set property -name oracle.sysman.db.restfulapi.executesql.repository.query.enable -value true -sysman_pwd "sysman"

Now is time to test our REST APIs.

I’m going to test them out using 2 different mechanisms. For the first one, let’s use CURL.

$ curl -k -u ABC -H "Content-Type: application/json" -d '{"sqlStatement": "SELECT * FROM sysman.MGMT$TARGET_METRIC_SETTINGS", "credential": {"DBCredsMonitoring":"NC_ABC"}, "maxRowLimit": 10, "maxColumnLimit": 2}' https://<EM_Host>:<EM Port>/em/websvcs/restful/emws/oracle.sysman.db/executesql/repository/query/v1

Here are the results based on my demo environment:

REST API results from CURL

The second test is by using Ansible. If you are not familiar on how to configure Ansible in order to work with EM, please follow the Oracle Live Labs that we created about this.



I’m going to create a new YAML file (test.yml) in order to test the REST API.

---
- name: Using a REST API
  become: false
  hosts: emserver
  gather_facts: false
  tasks:

    - name: Query Metric Settings
      uri:
        url: https://emcc.marketplace.com:7803/em/websvcs/restful/emws/oracle.sysman.db/executesql/repository/query/v1
        method: POST
        return_content: yes
        force_basic_auth: yes
        validate_certs: no
        body_format: json
        url_username: ABC
        url_password: *******
        headers:
           Content-Type: application/json
        body:
          {
            "sqlStatement": "SELECT * FROM sysman.MGMT$TARGET_METRIC_SETTINGS", "maxRowLimit": 2, "maxColumnLimit": 29
          }
      register: results

    - name: Print returned json dictionary
      debug:
        var: results.json

Let’s execute this using Ansible.

ansible-playbook /home/oracle/ansible/yml/test.yml -u oracle --private-key=~/.ssh/dtkey.ssh

Here the results:

REST API results from Ansible

With this you can get the desired results from the EM’s repository using this REST API.

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