使用 Terraform 扩展将诊断设置添加到虚拟机

Adding Diagnostic setting to Virtual Machine using Terraform extension

我正在使用 terraform 向 windows 虚拟机添加诊断扩展。

 resource "azurerm_virtual_machine_extension" "VMDiagnosticsSettings" {
  name                 = "${azurerm_virtual_machine.vm.name}-DiagnosticSettings"
  virtual_machine_id         = "${azurerm_virtual_machine.vm.id}"
  publisher                  = "Microsoft.Azure.Diagnostics"
  type                       = "IaaSDiagnostics"
  type_handler_version       = "1.9"
  auto_upgrade_minor_version = "true"
  settings = <<SETTINGS
    {
      "StorageAccount": "${data.azurerm_key_vault_secret.storage_acc.value}",
      "WadCfg": {
          "diagnosticMonitorConfiguration": {
                "eventVolume": "Medium", 
                "metrics": {
                     "metricAggregation": [
                        {
                            "scheduledTransferPeriod": "PT1H"
                        }, 
                        {
                            "scheduledTransferPeriod": "PT1M"
                        }
                    ], 
                    "resourceId": "${azurerm_virtual_machine.vm.id}"
                },
                "PerformanceCounters": ${file("${path.module}/Win-DiagnosticsConfiguration.json")}, 
            "sampleRateInSeconds": 15
        }
    }
}
  SETTINGS
  protected_settings = <<PROTECTED_SETTINGS
    {
      "storageAccountName": "${data.azurerm_key_vault_secret.diagnostic_acc_name.value}",
      "storageAccountKey": "${data.azurerm_key_vault_secret.diagnostic_acc_key.value}"
    }
  PROTECTED_SETTINGS
} 

在扩展下新创建的虚拟机下的 Azure 门户中,它显示已添加扩展,但当我打开诊断设置时,它一直在加载 window 从未打开以查看计数器。

赢-DiagnosticsConfiguration.json

{
    "scheduledTransferPeriod": "PT1M",
    "PerformanceCounterConfiguration": [
        {
            "counterSpecifier": "\Processor(_Total)\% Processor Time",
            "sampleRate": "PT1M",
            "unit": "percent"
        }
    ]
}

你可能传错了counterSpecifier。您可以将 Win-DiagnosticsConfiguration.json 编辑为

{
    "scheduledTransferPeriod": "PT1M",
    "PerformanceCounterConfiguration": [
      {
        "counterSpecifier": "\Processor Information(_Total)\% Processor Time",
        "unit": "Percent",
        "sampleRate": "PT60S"
      }
    ]
  }

我试过了,在我这边工作。

Here 是 public 设置文件的最小示例,它支持收集诊断基础结构日志、单个性能计数器和单个事件日志。

{
  "StorageAccount": "mystorageaccount",
  "WadCfg": {
    "DiagnosticMonitorConfiguration": {
      "overallQuotaInMB": 5120,
      "PerformanceCounters": {
        "scheduledTransferPeriod": "PT1M",
        "PerformanceCounterConfiguration": [
          {
            "counterSpecifier": "\Processor Information(_Total)\% Processor Time",
            "unit": "Percent",
            "sampleRate": "PT60S"
          }
        ]
      },
      "WindowsEventLog": {
        "scheduledTransferPeriod": "PT1M",
        "DataSource": [
          {
            "name": "Application!*[System[(Level=1 or Level=2 or Level=3)]]"
          }
        ]
      }
    }
  }
}