cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Query on API - V1 to V2

smartpraseetha
Participant

Hello 

I have a custom script which stops the monitoring on the hosts based on some conditions.

I am using 2 APIS the first API gets all the hosts - /entity/infrastructure/hosts/ and I made an iteration in a loop that it gets entity {id} for each host and it feeds that to the second API v1  - /api/config/v1/hosts/{id}/monitoring

That is now V1 versions are in the  Deprecated state.

The alternate option given is to Manage host monitoring settings for /api/config/v1/hosts/{id}/monitoring is  via the settings endpoint with schemaId builtin:host.monitoring instead. But I dont get to see the same results to get the ways to pass the entity id nor get  the monitoring configuration of OneAgents.

I am wondering what is the purpose of schema and how could we use in the same level as the previous API ?

Also is there any reference that I  can have the API -  /api/config/v1/hosts/{id}/monitoring  in  V2 version  ?

 

2 REPLIES 2

PedroSantos
Frequent Guest

If I am understanding your question properly, you're aiming to use the V2 API for the same purpose you used the V1, which in this case is to disable host monitoring based on certain conditions with a custom script.

I know this endpoint works for fetching hosts:

 

curl -X GET "https://{environmentid}.live.dynatrace.com/api/v2/entities?entitySelector=type(HOST)" \
-H "Authorization: Api-Token <your-api-token>"

 

 

Then, you can use the settings/objects with the builtin:host.monitoring schema to update settings on a specific host.


@smartpraseetha wrote:

I am wondering what is the purpose of schema and how could we use in the same level as the previous API ?

Also is there any reference that I  can have the API -  /api/config/v1/hosts/{id}/monitoring  in  V2 version  ?


Here's a python script I've successfully used before to disable monitoring of hosts based on a condition, using the V2.

Essentialy, V2 uses a structured approach where settings are managed via configurations (schemas and settings objects).

 

import requests
import json

# Dynatrace environment and API token
dt_env = "placeholder"
api_token = "placeholder"

# Headers for API requests
headers = {
    "Authorization": f"Api-Token {api_token}",
    "Content-Type": "application/json"
}

# Define the condition function before using it
def some_condition(host):
    # Implement your condition logic here
    # Example: Disable monitoring for all hosts
    return True

# Step 1: Get all hosts (requires entities.read permission)
hosts_url = f"https://{environmentid}.live.dynatrace.com/api/v2/entities?entitySelector=type(HOST)"
response = requests.get(hosts_url, headers=headers)
if response.status_code == 200:
    hosts = response.json().get('entities', [])
    print("Fetched Hosts:")
    print(json.dumps(hosts, indent=2))
else:
    print(f"Failed to fetch hosts: {response.status_code} - {response.text}")
    hosts = []

# Step 2: Iterate over hosts and disable monitoring (requires settings.write permission)
for host in hosts:
    host_id = host['entityId']
    print(f"Processing host {host_id}")
    
    # Check some condition to decide whether to disable monitoring
    if some_condition(host):
        # Disable monitoring for the host
        settings_url = f"https://{environmentid}.live.dynatrace.com/api/v2/settings/objects"
        payload = [
            {
                "schemaId": "builtin:host.monitoring",
                "scope": host_id,  
                "value": {
                    "enabled": False  
                }
            }
        ]
        response = requests.post(settings_url, headers=headers, json=payload)
        if response.status_code == 200:
            print(f"Monitoring disabled for host {host_id}")
        else:
            print(f"Failed to disable monitoring for host {host_id}: {response.status_code} - {response.text}")
    else:
        print(f"Condition not met for host {host_id}, monitoring remains enabled")

 

 

smartpraseetha
Participant

@PedroSantos Thank you very much for the insights on the schema id so that means its needs to be combined with another api /api/v2/settings/objects" to pass the entity ids. It is clear to me now. 

Featured Posts