toni
toni

Reputation: 133

Get metrics of an Azure vm in c#

I have the code below that retrieves CPU Percentage of an Azure vm. I want the metrics for Network In, Network Out, Disk Read Bytes, Disk Write Bytes, Disk Read Operations, Disk Write Operation. It doesn't seem to matter what I put for queryString, I always get cpu percentage. How can I get the other metrics?

private void test()
{
    string vmName = "myVM";
    string resourceId = "/subscriptions/{subscriptionId}/resourceGroups/ResourceGroupWest/providers/Microsoft.Compute/virtualMachines/" + vmName;
    var subscriptionId = "mySubscriptionID";
    var clientId = "myClientID";
    var secret = "myKey";
    var tenantId = "myTenantID";
    resourceId = resourceId.Replace("{subscriptionId}", subscriptionId);

    MonitorClient readOnlyClient = AuthenticateWithReadOnlyClient(tenantId, clientId, secret, subscriptionId).Result;

    string queryString = "name.value eq 'CpuPercentage'";
    ODataQuery<MetadataValue> odataQuery = new ODataQuery<MetadataValue>(queryString);
    var vals = readOnlyClient.Metrics.List(resourceId, odataQuery );
    foreach (MetricValue v in vals.Value[0].Timeseries[0].Data)
    {
        if (v.Average != null)
        {
            totalAverage += v.Average.Value;
        }
    }
    totalAverage = totalAverage / vals.Value[0].Timeseries[0].Data.Count;
}

private static async Task<MonitorClient> AuthenticateWithReadOnlyClient(string tenantId, string clientId, string secret, string subscriptionId)
{
    // Build the service credentials and Monitor client            
    var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, secret);            
    var monitorClient = new MonitorClient(serviceCreds);           
    monitorClient.SubscriptionId = subscriptionId;            
    return monitorClient;        
}

Upvotes: 0

Views: 1505

Answers (1)

Tom Sun
Tom Sun

Reputation: 24529

It doesn't seem to matter what I put for queryString, I always get cpu percentage. How can I get the other metrics?

You could get the other metrics name from Supported metrics with Azure Monitor. If you want to get multiple metrics you could use or to append the metrics. The CPU metrics is the default metrics of Azure VM

 var queryString = "(name.value eq 'Disk Write Operations/Sec' or  name.value eq 'Percentage CPU' or  name.value eq 'Network In' or  name.value eq 'Network Out' or  name.value eq 'Disk Read Operations/Sec' or  name.value eq 'Disk Read Bytes' or  name.value eq 'Disk Write Bytes')";

I also do a demo before, more detail please refer to another SO thread.

var azureTenantId = "tenant id";
var azureSecretKey = "secret key";
var azureAppId = "client id";
var subscriptionId = "subscription id";
var resourceGroup = "resource group";
var machineName = "machine name";
var serviceCreds = ApplicationTokenProvider.LoginSilentAsync(azureTenantId, azureAppId, azureSecretKey).Result;
MonitorClient monitorClient = new MonitorClient(serviceCreds) { SubscriptionId = subscriptionId };
 var resourceUrl = $"subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/virtualMachines/{machineName}";
 var metricNames = "(name.value eq 'Disk Write Operations/Sec' or  name.value eq 'Percentage CPU' or  name.value eq 'Network In' or  name.value eq 'Network Out' or  name.value eq 'Disk Read Operations/Sec' or  name.value eq 'Disk Read Bytes' or  name.value eq 'Disk Write Bytes')"; 
 string timeGrain = " and timeGrain eq duration'PT5M'";
 string startDate = " and startTime eq 2017-10-26T05:28:34.919Z";
 string endDate = " and endTime eq 2017-10-26T05:33:34.919Z";
 var odataFilterMetrics = new ODataQuery<MetricInner>(
                $"{metricNames}{timeGrain}{startDate}{endDate}");

 var metrics = monitorClient.Metrics.ListWithHttpMessagesAsync(resourceUrl, odataFilterMetrics).Result;

Upvotes: 2

Related Questions