Module 8 — Automating with the Dynatrace Managed API: environment API, Cluster API and configuration as code

Automate with the Dynatrace Managed API: create tightly scoped tokens, query the environment API and the Cluster API, then version your configuration.
Module 8 — Automatisation par API en Managed : API d'environnement, API Cluster et configuration as code

Module 8 of the course turns to the Dynatrace Managed API: tokens, scripts and configuration as code instead of manual settings.

Course: Dynatrace Managed · Module 8/8 · Duration: ~75 min · Level: Advanced

Objectives:

  • Distinguish the environment API from the Cluster API, and know which one to use.
  • Create an API token with minimal scope and make your first calls.
  • Read entities, metrics and problems from a script.
  • Understand the configuration-as-code approach and its tools.

Prerequisites: modules 1 to 7, a test Managed environment, permission to create API tokens, a workstation with curl and jq, and a basic knowledge of JSON and Git.

Everything you configured by hand during this course (tags, management zones, alerting profiles, environments) can be created, read and checked programmatically. This last module of the Dynatrace Managed course is dedicated to automation with the Dynatrace Managed API: the two API families, tokens, first scripts, then the move to configuration as code so you can version your settings like application code.

It follows directly from module 7, whose operational checks are good candidates for automation. The course overview lists all the modules.

Which Dynatrace Managed API should you use for automation?

A Managed deployment exposes two API families, with separate addresses and tokens:

API Base address Use
Environment API (v1 and v2) https://{your-domain}/e/{environment-id}/api/… Data and configuration of an environment: entities, metrics, problems, settings, tokens
Cluster API (v1 and v2) https://{your-domain}/api/cluster/… Cluster administration: environments, nodes, users and groups, quotas

Simple rule: if the task concerns what is being monitored, use the environment API; if it concerns the platform itself, use the Cluster API. Tokens for one do not work on the other.

Creating an API token with the right scope

Environment tokens are created in the environment settings, Cluster tokens in the CMC. In both cases, tick only the permissions (scopes) you need, give the token an explicit name (“audit-read”, “pipeline-config-test”) and store it in a secrets vault, never in a Git repository. The token is sent in the HTTP header:

Authorization: Api-Token {your-token}

For the first calls in this module, a read-only environment token is enough, with read permissions for entities, metrics, problems and settings. The exact names are shown on the token creation screen.

Reading data with the environment API

A few useful calls, to adapt to your domain and environment ID:

export DT_URL="https://{your-domain}/e/{environment-id}"
export DT_TOKEN="..."   # load from your secrets vault

# List monitored hosts
curl -s -H "Authorization: Api-Token $DT_TOKEN" \
  "$DT_URL/api/v2/entities?entitySelector=type(%22HOST%22)" | jq '.entities[].displayName'

# List open or recent problems
curl -s -H "Authorization: Api-Token $DT_TOKEN" \
  "$DT_URL/api/v2/problems" | jq '.problems[] | {title, severityLevel, status}'

# Query a metric
curl -s -H "Authorization: Api-Token $DT_TOKEN" \
  "$DT_URL/api/v2/metrics/query?metricSelector=builtin:host.cpu.usage&from=now-2h"

Large responses are paginated: follow the next-page key returned by the API until there are no more. Also check the HTTP status code of every call in your scripts; a 401 or 403 almost always means an invalid token or a missing permission.

Administering with the Cluster API

The Cluster API is mainly used to industrialise environment creation, group management and node status checks before maintenance. A typical first call lists the environments:

curl -s -H "Authorization: Api-Token $CLUSTER_TOKEN" \
  "https://{your-domain}/api/cluster/v2/environments" | jq

Write your scripts to be idempotent: check whether the object exists before creating it, so that you can rerun the script without side effects. The article Automating administration with the Dynatrace Managed Cluster API walks through several complete scenarios.

Moving to configuration as code

