Problem getting metrics from Dynatrace Managed

I am trying to utilize this python script:

import requests
import csv

DYNATRACE_BASE_URL = ‘https://your-dynatrace-instance.com/api/v1’
API_TOKEN = ‘your-api-token’
MANAGEMENT_ZONE_ID = ‘your-management-zone-id’

headers = {
‘Authorization’: ‘Api-Token {}’.format(API_TOKEN)
}

def get_metrics(management_zone_id):
endpoint = ‘{}/metrics’.format(DYNATRACE_BASE_URL)
params = {
‘managementZone’: management_zone_id
}
response = requests.get(endpoint, headers=headers, params=params)
response.raise_for_status()
return response.json()

def export_metrics_to_csv(metrics, filename=‘metrics.csv’):
if not metrics:
print(‘No metrics to export.’)
return
headers = metrics[0].keys()
with open(filename, ‘w’, newline=‘’) as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=headers)
writer.writeheader()
writer.writerows(metrics)
print(‘Metrics exported to {}’.format(filename))

def main():
try:
metrics_data = get_metrics(MANAGEMENT_ZONE_ID)
metrics = metrics_data.get(‘metrics’, [])
if not metrics:
print(‘No metrics found for the specified management zone.’)
return
export_metrics_to_csv(metrics)
except Exception as e:
print(‘Error occurred: {}’.format(e))

if __name__ == ‘__main__’:
main()

But on the execution, it says “the hostname does not match either of *.ypz572.dynatrace-managed.com”

my hostname is dynatrace.app.intra.pfs.local

Hi,

Which metrics you are interested in?

Best regards

Number of web service requests and response time for each of the element of the management zone,

Hi,

Easier way to me is from Data Explorer:

  1. Select metrics you are interested. Those are built-in.
  2. Split both by service.
  3. Filter by management zone.
  4. Change to table visualization.
  5. Execute query.

Select “…” and “Copy request”:

You will have a way to export information in JSON or CSV, just input your token:

Notice it is filtered by “mzId”, you can filter also by “mzName” if you prefer.

This would be for one specific managment zone. If you need all of them:

  1. Make a previous call to management zones APIs to reach all of them.
  2. Make a loop to repeat similar GET request replacing mzIds.

Best regards