dt is a Python client for the Dynatrace Rest API.
It focuses on ease of use and nice type hints, perfect to explore the API and create quick scripts
$ pip install dtfrom dynatrace import Dynatrace
from dynatrace import TOO_MANY_REQUESTS_WAIT
from dynatrace.environment_v2.tokens_api import SCOPE_METRICS_READ, SCOPE_METRICS_INGEST
from dynatrace.configuration_v1.credential_vault import PublicCertificateCredentials
from dynatrace.environment_v2.settings import SettingsObject, SettingsObjectCreate
from datetime import datetime, timedelta
# Create a Dynatrace client
dt = Dynatrace("environment_url", "api_token")
# Create a client that handles too many requests (429)
# dt = Dynatrace("environment_url", "api_token", too_many_requests_strategy=TOO_MANY_REQUESTS_WAIT )
# Create a client that automatically retries on errors, up to 5 times, with a 1 second delay between retries
# dt = Dynatrace("environment_url", "api_token", retries=5, retry_delay_ms=1000 )
# Create a client with a custom HTTP timeout of 10 seconds
# dt = Dynatrace("environment_url", "api_token", timeout=10 )
# Get all hosts and some properties
for entity in dt.entities.list('type("HOST")', fields="properties.memoryTotal,properties.monitoringMode"):
    print(entity.entity_id, entity.display_name, entity.properties)
# Get idle CPU for all hosts
for metric in dt.metrics.query("builtin:host.cpu.idle", resolution="Inf"):
    print(metric)
# Print dimensions, timestamp and values for the AWS Billing Metric
for metric in dt.metrics.query("ext:cloud.aws.billing.estimatedChargesByRegionCurrency"):
    for data in metric.data:
        for timestamp, value in zip(data.timestamps, data.values):
            print(data.dimensions, timestamp, value)
# Get all ActiveGates
for ag in dt.activegates.list():
    print(ag)
# Get metric descriptions for all host metrics
for m in dt.metrics.list("builtin:host.*"):
    print(m)
# Delete endpoints that contain the word test
for plugin in dt.plugins.list():
    # This could also be dt.get_endpoints(plugin.id)
    for endpoint in plugin.endpoints:
        if "test" in endpoint.name:
            endpoint.delete(plugin.id)
# Prints dashboard ID, owner and number of tiles
for dashboard in dt.dashboards.list():
    full_dashboard = dashboard.get_full_dashboard()
    print(full_dashboard.id, dashboard.owner, len(full_dashboard.tiles))
# Delete API Tokens that haven't been used for more than 3 months
for token in dt.tokens.list(fields="+lastUsedDate,+scopes"):
    if token.last_used_date and token.last_used_date < datetime.now() - timedelta(days=90):
        print(f"Deleting token! {token}, last used date: {token.last_used_date}")
# Create an API Token that can read and ingest metrics
new_token = dt.tokens.create("metrics_token", scopes=[SCOPE_METRICS_READ, SCOPE_METRICS_INGEST])
print(new_token.token)
# Upload a public PEM certificate to the Credential Vault
with open("ca.pem", "r") as f:
    ca_cert = f.read()
