Homeโ€บ๐Ÿ Python Extensionsโ€บModule 91 min read ยท 10/16

Python Extension Basics

Hands-on2 exercises

When to Use Python Extensions

SNMP extensions are YAML-only. Python extensions let you write code for data sources SNMP can't reach:

Data SourceExtension TypeExample
SNMP devicesSNMP (YAML only)Cisco switches, UPS devices
REST APIsPythonFortiSwitch, HP ILO (Redfish)
WSMAN/WMIPythonDell iDRAC server monitoring
CLI commandsPythonCisco NX-API, SSH-based collection
DatabasesPythonCustom 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.

๐Ÿ›  Try it

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.

extension.yamlYAML
Loading...

Key Methods

MethodPurpose
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
๐Ÿ›  Try itExercise 2

This extension has a count metric with the wrong key. Count metrics must end in .count. Fix the requests metric key.

extension.yamlYAML
Loading...

What's Next

Module 10 โ€” Connect to a real REST API and parse the response into metrics.