简体   繁体   中英

How to embed resource's URL into metric alert description within Azure monitor?

It's been a pain when tracking alerts when coming across multi regions and environments. Not only wasting time to find the right resources on the Azure portal but sometime took a while to filter/load all the resources on the portal. Is there a way to embed the resources/objects URL into the metric alert's description so one can just click it on the eg Opsgenie and lead to the problematic resources on the Azure portal?

Example:

Take this alert message for instance:

Name: [P2]-[NA]-[prod_v1_SDL_api-async_azure_australiaeast_Commercial]-[sdl-au-prod-async-cosmosdb Cosmosdb Availability is LessThan Threshold]
Description: The availability of Async cosmosdb is less than 100%.

I want to have a link inside the Description that can lead me to the right cosmosDB on the Azure portal.

I'm looking to make the resources/objects URL into a variable then use it inside Terraform monitor_metric_alert or something similar to that. But I didn't see anything similar to what I'm looking for on Terraform monitor_metric_alert .

I tried to reproduce the same in my environment.

I think what you are looking for is Data collection endpoints and rules in Azure Monitor.

Note: You can define a data collection rule to send data from multiple machines to multiple Log Analytics workspaces, including workspaces in a different region or tenant. Create the data collection rule in the same region as your Log Analytics workspace.

See the steps here

Code:

resource "azurerm_monitor_data_collection_endpoint" "example" {
  name                          = "example-mdce"
  resource_group_name           = azurerm_resource_group.example.name
  location                      = azurerm_resource_group.example.location
  kind                          = "Windows"
  public_network_access_enabled = true
  description                   = "monitor_data_collection_endpoint example"
  tags = {
    foo = "bar"
  }
}

snippet from azurerm_monitor_data_collection_rule |Resources | hashicorp/azurerm | Terraform Registry

Code:

resource "azurerm_storage_account" "to_monitor" {
  name                     = "kavyaexamplestorageaccount"
  resource_group_name      = data.azurerm_resource_group.example.name
  location                 = data.azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

#Create Log Analytics workspace with contributor rights:

resource "azurerm_log_analytics_workspace" "example" {
  name                = "example-workspace"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
}

resource "azurerm_log_analytics_solution" "example" {
  solution_name         = "WindowsEventForwarding"
  location              = data.azurerm_resource_group.example.location
  resource_group_name   = data.azurerm_resource_group.example.name
  workspace_resource_id = azurerm_log_analytics_workspace.example.id
  workspace_name        = azurerm_log_analytics_workspace.example.name
  plan {
    publisher = "Microsoft"
    product   = "OMSGallery/WindowsEventForwarding"
  }
}

resource "azurerm_monitor_data_collection_rule" "example" {
  name                = "kavyaexample-rule"
  resource_group_name = data.azurerm_resource_group.example.name
  location            = data.azurerm_resource_group.example.location

  destinations {
    log_analytics {
      workspace_resource_id = azurerm_log_analytics_workspace.example.id
      name                  = "test-destination-log"
    }

    azure_monitor_metrics {
      name = "test-destination-metrics"
    }
  }

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

  data_flow {
    streams      = ["Microsoft-InsightsMetrics", "Microsoft-Syslog", "Microsoft-Perf"]
    destinations = ["test-destination-log"]
  }

  data_sources {
    syslog {
      facility_names = ["*"]
      log_levels     = ["*"]
      name           = "test-datasource-syslog"
    }

    performance_counter {
      streams                       = ["Microsoft-Perf", "Microsoft-InsightsMetrics"]
      sampling_frequency_in_seconds = 10
      counter_specifiers            = ["Processor(*)\\% Processor Time"]
      name                          = "test-datasource-perfcounter"
    }

    windows_event_log {
      streams        = ["Microsoft-WindowsEvent"]
      x_path_queries = ["*[System/Level=1]"]
      name           = "test-datasource-wineventlog"
    }

    extension {
      streams            = ["Microsoft-WindowsEvent"]
      input_data_sources = ["test-datasource-wineventlog"]
      extension_name     = "test-extension-name"
      extension_json = jsonencode({
        a = 1
        b = "hello"
      })
      name = "test-datasource-extension"
    }
  }

  description = "data collection rule example"
  tags = {
    foo = "bar"
  }
  depends_on = [
    azurerm_log_analytics_solution.example
  ]
}


It worked in azurerm version 3.40.0
    azurerm = {
      source  = "hashicorp/azurerm"
       version = "=3.40.0"

    }
  • Make sure to have permissions to create Data Collection Rule.
  • Create data collection rules (DCRs) which represent the data about which Azure - - - Monitor Agent sends to which destinations
  • Associate the data collection rule to specific resources ex:virtual machines.

Terraform apply ran successfully

在此处输入图像描述

Portal view.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM