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 dt
from dynatrace import Dynatrace
from dynatrace import TOO_MANY_REQUESTS_WAIT
from dynatrace.environment_v2.tokens_api import SCOPE_METRICS_READ, SCOPE_METRICS_INGEST
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 )
# 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)
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 |
Audit Logs | ✔️ | dt.audit_logs |
Extensions 2.0 | ❌ | |
Metrics | ✔️ | dt.metrics |
Monitored entities | dt.entities |
|
Monitored entities - Custom tags | ❌ | |
Network zones | ❌ | |
Problems | ✔️ | dt.problems |
Security problems | ❌ | |
Service-level objectives | ❌ |
API | Level | Access |
---|---|---|
Anonymization | ❌ | |
Cluster time | ✔️ | dt.time |
Cluster version | ❌ | |
Deployment | ❌ | |
Events | dt.events |
|
JavaScript tag management | ❌ | |
Log monitoring - Custom devices | ❌ | |
Log monitoring - Hosts | ❌ | |
Log monitoring - Process groups | ❌ | |
Maintenance window | ❌ | |
OneAgent on a host | ❌ | |
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 | ||
Topology & Smartscape - Process | ❌ | |
Topology & Smartscape - Process group | ❌ | |
Topology & Smartscape - Service | ❌ | |
User sessions | ❌ |
API | Level | Access |
---|---|---|
Alerting Profiles | ❌ | |
Anomaly detection - Applications | ❌ | |
Anomaly detection - AWS | ❌ | |
Anomaly detection - Database services | ❌ | |
Anomaly detection - Disk events | ❌ | |
Anomaly detection - Hosts | ❌ | |
Anomaly detection - Metric events | ❌ | |
Anomaly detection - Process groups | ❌ | |
Anomaly detection - Services | ❌ | |
Anomaly detection - VMware | ❌ | |
Automatically applied 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 | ❌ | |
Dashboards | dt.dashboards |
|
Data privacy and security | ❌ | |
Extensions | ✔️ | dt.extensions |
Frequent issue detection | ❌ | |
Kubernetes credentials configuration | ❌ | |
Maintenance windows | ❌ | |
Management zones | ||
Notifications | dt.notifications |
|
OneAgent - Environment-wide configuration | ❌ | |
OneAgent in a host group | ❌ | |
OneAgent on a 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 | ❌ |