gpt4 book ai didi

c# - 在 C# 中获取 Azure 虚拟机的指标

转载 作者:行者123 更新时间:2023-12-03 05:50:55 25 4
gpt4 key购买 nike

我有下面的代码,用于检索 Azure 虚拟机的 CPU 百分比。我想要网络输入、网络输出、磁盘读取字节、磁盘写入字节、磁盘读取操作、磁盘写入操作的指​​标。我为 queryString 设置的内容似乎并不重要,我总是得到 cpu 百分比。如何获取其他指标?

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;
}

最佳答案

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

您可以从 Supported metrics with Azure Monitor 获取其他指标名称。如果您想获取多个指标,可以使用来附加指标。 CPU指标是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')";

我之前也做过一个demo,更详细的请引用另一个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# - 在 C# 中获取 Azure 虚拟机的指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48099715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com