Search code examples
azureterraformazure-virtual-machineterraform-provider-azureazure-diagnostics

How to setup azurerm_virtual_machine_extension for linux diagnostics


I have been struggling to automate the setup of my linux diagnostic settings with terraform. I am able to create the extension okay, however it does not seem to report metrics and if I try to edit it in the azure portal it fails to save. Hoping you guys can point out where I went wrong.

resource "azurerm_virtual_machine_extension" "vm_linux_diagnostics" {
    name                       = "LinuxDiagnostics"
    virtual_machine_id         = azurerm_virtual_machine.vm.id
    publisher                  = "Microsoft.Azure.Diagnostics"
    type                       = "LinuxDiagnostic"
    type_handler_version       = "3.0"
    auto_upgrade_minor_version = "true"

    protected_settings         = <<PROTECTED_SETTINGS
      {
          "storageAccountName": "${var.metrics_storage_account_name}",
          "storageAccountSasToken": "${var.metrics_storage_account_token}"
      }
    PROTECTED_SETTINGS

    settings = <<SETTINGS
      {
        "StorageAccount": "${var.metrics_storage_account_name}",
        "ladCfg": {
          "diagnosticMonitorConfiguration": {
            "eventVolume": "Medium",
            "metrics": {
              "metricAggregation": [
                {
                  "scheduledTransferPeriod": "PT1M"
                },
                {
                  "scheduledTransferPeriod": "PT1H"
                }
              ],
              "resourceId": "${azurerm_virtual_machine.vm.id}"
            },
            "performanceCounters": {
              "performanceCounterConfiguration": [
                {
                  "annotation": [
                    {
                      "displayName": "Filesystem % used space",
                      "locale": "en-us"
                    }
                  ],
                  "class": "filesystem",
                  "condition": "IsAggregate=TRUE",
                  "counter": "percentusedspace",
                  "counterSpecifier": "/builtin/filesystem/percentusedspace",
                  "sampleRate": "PT15S",
                  "type": "builtin",
                  "unit": "Percent"
                }
              ]
            },
            "syslogEvents": {
              "syslogEventConfiguration": {}
            }
          },
          "sampleRateInSeconds": 15
        }
      }
    SETTINGS
}

Solution

  • Answer was that the metric is no longer reported and I had to install telegraf and report disk usage metrics that way. Then you make an azurerm_monitor_metric_alert like the following:

    resource "azurerm_monitor_metric_alert" "diskUsage" {
      name                = "vm-disk-usage-alert"
      resource_group_name = var.resource_group
      scopes              = [azurerm_virtual_machine.vm.id]
      window_size         = "PT1H"
      frequency           = "PT30M"
    
      criteria {
        metric_namespace = "telegraf/disk"
        metric_name      = "used_percent"
        aggregation      = "Maximum"
        operator         = "GreaterThan"
        threshold        = 80
      }
    
      action {
        action_group_id = var.action_group_id
      }
    }