my_cred = PublicCertificateCredentials(
    name="my_cred",
    description="my_cred description",
    scope="EXTENSION",
    owner_access_only=False,
    certificate=ca_cert,
    password="",
    credential_type="PUBLIC_CERTIFICATE",
    certificate_format="PEM"
)
r = dt.credentials.post(my_cred)
print(r.id)
# Create a new settings 2.0 object
settings_value = {
    "enabled": True,
    "summary": "DT API TEST 1",
    "queryDefinition": {
        "type": "METRIC_KEY",
        "metricKey": "netapp.ontap.node.fru.state",
        "aggregation": "AVG",
        "entityFilter": {
            "dimensionKey": "dt.entity.netapp_ontap:fru",
            "conditions": [],
        },
        "dimensionFilter": [],
    },
    "modelProperties": {
        "type": "STATIC_THRESHOLD",
        "threshold": 100.0,
        "alertOnNoData": False,
        "alertCondition": "BELOW",
        "violatingSamples": 3,
        "samples": 5,
        "dealertingSamples": 5,
    },
    "eventTemplate": {
        "title": "OnTap {dims:type} {dims:fru_id} is in Error State",
        "description": "OnTap field replaceable unit (FRU) {dims:type} with id {dims:fru_id} on node {dims:node} in cluster {dims:cluster} is in an error state.\n",
        "eventType": "RESOURCE",
        "davisMerge": True,
        "metadata": [],
    },
    "eventEntityDimensionKey": "dt.entity.netapp_ontap:fru",
}
settings_object = SettingsObjectCreate(schema_id="builtin:anomaly-detection.metric-events", value=settings_value, scope="environment")
dt.settings.create_object(validate_only=False, body=settings_object)| API | Level | Access | 
|---|---|---|
| Access Tokens - API tokens | ✔️ | dt.tokens | 
| Access tokens - Tenant tokens | ✔️ | dt.tenant_tokens | 
| ActiveGates | ✔️ | dt.activegates | 
| ActiveGates - Auto-update configuration | ✔️ | dt.activegates_autoupdate_configuration | 
| ActiveGates - Auto-update jobs | ✔️ | dt.activegates_autoupdate_jobs | 
| ActiveGates - Remote configuration | ✔️ | dt.activegates_remote_configuration | 
| Audit Logs | ✔️ | dt.audit_logs | 
| Events | dt.events_v2 | |
| Extensions 2.0 | ✔️ | dt.extensions_v2 | 
| Logs | dt.logs | |
| Metrics | ✔️ | dt.metrics | 
| Monitored entities | dt.entities | |
| Monitored entities - Custom tags | ✔️ | dt.custom_tags | 
| Network zones | dt.network_zones | |
| OneAgents - Remote configuration | ✔️ | dt.oneagents_remote_configuration | 
| Problems | ✔️ | dt.problems | 
| Security problems | ❌ | |
| Service-level objectives | ✔️ | dt.slos | 
| Settings | dt.settings | 
| API | Level | Access | 
|---|---|---|
| Anonymization | ❌ | |
| Cluster time | ✔️ | dt.cluster_time | 
| Cluster version | ❌ | |
| Custom devices | ✔️ | dt.custom_devices | 
| Deployment | ✔️ | dt.deployment | 
| Events | dt.events | |
| JavaScript tag management | ❌ | |
| Log monitoring - Custom devices | ❌ | |
| Log monitoring - Hosts | ❌ | |
| Log monitoring - Process groups | ❌ | |
| Maintenance window | ❌ | |
| OneAgent on a host | dt.oneagents | |
| Problem | ❌ | |
| Synthetic - Locations and nodes | ❌ | |
| Synthetic - Monitors | dt.synthetic_monitors | |
| Synthetic - Third party | ✔️ | dt.third_part_synthetic_tests | 
| Threshold | ❌ | |
| Timeseries | dt.timeseries | |
| Tokens | ❌ | |
| Topology & Smartscape - Application | ❌ | |
| Topology & Smartscape - Custom device | dt.custom_devices | |
| Topology & Smartscape - Host | dt.smartscape_hosts | |
| Topology & Smartscape - Process | ❌ | |
| Topology & Smartscape - Process group | ❌ | |
| Topology & Smartscape - Service | ❌ | |
| User sessions | ❌ | 
| API | Level | Access | 
|---|---|---|
| Alerting Profiles | dt.alerting_profiles | |
| Anomaly detection - Applications | ❌ | |
| Anomaly detection - AWS | ❌ | |
| Anomaly detection - Database services | ❌ | |
| Anomaly detection - Disk events | ❌ | |
| Anomaly detection - Hosts | ❌ | |
| Anomaly detection - Metric events | dt.anomaly_detection_metric_events | |
| Anomaly detection - Process groups | dt.anomaly_detection_process_groups | |
| Anomaly detection - Services | ❌ | |
| Anomaly detection - VMware | ❌ | |
| Automatically applied tags | dt.auto_tags | |
| AWS credentials configuration | ❌ | |
| AWS PrivateLink | ❌ | |
| Azure credentials configuration | ❌ | |
| Calculated metrics - Log monitoring | ❌ | |
| Calculated metrics - Mobile & custom applications | ❌ | |
| Calculated metrics - Services | ❌ | |
| Calculated metrics - Synthetic | ❌ | |
| Calculated metrics - Web applications | ❌ | |
| Cloud Foundry credentials configuration | ❌ | |
| Conditional naming | ❌ | |
| Credential vault | ❌ | |
| Custom tags | ✔️ | dt.custom_tags | 
| Dashboards | dt.dashboards | |
| Data privacy and security | ❌ | |
| Extensions | ✔️ | dt.extensions | 
| Frequent issue detection | ❌ | |
| Kubernetes credentials configuration | ❌ | |
| Maintenance windows | dt.maintenance_windows | |
| Management zones | dt.management_zones | |
| Notifications | dt.notifications | |
| OneAgent - Environment-wide configuration | ✔️ | dt.oneagents_config_environment | 
| OneAgent in a host group | ✔️ | dt.oneagents_config_hostgroup | 
| OneAgent on a host | ✔️ | dt.oneagents_config_host | 
| Plugins | dt.plugins | |
| Remote environments | ❌ | |
| Reports | ❌ | |
| RUM - Allowed beacon origins for CORS | ❌ | |
| RUM - Application detection rules | ❌ | |
| RUM - Application detection rules - Host detection | ❌ | |
| RUM - Content resources | ❌ | |
| RUM - Geographic regions - custom client IP headers | ❌ | |
| RUM - Geographic regions - IP address mapping | ❌ | |
| RUM - Mobile and custom application configuration | ❌ | |
| RUM - Web application configuration | ❌ | |
| Service - Custom services | ❌ | |
| Service - Detection full web request | ❌ | |
| Service - Detection full web service | ❌ | |
| Service - Detection opaque and external web request | ❌ | |
| Service - Detection opaque and external web service | ❌ | |
| Service - Failure detection parameter sets | ❌ | |
| Service - Failure detection rules | ❌ | |
| Service - IBM MQ tracing | ❌ | |
| Service - Request attributes | ❌ | |
| Service - Request naming | ❌ |