2

我似乎找不到任何示例,并且根据我在做什么,我遇到了不同的错误。

我正试图让这个工作,它只是没有发生......有什么想法吗?

resource "azurerm_monitor_metric_alert" "example" {
  name                = "example-metricalert"
  resource_group_name = azurerm_resource_group.example.name
  scopes              = [azurerm_mssql_database.test.server_id]
  description         = "Action will be triggered when cpu is greater than 80%."

  criteria {
    metric_namespace = "Microsoft.Sql/servers/databases"
    metric_name      = "CPU_percentage"
    aggregation      = "Average"
    operator         = "GreaterThan"
    threshold        = 80
}

}
4

1 回答 1

1

您可以使用以下代码为 SQL DB 创建指标警报。我已经针对现有的 SQL DB 对其进行了测试,因此使用了数据块。

主文件

   provider "azurerm" {
  features {}
}


data "azurerm_mssql_server" "example" {
  name                = "ztestansumanserver"
  resource_group_name = "yourresourcegroup"
}

data "azurerm_mssql_database" "dbtomonitor" {
  name      = "testansumandb"
  server_id = data.azurerm_mssql_server.example.id
}

resource "azurerm_monitor_action_group" "example" {
  name                = "CriticalAlertsAction"
  resource_group_name = data.azurerm_mssql_server.example.resource_group_name
  short_name          = "p0action"

  email_receiver {
    name                    = "sendtoadmin"
    email_address           = "youremailid"
    use_common_alert_schema = true
  }
}

resource "azurerm_monitor_metric_alert" "example" {
  name                = "example-metricalert"
  resource_group_name = data.azurerm_mssql_server.example.resource_group_name
  scopes              = [data.azurerm_mssql_database.dbtomonitor.id]
  description         = "Action will be triggered when cpu percent is greater than 80."

  criteria {
    metric_namespace = "Microsoft.Sql/servers/databases"
    metric_name      = "cpu_percent"
    aggregation      = "Average"
    operator         = "GreaterThan"
    threshold        = 80
  }
 action {
    action_group_id = azurerm_monitor_action_group.example.id
  }
}

输出:

在此处输入图像描述

注意:根据上面的脚本警报创建成功,当cpu_percent > 80.

参考:

Azure Monitor 按资源类型支持的指标 - Azure Monitor | 微软文档

于 2021-09-08T09:59:16.110 回答