Automation for OCI Stack Monitoring

Karthic
2 min readNov 28, 2023

--

In this blog we will see some automation scripts while using Oracle cloud infrastructure Stack Monitoring

NOTE : Please test the script before using it in your production environment

  1. Discovery of existing hosts in Stack Monitoring.
#To enable Stack Monitoring for OCI hosts where agent is active and no plugin is enabled.
import oci

## For local use
config = oci.config.from_file("<config path>")

#Replace None with Tenancy OCID to enable stack monitoring for all hosts in the tenancy
TENANCY_ID = None

stack_monitoring_client = oci.stack_monitoring.StackMonitoringClient(config)
management_agent_client = oci.management_agent.ManagementAgentClient(config)

#user should have permission to list maangement agents at tenancy level
if TENANCY_ID is not None:
list_management_agents_response = management_agent_client.list_management_agents(
compartment_id=TENANCY_ID,
availability_status="ACTIVE",
install_type="AGENT",
plugin_name="None",
is_customer_deployed=False,
compartment_id_in_subtree=True,
access_level="ACCESSIBLE")

else:
COMPARTMENT_ID = input("Enter the compartment ocid to enable stack monitoring for all hosts in a compartment: ")
list_management_agents_response = management_agent_client.list_management_agents(
compartment_id=COMPARTMENT_ID,
availability_status="ACTIVE",
install_type="AGENT",
plugin_name="None",
is_customer_deployed=False)

for agent in list_management_agents_response.data:
try:
agent_id = agent.id
instance_name = agent.host
comp_id = agent.compartment_id
#print(f'Agent Id is {agent_id} and Instance name is {instance_name} and Compartment Id is {comp_id}')

create_discovery_job_response = stack_monitoring_client.create_discovery_job(
create_discovery_job_details=oci.stack_monitoring.models.CreateDiscoveryJobDetails(
compartment_id=comp_id,
discovery_details=oci.stack_monitoring.models.DiscoveryDetails(
agent_id=agent_id,
resource_type="HOST",
resource_name=instance_name,
properties=oci.stack_monitoring.models.PropertyDetails(
properties_map={})),
discovery_type="ADD",
discovery_client="DISCOVERY_UI"))
except Exception as e1:
print(e1)

2. For auto-promotion of hosts in a compartment create this config so new hosts created after this config will be automatically discovered . For this to work you need to have the below policy defined

ALLOW SERVICE appmgmt TO {STACK_MONITORING_DISCOVERY_JOB_CREATE,STACK_MONITORING_WORK_REQUEST_READ,STACK_MONITORING_CONFIG_INSPECT} IN COMPARTMENT <compartment_name>
ALLOW SERVICE appmgmt TO {MGMT_AGENT_DEPLOY_PLUGIN_CREATE, MGMT_AGENT_INSPECT, MGMT_AGENT_READ} IN COMPARTMENT <compartment_name>

#Enable auto-promote
export compartment_id=<compartment_ocid>
oci stack-monitoring config create-auto-promote-config --resource-type HOST --is-enabled true --compartment-id $compartment_id

3. How to delete all active stack monitoring resources in a compartment if no longer needed.

import oci

config = oci.config.from_file("<configfilepath>")
stack_monitoring_client = oci.stack_monitoring.StackMonitoringClient(config)

COMPARTMENT_ID = input("Enter the compartment ocid: ")

list_monitored_resources = stack_monitoring_client.list_monitored_resources(
compartment_id=COMPARTMENT_ID)
items = [item for item in list_monitored_resources.data.items if item.lifecycle_state == "ACTIVE"]
for x in items:
delete_monitored_resource = stack_monitoring_client.delete_monitored_resource(
monitored_resource_id=x.id,
is_delete_members=True)

--

--

No responses yet