Search code examples
azurevirtual-machineterraform-provider-azureazure-diagnostics

Adding Diagnostic setting to Virtual Machine using Terraform extension


I am adding Diagnostic extension to windows virtual machine using terraform.

 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
} 

In Azure Portal under newly created virtual machine under extensions it shows extension added but when I open Diagnostic setting it keeps on loading window never opened to see counters.

Win-DiagnosticsConfiguration.json

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

Solution

  • You probably have passed the wrong counterSpecifier. You could edit the Win-DiagnosticsConfiguration.json to

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

    I tried it and worked on my side.

    enter image description here

    Here is a minimal example of a public settings file that enables the collection of diagnostic infrastructure logs, a single performance counter, and a single event log.

    {
      "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)]]"
              }
            ]
          }
        }
      }
    }