0

I am trying to collect the Performance of a Virtual Machine like CPU Utilization, Available Memory, Logical Disk MB/s, and Logical Disk IOPS, which can be seen under Insights via console. I want to collect these data and save them into a CSV file. Is there any API to get the data with Avg, Min, Max, 50th, 90th, and 95th included?

I have tried the following solutions:

  1. az monitor metrics command: az monitor metrics list --resource {ResourceName} --metric "Percentage CPU"

  2. API: https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Compute/virtualMachines/{vm_name}/providers/microsoft.insights/metrics?api-version=2018-01-01&metricnames=Percentage CPU&timespan={start_time}/{end_time}&interval=PT1H&aggregation=average

  3. Microsoft Azure Monitor Client Library (Python SDK): azure-mgmt-monitor

In all the above-mentioned approaches, Instead of CPU Utilization, I'm getting results of 'Percentage CPU', i.e., instead of Insights these approaches are giving metrics.

1 Answers1

1

One possible solution is to use the Azure Monitor REST API which allows you to collect various metrics from a virtual machine. You can specify the metric names, time span, interval, and aggregation parameters in the request URL. For example:

https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Compute/virtualMachines/{vm_name}/providers/microsoft.insights/metrics?api-version=2018-01-01&metricnames=Percentage CPU,Average Memory Bytes,Disk Read Bytes/sec,Disk Write Bytes/sec,Disk Read Operations/Sec,Disk Write Operations/Sec&timespan={start_time}/{end_time}&interval=PT1H&aggregation=average,count,maximum,minimun,total

This request will return the average, count, maximum, minimum, and total values for each metric in each hour within the specified time span. You can also use other aggregation types such as percentile.

Another possible solution is to use the Azure Monitor libraries for Python which provides a wrapper for the REST API. You can install the azure-mgmt-monitor package and use the list method in MetricsOperations class to get the metrics data. For example:

import datetime
from azure.mgmt.monitor import MonitorManagementClient

# Get the ARM id of your resource
resource_id = (
    "subscriptions/{}/"
    "resourceGroups/{}/"
    "providers/Microsoft.Compute/virtualMachines/{}"
).format(subscription_id, resource_group_name, vm_name)

# Get your credentials ready
credentials = ServicePrincipalCredentials(
    client_id = client_id,
    secret = secret,
    tenant = tenant_id
)

# Create a monitor client
monitor_client = MonitorManagementClient(
    credentials,
    subscription_id
)

# Get metrics data
metrics_data = monitor_client.metrics.list(
    resource_id,
    timespan="{}/{}".format(start_time,end_time),
    interval='PT1H',
    metricnames="Percentage CPU,Average Memory Bytes,Disk Read Bytes/sec,Disk Write Bytes/sec,Disk Read Operations/Sec,Disk Write Operations/Sec",
    aggregation="Average,count,maximum,minimun,total",
)

This code will return a similar result as the REST API request.

To save the metrics data into a CSV file, you can use Python’s built-in csv module or other libraries such as pandas. You can iterate over each metric value object in metrics_data.value and write its properties into a row of CSV file.

RodolfoGS
  • 36
  • 1
  • 5
  • Thanks for the suggestions. I have tried these solutions. As I mentioned that, I want "CPU Utilization", not "Percentage CPU". The solutions which you proposed are to get Percentage CPU. – Saiyam Jain Feb 16 '23 at 19:39