gpt4 book ai didi

azure - Terraform:如何使用 Azure Monitor Agent 为 Azure Windows VM 启用 VM Insights?

转载 作者:行者123 更新时间:2023-12-03 02:00:41 25 4
gpt4 key购买 nike

我想使用 Azure Monitor Agent (AMA) 在我的 Azure Windows VM 上启用 Insights,但目前没有很多用于此目的的示例代码,大多数示例使用已弃用的 Microsoft Monitoring Agent (MMA)。

通过在 Azure 门户中配置 Insights,我发现 Azure 需要:

  • 数据收集规则
  • 目标 Log Analytics 工作区
  • 将数据收集规则与虚拟机关联
  • 安装 DependencyAgentWindows VM 扩展
  • 安装 AzureMonitorWindowsAgent VM 扩展

当我在 Terraform 中复制此内容时,我在 Log Analytics 工作区中看到条目,但 VM Insights 界面中没有显示任何内容。 Azure Monitor 识别出 VM 通过 AMA 连接。

是否有人有使用 AMA 而非 MMA 的 VM Insights 工作示例代码?

清理后的 Terraform 代码:​​

resource "azurerm_virtual_machine_extension" "ama" {
for_each = var.windows_vms

name = "AzureMonitorWindowsAgent"
auto_upgrade_minor_version = true
automatic_upgrade_enabled = true
publisher = "Microsoft.Azure.Monitor"
type = "AzureMonitorWindowsAgent"
type_handler_version = "1.14"
virtual_machine_id = azurerm_windows_virtual_machine.this[each.key].id
}

resource "azurerm_virtual_machine_extension" "da" {
for_each = var.windows_vms

name = "DependencyAgentWindows"
auto_upgrade_minor_version = true
automatic_upgrade_enabled = true
publisher = "Microsoft.Azure.Monitoring.DependencyAgent"
type = "DependencyAgentWindows"
type_handler_version = "9.10"
virtual_machine_id = azurerm_windows_virtual_machine.this[each.key].id
}

resource "azurerm_monitor_data_collection_rule" "vminsights" {
for_each = var.windows_vms

name = each.value.vminsights_data_collection.rule_name
resource_group_name = each.value.vminsights_data_collection.rule_rg_name
location = var.region

data_flow {
destinations = [ "log-analytics" ]
streams = [
"Microsoft-Event",
"Microsoft-InsightsMetrics",
"Microsoft-Perf",
"Microsoft-ServiceMap"
]
}

data_flow {
destinations = [ "monitor-metrics" ]
streams = [ "Microsoft-InsightsMetrics" ]
}

data_sources {
extension {
extension_name = "DependencyAgent"
name = "DependencyAgentDataSource"
streams = [ "Microsoft-ServiceMap" ]
}

performance_counter {
counter_specifiers = [ "\\VmInsights\\DetailedMetrics" ]
name = "insights-metrics"
sampling_frequency_in_seconds = 60
streams = [
"Microsoft-InsightsMetrics",
"Microsoft-Perf"
]
}

windows_event_log {
name = "windows-events"
streams = [ "Microsoft-Event" ]
x_path_queries = [
"Application!*[System[(Level=1 or Level=2 or Level=3)]]",
"System!*[System[(Level=1 or Level=2 or Level=3)]]"
]
}
}

destinations {
azure_monitor_metrics {
name = "monitor-metrics"
}

log_analytics {
name = "log-analytics"
workspace_resource_id = each.value.vminsights_data_collection.log_analytics_workspace_id
}
}
}

resource "azurerm_monitor_data_collection_rule_association" "vminsights" {
for_each = var.windows_vms

name = each.value.vminsights_data_collection.rule_association_name
data_collection_rule_id = azurerm_monitor_data_collection_rule.vminsights[each.key].id
description = "Monitor data collection rule for ${each.value.name}"
target_resource_id = azurerm_windows_virtual_machine.this[each.key].id
}

最佳答案

见解指标和见解 map 都正在进行以下更改:

  1. 向 DependencyAgentWindows 扩展添加设置 block :
resource "azurerm_virtual_machine_extension" "da" {
for_each = var.windows_vms

name = "DependencyAgentWindows"
auto_upgrade_minor_version = true
automatic_upgrade_enabled = true
publisher = "Microsoft.Azure.Monitoring.DependencyAgent"
settings = jsonencode(
{
"enableAMA" = "true"
}
)
type = "DependencyAgentWindows"
type_handler_version = "9.10"
virtual_machine_id = azurerm_windows_virtual_machine.this[each.key].id
}
  • 删除了 DCR 目标 azure_monitor_metrics,如下所示:
  • resource "azurerm_monitor_data_collection_rule" "vminsights" {
    for_each = var.windows_vms

    name = each.value.vminsights_data_collection.rule_name
    resource_group_name = each.value.vminsights_data_collection.rule_rg_name
    location = var.region

    data_flow {
    destinations = [ "log-analytics" ]
    streams = [ "Microsoft-Event" ]
    }

    data_flow {
    destinations = [ "log-analytics" ]
    streams = [ "Microsoft-InsightsMetrics" ]
    }

    data_flow {
    destinations = [ "log-analytics" ]
    streams = [ "Microsoft-ServiceMap" ]
    }

    # data_flow {
    # destinations = [ "monitor-metrics" ]
    # streams = [ "Microsoft-InsightsMetrics" ]
    # }

    data_sources {
    extension {
    extension_name = "DependencyAgent"
    name = "DependencyAgentDataSource"
    streams = [ "Microsoft-ServiceMap" ]
    }

    performance_counter {
    counter_specifiers = [ "\\VmInsights\\DetailedMetrics" ]
    name = "insights-metrics"
    sampling_frequency_in_seconds = 60
    streams = [
    "Microsoft-InsightsMetrics"
    ]
    }

    windows_event_log {
    name = "windows-events"
    streams = [ "Microsoft-Event" ]
    x_path_queries = [
    "Application!*[System[(Level=1 or Level=2 or Level=3)]]",
    "System!*[System[(Level=1 or Level=2 or Level=3)]]"
    ]
    }
    }

    destinations {
    # azure_monitor_metrics {
    # name = "monitor-metrics"
    # }

    log_analytics {
    name = "log-analytics"
    workspace_resource_id = each.value.vminsights_data_collection.log_analytics_workspace_id
    }
    }
    }

    关于azure - Terraform:如何使用 Azure Monitor Agent 为 Azure Windows VM 启用 VM Insights?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76161479/

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