Individual calls quickly show their limits: you have to manage creation order, dependencies (an alerting profile that references a management zone) and promotion from one environment to another. Configuration as code addresses these needs: the configuration is described in versioned files, reviewed in code review and applied by a tool.

Tool Principle When to choose it
Monaco (Dynatrace Configuration as Code) Open-source CLI maintained by Dynatrace, configurations in YAML and JSON, deployment to one or more environments Teams that want a tool dedicated to Dynatrace
Dynatrace Terraform provider Terraform resources for Dynatrace configuration Organisations already using Terraform
In-house scripts Direct API calls One-off or very specific needs

Before choosing, check in each tool’s documentation its compatibility with your Managed version and the configuration types it supports. In every case, the target pattern is the same: Git repository, review, and a pipeline that applies to test first, then to production.

dynatrace-config-repo/
├── test/
│   ├── management-zones/
│   ├── auto-tagging/
│   └── alerting-profiles/
└── prod/
    └── ...

For tags and zones, the article Management zones and automatic tagging shows how to create them through the settings API.

Hands-on exercise

Step 1 — Create a read-only token

In your test environment, create a token named training-read with read permissions only (entities, metrics, problems).

Expected result: a token displayed only once, which you store in your secrets manager.

Step 2 — List the hosts

Run the entities call above.

Expected result: the list of host names, identical to the one in the interface.

Step 3 — Produce a problem report

Adapt the problems call to output a CSV file (title, severity, status) with jq -r.

Expected result: a file you can open in a spreadsheet and could send every Monday.

Step 4 — Check the permissions

Attempt a write (for example creating a setting) with the same token.

Expected result: a refusal (403 code), proof that the token’s scope is properly limited.

Step 5 — Export a configuration

With Monaco or the Terraform provider, following their documentation, export the management zones of the test environment into a local Git repository.

Expected result: files describing your zones, ready to be reviewed and reapplied.

Quiz

  1. Which API do you use to create a new environment?
    A. The environment API · B. The Cluster API · C. The OneAgent API
  2. How is the token sent in a call?
    A. In the Authorization: Api-Token … header · B. In the domain name · C. In an interface session cookie
  3. A script receives a 403 code. What is the most likely cause?
    A. The cluster is switched off · B. The token lacks the required permission · C. The metric does not exist
  4. What is the main benefit of configuration as code?
    A. It replaces monitoring · B. It versions, reviews and replays configuration reproducibly · C. It removes the need for tokens

Answers

  1. B. Creating environments is part of cluster administration.
  2. A. The Authorization header with the Api-Token prefix carries the token.
  3. B. A 403 means access denied, most often a permission missing from the token.
  4. B. Versioned files make every change traceable and reproducible from one environment to another.

Further reading

← Module 7: Operating the cluster · Course overview →

Frequently asked questions

Should I prefer API v2 over v1?

For new scripts, yes, when the equivalent exists in v2. Some configuration operations remain available only in v1 depending on the version: check the documentation for your version.

Can I use the same token for everything?

It is not recommended. One token per use, with minimal scope, limits the impact of a leak and makes rotation easier.

Do scripts written for Managed work on SaaS?

Part of the environment API is shared, but the latest-generation SaaS platform offers other mechanisms and has no Cluster API. The Dynatrace SaaS course covers them separately.

This module completes the course: you now know how to install, deploy, explore, alert, organise, operate and automate a Dynatrace Managed environment. Go back to the overview to revise a module or prepare for an official certification.

span.observer is an independent community blog, not affiliated with Dynatrace LLC. This course does not replace the official Dynatrace University training and certifications. Check features in the official documentation for your version.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
Module 7 — Exploitation : cluster Managed, santé, sauvegardes, mises à jour et stockage

Module 7 — Dynatrace Managed cluster operations: health, backups, updates and storage

Next Post
Formation Dynatrace SaaS : le parcours complet en 8 modules

Dynatrace SaaS training: a free Dynatrace course in 8 modules

Related Posts