Python Extension Basics
When to Use Python Extensions
SNMP extensions are YAML-only. Python extensions let you write code for data sources SNMP can't reach:
| Data Source | Extension Type | Example |
|---|---|---|
| SNMP devices | SNMP (YAML only) | Cisco switches, UPS devices |
| REST APIs | Python | FortiSwitch, HP ILO (Redfish) |
| WSMAN/WMI | Python | Dell iDRAC server monitoring |
| CLI commands | Python | Cisco NX-API, SSH-based collection |
| Databases | Python | Custom SQL queries |
๐ก Same extension.yaml for topology/screens/dashboards. The difference is the data source โ Python code instead of SNMP OIDs.
Python SDK API (from dynatrace-extensions org)
The Extensions SDK provides these key methods:
from dynatrace_extension import Extension, Status, StatusValue
class MyExtension(Extension):
def query(self):
# Report a metric
self.report_metric("custom.my.metric", 42.0, dimensions={"host": "server1"})
# Report multiple metrics
for device in self.get_devices():
self.report_metric("custom.device.cpu", device.cpu,
dimensions={"device.name": device.name})
# Schedule periodic execution
# (configured in extension.yaml, not in code)
def initialize(self):
# Called once at startup
self.logger.info("Extension started")
def fastcheck(self):
# Health check โ return Status
return Status(StatusValue.OK)
Key SDK methods: report_metric(), report_event(), schedule(), logger.
Extension Structure
extension.zip/
โโโ extension.yaml โ topology, screens (same as SNMP)
โโโ lib/
โโโ your_extension-0.0.1-py3-none-any.whl
โโโ your_extension/
โโโ __init__.py
โโโ __main__.py โ your code
The Extension Class
from dynatrace_extension import Extension, Status, StatusValue
class MyExtension(Extension):
def query(self):
# Called every polling interval
group = self.activation_config.get("group_name")
self.report_metric(
"my_ext.cpu", 42.5,
{"device.address": "10.0.0.1"}
)
def main():
MyExtension().run()
report_metric(key, value, dimensions) โ this is how you send data to Dynatrace. Same metric keys as in extension.yaml.
Complete this extension.yaml for a Python extension. Add the python: runtime section with module my_api_monitor and set the CPU metric unit to Percent.
Key Methods
| Method | Purpose |
|---|---|
self.query() | Called every polling interval โ collect data here |
self.report_metric(key, value, dims) | Send a metric to Dynatrace |
self.activation_config.get(key) | Read monitoring config values |
self.report_dt_event(title, ...) | Send a custom event |
Status(StatusValue.OK) | Report extension health status |
This extension has a count metric with the wrong key. Count metrics must end in .count. Fix the requests metric key.
What's Next
Module 10 โ Connect to a real REST API and parse the response into metrics.