azure - 使用 terraform 将 VM 添加到新的 Azure 监控(无需 OMS 代理)

标签 azure azure-virtual-machine azure-monitoring

当我使用 OMS 解决方案为虚拟机配置 Azure 监控时,答案为 Enable Azure Monitor for existing Virtual machines using terraform ,我注意到此功能已被弃用,Azure 希望您迁移到新的监视解决方案(不使用日志分析代理)。

Azure 允许我使用此 GUI 配置虚拟机监控,但我想使用 terraform 来完成此操作。

enter image description here

我必须在 terraform 中使用特定的设置才能实现此目的吗? (顺便说一句,我使用的是 Linux VM)

最佳答案

是的,这是正确的。 omsagent 已被标记为旧版,Azure 现在有一个名为“Azure Monitor 代理”的新监视代理。下面给出的解决方案适用于 Linux,请查看 Windows 机器的官方 Terraform 文档。

我们需要三件事才能在 Terraform 中完成同等的 UI 对应操作。

  • azurerm_log_analytics_workspace
  • azurerm_monitor_data_collection_rule
  • azurerm_monitor_data_collection_rule_association

下面是示例代码:

    data "azurerm_linux_virtual_machine" "vm" {
      name                = var.vm_name
      resource_group_name = var.az_resource_group_name
    }
    
    resource "azurerm_log_analytics_workspace" "workspace" {
      name                = "${var.project}-${var.env}-log-analytics"
      location            = var.az_location
      resource_group_name = var.az_resource_group_name
      sku                 = "PerGB2018"
      retention_in_days   = 30
    }
    
     resource "azurerm_virtual_machine_extension" "AzureMonitorLinuxAgent" {        
      name                       = "AzureMonitorLinuxAgent"
      publisher                  = "Microsoft.Azure.Monitor"
      type                       = "AzureMonitorLinuxAgent"
      type_handler_version       = "1.0"
      auto_upgrade_minor_version = "true"
    
      virtual_machine_id = data.azurerm_linux_virtual_machine.vm.id
    }
    
    
     resource "azurerm_monitor_data_collection_rule" "example" {
      name                = "example-rules"
      resource_group_name = var.az_resource_group_name
      location            = var.az_location
    
      destinations {
        log_analytics {
          workspace_resource_id = azurerm_log_analytics_workspace.workspace.id
          name                  = "test-destination-log"
        }
    
        azure_monitor_metrics {
          name = "test-destination-metrics"
        }
      } 
    
      data_flow {
        streams      = ["Microsoft-InsightsMetrics"]
        destinations = ["test-destination-log"]
      }
    
      data_sources {
    
        performance_counter {
          streams                       = ["Microsoft-InsightsMetrics"]
          sampling_frequency_in_seconds = 60
          counter_specifiers            = ["\\VmInsights\\DetailedMetrics"]
          name                          = "VMInsightsPerfCounters"
        }
    }
     }
    
    # associate to a Data Collection Rule
    resource "azurerm_monitor_data_collection_rule_association" "example1" {
      name                    = "example1-dcra"
      target_resource_id      = data.azurerm_linux_virtual_machine.vm.id
      data_collection_rule_id = azurerm_monitor_data_collection_rule.example.id
      description             = "example"
    }

引用:

monitor_data_collection_rule

monitor_data_collection_rule_association

关于azure - 使用 terraform 将 VM 添加到新的 Azure 监控(无需 OMS 代理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73857873/

相关文章:

node.js - 从 Nodejs 服务器接收来自 ServiceBus 的 BrokeredMessage

sql-server - Polybase 不创建外部文件格式

azure - Azure 的 CPU 总时间和 CPU 平均时间有什么区别

azure - 如何在 Application Insights 中获取 VMSS 的实例计数?

python - pymssql 在 Azure/Windows 上返回的字符集与在 Mac 上不同

Php exec Powershell 脚本 Azure AZ 命令不运行,所有其他功能都运行

azure - 在 Windows Azure 上,VM 角色和 vm 实例之间有区别吗?

虚拟机的 Azure 多个公共(public) IP

linux - 如果 Linux ssh 守护进程在 Azure 上停止,则发出警报

azure - Azure 容器实例的可缓存 Docker 镜像