首页 > 解决方案 > 是否可以在不接触机器的情况下将 Azure VM 指标流式传输到 eventhub?

问题描述

我目前正在尝试将 Azure Windows VM(如 cpu 等)的指标流式传输到 eventthub。
我在https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-streaming-azure-diags-data下找到了一个教程,但是在本教程中,有必要更改每个的配置系统。由于我们有很多系统,这将意味着很大的不便。我们目前需要的唯一指标是单击 Azure 中的 VM 时显示的指标(CPU 百分比、网络输入/输出、磁盘读取/写入字节、磁盘读取操作)。我们已经设法通过逻辑应用根据这些指标发送警报。

标签: azurestreamvirtual-machinemetricsazure-eventhub

解决方案


是否可以在不接触机器的情况下将 Azure VM 指标流式传输到 eventhub?

是的,我们可以使用Microsoft.Azure.Management.Monitor.Fluent 库t 获取指标。您可以参考这个SO 线程来获取演示代码。我们还可以从Supported metrics with Azure Monitor中获得更多其他受支持的指标名称。

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;

以及如何将消息发送到事件中心,请参阅本教程


推荐阅读