在C#中获取Azure VM的指标 [英] Get metrics of an Azure vm in c#
问题描述
我有下面的代码来检索Azure虚拟机的CPU百分比. 我想要有关网络输入",网络输出",磁盘读取字节",磁盘写入字节",磁盘读取操作",磁盘写入操作"的指标.我为queryString输入什么似乎无关紧要,我总是得到cpu百分比.如何获得其他指标?
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;
}
推荐答案
我为queryString输入什么似乎无关紧要,我总是得到cpu百分比.如何获得其他指标?
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;
这篇关于在C#中获取Azure VM的指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!