Kenji
Kenji

Reputation: 25

Monitoring azure-resources from Azure-Monitor by using Azure-Monitoring-libraries-for-python and getting these metrics

We are going to get some metrics from Azure Monitor.
I Installed the libraries and made a script to do that.
The script is following.

#! /usr/bin/env python
 
import datetime
from azure.mgmt.monitor import MonitorManagementClient
from azure.identity import ClientSecretCredential

subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
resource_group_name = 'xxxx-xxxxx'
vm_name = 'xxxxxxxxxx'
 
resource_id = (
    "subscriptions/{}/"
    "resourceGroups/{}/"
    "providers/Microsoft.Compute/virtualMachines/{}"
).format(subscription_id, resource_group_name, vm_name)
 
TENANT_ID = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
CLIENT = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
KEY = 'xxxxxxxxx'
credentials = ServicePrincipalCredentials(
    client_id = CLIENT,
    secret = KEY,
    tenant = TENANT_ID
)
 
client = MonitorManagementClient(
    credentials,
    subscription_id
)
 
today = datetime.datetime.now()
nexttime = today - datetime.timedelta(minutes=1)
 
metrics_data = client.metrics.list(
    resource_id,
    timespan="{}/{}".format(nexttime, today),
    interval='PT1M',
    metricnames='Percentage CPU',
    aggregation='average'
)
for item in metrics_data.value:
    for timeserie in item.timeseries:
        for data in timeserie.data:
            print("{}".format(data.average))

when I run this script, the result shows sometimes 'None',regardless of I can see the right value on Azure-Monitor.
When I run the script that getting metric from Azure Load Balancer, the script returns the right value.
This URL (https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/metrics-supported#microsoftcomputevirtualmachines) says the aggregation type of Percentage CPU is Average, so the script seems to be correct.
I don't know why this script of getting Percentage CPU doesn't return the same value as Azure-Monitor.
Does anyone know the solution?

Upvotes: 2

Views: 1136

Answers (1)

Stanley Gong
Stanley Gong

Reputation: 12153

Azure monitor logs metrics by UTC time, so you should use today = datetime.datetime.utcnow() to define your timespan.

Based on your code, you are querying the latest 1 min VM CPU Percentage, it will be some latency that Azure monitors log metrics, so maybe you can't get this value due to latency, so maybe you can try to get i,e last 5th min CPU Percentage.

Just try the code snippet below:

today = datetime.datetime.utcnow()
nexttime = today - datetime.timedelta(minutes=5)

query_timespan = "{}/{}".format(nexttime, today - datetime.timedelta(minutes=4))

print(query_timespan)

metrics_data = client.metrics.list(
    resource_id,
    timespan=query_timespan,
    interval='PT1M',
    metricnames='Percentage CPU',
    aggregation='average'
)
for item in metrics_data.value:
    for timeserie in item.timeseries:
        for data in timeserie.data:
            print("{}".format(data.average))

Result on my side:

enter image description here

Portal display(my timezone is UTC+8):

enter image description here

Upvotes: 2

Related Questions