Search code examples
azureazure-resource-managerazure-resource-group

get cpu utilization of virtual machines in azure using python sdk


I am trying to get CPU utilization of virtual machines using azure python APIs. like if a virtual machine has 2 cpus, i need overall utilization (means cpu1+ cpu2).

What are the possible methods to get cpu utilization of virtual machines?


Solution

  • Possibly you could use Azure Monitoring libraries for python to obtain the Percentage CPU metric on Azure VMs. Install azure-mgmt-monitor package and call the list methods in MetricsOperations class

    Example - Metrics

    import datetime
    from azure.mgmt.monitor import MonitorManagementClient
    
    # Get the ARM id of your resource. You might chose to do a "get"
    # using the according management or to build the URL directly
    # Example for a ARM VM
    resource_id = (
        "subscriptions/{}/"
        "resourceGroups/{}/"
        "providers/Microsoft.Compute/virtualMachines/{}"
    ).format(subscription_id, resource_group_name, vm_name)
    
    # create client
    client = MonitorManagementClient(
        credentials,
        subscription_id
    )
    
    # You can get the available metrics of this specific resource
    for metric in client.metric_definitions.list(resource_id):
        # azure.monitor.models.MetricDefinition
        print("{}: id={}, unit={}".format(
            metric.name.localized_value,
            metric.name.value,
            metric.unit
        ))
    
    # Example of result for a VM:
    # Percentage CPU: id=Percentage CPU, unit=Unit.percent
    # Network In: id=Network In, unit=Unit.bytes
    # Network Out: id=Network Out, unit=Unit.bytes
    # Disk Read Bytes: id=Disk Read Bytes, unit=Unit.bytes
    # Disk Write Bytes: id=Disk Write Bytes, unit=Unit.bytes
    # Disk Read Operations/Sec: id=Disk Read Operations/Sec, unit=Unit.count_per_second
    # Disk Write Operations/Sec: id=Disk Write Operations/Sec, unit=Unit.count_per_second
    
    # Get CPU total of yesterday for this VM, by hour
    
    today = datetime.datetime.now().date()
    yesterday = today - datetime.timedelta(days=1)
    
    metrics_data = client.metrics.list(
        resource_id,
        timespan="{}/{}".format(yesterday, today),
        interval='PT1H',
        metric='Percentage CPU',
        aggregation='Total'
    )
    
    for item in metrics_data.value:
        # azure.mgmt.monitor.models.Metric
        print("{} ({})".format(item.name.localized_value, item.unit.name))
        for timeserie in item.timeseries:
            for data in timeserie.data:
                # azure.mgmt.monitor.models.MetricData
                print("{}: {}".format(data.time_stamp, data.total))
    
    # Example of result:
    # Percentage CPU (percent)
    # 2016-11-16 00:00:00+00:00: 72.0
    # 2016-11-16 01:00:00+00:00: 90.59
    # 2016-11-16 02:00:00+00:00: 60.58
    # 2016-11-16 03:00:00+00:00: 65.78
    # 2016-11-16 04:00:00+00:00: 43.96
    # 2016-11-16 05:00:00+00:00: 43.96
    # 2016-11-16 06:00:00+00:00: 114.9
    # 2016-11-16 07:00:00+00:00: 45.4