由于“ actionGroupId”

时间:2019-09-16 17:51:20

标签: azure azure-application-insights arm-template azure-cli azure-monitoring

我正在使用新的度量标准规则创建应用程序见解。 我以前使用的是经典警报,并且在2019年9月1日之前可以正常工作。我想采用新警报,因此我在下面修改了我的手臂模板:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "applicationinsights_platform": {
            "defaultValue": "",
            "type": "string",
            "maxLength": 6
          },
          "applicationinsights_location_shortname": {
            "defaultValue": "",
            "type": "string",
            "maxLength": 3
          },
          "applicationinsights_project": {
            "defaultValue": "",
            "type": "string",
            "maxLength": 6
          },
          "applicationinsights_environment": {
            "defaultValue": "",
            "type": "string",
            "maxLength": 7
          },
          "applicationinsights_uniqueid": {
            "defaultValue": "1",
            "type": "string",
            "maxLength": 1
          },
          "applicationinsights_componentKind": {
            "type": "string",
            "allowedValues": [
              "web",
              "ios",
              "other",
              "store",
              "java",
              "phone"
            ],
            "defaultValue": "web",
            "metadata": {
              "description": "application insights component kind"
            }
          },   
            "applicationinsights_alertDescription": {
            "type": "string",
            "defaultValue": "This is a metric alert",
            "metadata": {
                "description": "Description of alert"
            }
        },
        "applicationinsights_alertSeverity": {
            "type": "int",
            "defaultValue": 3,
            "allowedValues": [
                0,
                1,
                2,
                3,
                4
            ],
            "metadata": {
                "description": "Severity of alert {0,1,2,3,4}"
            }
        },
        "applicationinsights_isEnabled": {
            "type": "bool",
            "defaultValue": true,
            "metadata": {
                "description": "Specifies whether the alert is enabled"
            }
        },
            "applicationinsights_metricName": {
            "type": "string",
            "minLength": 1,
            "metadata": {
                "description": "Name of the metric used in the comparison to activate the alert."
            }
        },
        "applicationinsights_operator": {
            "type": "string",
            "defaultValue": "GreaterThan",
            "allowedValues": [
                "Equals",
                "NotEquals",
                "GreaterThan",
                "GreaterThanOrEqual",
                "LessThan",
                "LessThanOrEqual"
            ],
            "metadata": {
                "description": "Operator comparing the current value with the threshold value."
            }
        },
        "applicationinsights_threshold": {
            "type": "string",
            "defaultValue": "0",
            "metadata": {
                "description": "The threshold value at which the alert is activated."
            }
        },
        "applicationinsights_timeAggregation": {
            "type": "string",
            "defaultValue": "Average",
            "allowedValues": [
                "Average",
                "Minimum",
                "Maximum",
                "Total",
                "Count"
            ],
            "metadata": {
                "description": "How the data that is collected should be combined over time."
            }
        },
        "applicationinsights_windowSize": {
            "type": "string",
            "defaultValue": "PT5M",
            "allowedValues": [
                "PT1M",
                "PT5M",
                "PT15M",
                "PT30M",
                "PT1H",
                "PT6H",
                "PT12H",
                "PT24H"
            ],
            "metadata": {
                "description": "Period of time used to monitor alert activity based on the threshold. Must be between one minute and one day. ISO 8601 duration format."
            }
        },
        "evaluationFrequency": {
            "type": "string",
            "defaultValue": "PT1M",
            "allowedValues": [
                "PT1M",
                "PT5M",
                "PT15M",
                "PT30M",
                "PT1H"
            ],
            "metadata": {
                "description": "how often the metric alert is evaluated represented in ISO 8601 duration format"
            }
        },
         "applicationinsights_instrumentation_key_output": {
          "defaultValue": "ApplicationInsightsInstrumentationKey",
          "type": "string"
        },

        "applicationinsights_actionGroupId": {
            "type": "string",
            "defaultValue": "test",
            "metadata": {
                "description": "The ID of the action group that is triggered when the alert is activated or deactivated"
            }
        }
    },
    "variables": { 
      "basename": "[concat(parameters('applicationinsights_platform'), '-', parameters('applicationinsights_project'), '-', parameters('applicationinsights_location_shortname'), '-',  parameters('applicationinsights_environment'))]",
      "alertRulesName": "[concat(variables('basename'), '-aaiar-', parameters('applicationinsights_uniqueid'))]",
      "appInsightName": "[concat(variables('basename'), '-aaic-', parameters('applicationinsights_uniqueid'))]"
     },
    "resources": [
        {
            "name": "[variables('alertRulesName')]",
            "type": "Microsoft.Insights/metricAlerts",
            "location": "global",
            "apiVersion": "2018-03-01",
            "tags": {
              "[concat('hidden-link:', resourceId('Microsoft.Insights/components', variables('appInsightName')))]": "Resource"  
            },
            "properties": {
                "name": "[variables('alertRulesName')]",
                "description": "Alert rule assigned to Application Insight component",
                "severity": "[parameters('applicationinsights_alertSeverity')]",
                "enabled": "[parameters('applicationinsights_isEnabled')]",
                "scopes": ["[resourceId('microsoft.insights/components', variables('appInsightName'))]"],
                "evaluationFrequency":"[parameters('evaluationFrequency')]",
                "windowSize": "[parameters('applicationinsights_windowSize')]",
                "criteria": {
                    "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
                    "allOf": [
                        {
                            "name" : "test01",
                            "metricName": "[parameters('applicationinsights_metricName')]",
                            "dimensions":[],
                            "operator": "[parameters('applicationinsights_operator')]",
                            "threshold" : "[parameters('applicationinsights_threshold')]",
                            "timeAggregation": "[parameters('applicationinsights_timeAggregation')]"
                        }
                             ]
                           },
                "actions": [
                    {
                        "actionGroupId": "[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',variables('basename'), '/providers/Microsoft.Insights/actionGroups/',parameters('applicationinsights_platform'))]"
                    }
                           ]
                         },
            "dependsOn": [
          "[resourceId('microsoft.insights/components', variables('appInsightName'))]"
                         ]
       },
     {
      "name": "[variables('appInsightName')]",
      "type": "microsoft.insights/components",
      "apiVersion": "2015-05-01",
      "location": "[resourceGroup().location]",
      "tags": {},
      "kind": "[parameters('applicationinsights_componentKind')]",
      "properties": {
        "ApplicationId": "[variables('appInsightName')]"
      }}

    ],
    "outputs": {
        "secrets": {
          "type": "object",
          "value": {
            "[parameters('applicationinsights_instrumentation_key_output')]": "[reference(concat('microsoft.insights/components/', variables('appInsightName'))).InstrumentationKey]"
          }
        }
      }  

  }

我的参数文件是-

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "applicationinsights_platform": {
            "value": "abc"
        },
         "applicationinsights_location_shortname": {
            "value": "def"
        },
         "applicationinsights_project": {
            "value": "ghi"
          },
          "applicationinsights_environment": {
            "value": "ijk"
          },
          "applicationinsights_uniqueid": {
            "value": "1"
          },
          "applicationinsights_componentKind": {
            "value": "web"
          },
          "applicationinsights_metricName": {
            "value": "availabilityResults/availabilityPercentage"
          },
          "applicationinsights_operator": {
            "value": "GreaterThan"
          },
          "applicationinsights_threshold": {
            "value": "80"
          },
          "applicationinsights_windowSize": {
            "value": "PT5M"
          },
          "applicationinsights_timeAggregation": {
            "value": "Average"
          },
         "applicationinsights_alertDescription": {
            "value": "New metric alert created via template"
          },
        "applicationinsights_alertSeverity": {
            "value": 3
        },
        "applicationinsights_isEnabled": {
            "value": true
        }
    }
}

我遇到错误-

  

“代码”:“ BadRequest”,“消息”:“手臂资源   /subscriptions/xxxx/resourceGroups/abc-def-ghi-jkl/providers/Microsoft.Insights/actionGroups/abc   无效。”

我的资源组已经存在,并且我正在使用Az Cli(2.0.64版)及以下命令来创建资源-

az group deployment create --name 'test' --resource-group 'abc-def-ghi-jkl' --template-file azuredeploy.json --parameters azuredeploy.parameter.json --verbose --debug

但是,当我在模板中对零件进行硬编码时,资源已配置完毕

 "actions": [
                    {
                        "actionGroupId": "/subscriptions/xxxxxxxx/resourceGroups/abc-def-ghi-jkl/providers/Microsoft.Insights/actionGroups/test0"
                    }
                           ]

我没有任何线索,为什么我在对它进行参数化时会引发错误。 我从这里参考了ARM模板- https://docs.microsoft.com/en-us/azure/templates/microsoft.insights/2018-03-01/metricalerts

预先感谢您的帮助/建议。

2 个答案:

答案 0 :(得分:0)

好像您的ARM模板中有一个错字。 您写道:

"actions": [
                {
                    "actionGroupId": "[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',variables('basename'), '/providers/Microsoft.Insights/actionGroups/',parameters('applicationinsights_platform'))]"
                }

但是查看您的参数,您可能打算使用applicationinsights_actionGroupId代替applicationinsights_platform作为操作组名称。 像这样:

"actionGroupId": "[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',variables('basename'), '/providers/Microsoft.Insights/actionGroups/',parameters('applicationinsights_actionGroupId'))]"

作为建议,您还可以在ARM模板中创建操作组(see here for the documentation),然后使用resourceId参考设置操作组ID:

"actionGroupId": "[resourceId('Microsoft.Insights/actionGroups',parameters('applicationinsights_actionGroupId'))]"

希望这会有所帮助,

Asaf

答案 1 :(得分:0)

我后来了解到,首先需要添加操作组,然后才可以将其链接到其他资源。因此,我添加了另一个资源actiongroup,然后我就可以毫无问题地创建应用程序见解。是的,您唯一需要注意的是AG的短名称不能超过12个字符。

这是ARM模板-

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "applicationinsights_platform": {
      "defaultValue": "",
      "type": "string",
      "maxLength": 6
    },
    "applicationinsights_location_shortname": {
      "defaultValue": "",
      "type": "string",
      "maxLength": 3
    },
    "applicationinsights_project": {
      "defaultValue": "",
      "type": "string",
      "maxLength": 6
    },
    "applicationinsights_environment": {
      "defaultValue": "",
      "type": "string",
      "maxLength": 7
    },
    "applicationinsights_uniqueid": {
      "defaultValue": "1",
      "type": "string",
      "maxLength": 1
    },
    "applicationinsights_componentKind": {
      "type": "string",
      "allowedValues": [
        "web",
        "ios",
        "other",
        "store",
        "java",
        "phone"
      ],
      "defaultValue": "web",
      "metadata": {
        "description": "application insights component kind"
      }
    },
    "applicationinsights_alertSeverity": {
      "type": "int",
      "defaultValue": 3,
      "allowedValues": [
        0,
        1,
        2,
        3,
        4
      ],
      "metadata": {
        "description": "Severity of alert {0,1,2,3,4}"
      }
    },
    "applicationinsights_metricName": {
      "type": "string",
      "minLength": 1,
      "metadata": {
        "description": "Name of the metric used in the comparison to activate the alert."
      }
    },
    "applicationinsights_operator": {
      "type": "string",
      "defaultValue": "GreaterThan",
      "allowedValues": [
        "Equals",
        "NotEquals",
        "GreaterThan",
        "GreaterThanOrEqual",
        "LessThan",
        "LessThanOrEqual"
      ],
      "metadata": {
        "description": "Operator comparing the current value with the threshold value."
      }
    },
    "applicationinsights_threshold": {
      "type": "string",
      "defaultValue": "0",
      "metadata": {
        "description": "The threshold value at which the alert is activated."
      }
    },
    "applicationinsights_timeAggregation": {
      "type": "string",
      "defaultValue": "Average",
      "allowedValues": [
        "Average",
        "Minimum",
        "Maximum",
        "Total",
        "Count"
      ],
      "metadata": {
        "description": "How the data that is collected should be combined over time."
      }
    },
    "applicationinsights_windowSize": {
      "type": "string",
      "defaultValue": "PT5M",
      "allowedValues": [
        "PT1M",
        "PT5M",
        "PT15M",
        "PT30M",
        "PT1H",
        "PT6H",
        "PT12H",
        "PT24H"
      ],
      "metadata": {
        "description": "Period of time used to monitor alert activity based on the threshold. Must be between one minute and one day. ISO 8601 duration format."
      }
    },
    "evaluationFrequency": {
      "type": "string",
      "defaultValue": "PT1M",
      "allowedValues": [
        "PT1M",
        "PT5M",
        "PT15M",
        "PT30M",
        "PT1H"
      ],
      "metadata": {
        "description": "how often the metric alert is evaluated represented in ISO 8601 duration format"
      }
    },
    "applicationinsights_instrumentation_key_output": {
      "defaultValue": "ApplicationInsightsInstrumentationKey",
      "type": "string"
    },
    "applicationinsights_actionGroupId": {
      "type": "string",
      "defaultValue": "test",
      "metadata": {
        "description": "The ID of the action group that is triggered when the alert is activated or deactivated"
      }
    },
    "applicationinsights_recieverEmailId": {
      "type": "string",
      "defaultValue": "atf@stackoverflow.com",
      "metadata": {
        "description": "Email Address of Receiver"
      }
    },
    "applicationinsights_webhookURI": {
      "type": "string",
      "defaultValue": "http://requestb.in/1bq62iu2",
      "metadata": {
        "description": "Webhook receiver service URI"
      }
    }
  },
  "variables": {
    "basename": "[concat(parameters('applicationinsights_platform'), '-', parameters('applicationinsights_project'), '-', parameters('applicationinsights_location_shortname'), '-',  parameters('applicationinsights_environment'))]",
    "alertRulesName": "[concat(variables('basename'), '-aiar-', parameters('applicationinsights_uniqueid'))]",
    "appInsightName": "[concat(variables('basename'), '-aic-', parameters('applicationinsights_uniqueid'))]",
    "AGName": "[concat(parameters('applicationinsights_platform'), parameters('applicationinsights_project'), parameters('applicationinsights_location_shortname'), parameters('applicationinsights_environment'))]",
    "AGId": "[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',variables('basename'),'/providers/Microsoft.Insights/actionGroups/',variables('AGName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Insights/actionGroups",
      "apiVersion": "2018-03-01",
      "name": "[variables('AGName')]",
      "location": "Global",
      "properties": {
        "groupShortName": "[variables('AGName')]",
        "enabled": true,
        "emailReceivers": [
          {
            "name": "EmailOfRecipient",
            "emailAddress": "[parameters('applicationinsights_recieverEmailId')]"
          }
        ],
        "webhookReceivers": [
          {
            "name": "WebhookURI",
            "serviceUri": "[parameters('applicationinsights_webhookURI')]"
          }
        ]
      }
    },
    {
      "name": "[variables('alertRulesName')]",
      "type": "Microsoft.Insights/metricAlerts",
      "location": "global",
      "apiVersion": "2018-03-01",
      "tags": {
        "[concat('hidden-link:', resourceId('Microsoft.Insights/components', variables('appInsightName')))]": "Resource"
      },
      "properties": {
        "name": "[variables('alertRulesName')]",
        "description": "Alert rule assigned to Application Insight component",
        "severity": "[parameters('applicationinsights_alertSeverity')]",
        "enabled": true,
        "scopes": [
          "[resourceId('microsoft.insights/components', variables('appInsightName'))]"
        ],
        "evaluationFrequency": "[parameters('evaluationFrequency')]",
        "windowSize": "[parameters('applicationinsights_windowSize')]",
        "criteria": {
          "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
          "allOf": [
            {
              "name": "[variables('AGName')]",
              "metricName": "[parameters('applicationinsights_metricName')]",
              "dimensions": [],
              "operator": "[parameters('applicationinsights_operator')]",
              "threshold": "[parameters('applicationinsights_threshold')]",
              "timeAggregation": "[parameters('applicationinsights_timeAggregation')]"
            }
          ]
        },
        "actions": [
          {
            "actionGroupId": "[variables('AGId')]"
          }
        ]
      },
      "dependsOn": [
        "[resourceId('microsoft.insights/components', variables('appInsightName'))]",
        "[resourceId('microsoft.insights/actionGroups', variables('AGName'))]"
      ]
    },
    {
      "name": "[variables('appInsightName')]",
      "type": "microsoft.insights/components",
      "apiVersion": "2015-05-01",
      "location": "[resourceGroup().location]",
      "tags": {},
      "kind": "[parameters('applicationinsights_componentKind')]",
      "properties": {
        "ApplicationId": "[variables('appInsightName')]"
      }
    }
  ],
  "outputs": {
    "secrets": {
      "type": "object",
      "value": {
        "[parameters('applicationinsights_instrumentation_key_output')]": "[reference(concat('microsoft.insights/components/', variables('appInsightName'))).InstrumentationKey]"
      }
    }
  }
}

和我的参数文件-

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "applicationinsights_platform": {
      "value": "foo"
    },
    "applicationinsights_location_shortname": {
      "value": "eus"
    },
    "applicationinsights_project": {
      "value": "bar"
    },
    "applicationinsights_environment": {
      "value": "dev"
    },
    "applicationinsights_uniqueid": {
      "value": "1"
    },
    "applicationinsights_componentKind": {
      "value": "web"
    },
    "applicationinsights_metricName": {
      "value": "requests/duration"
    },
    "applicationinsights_operator": {
      "value": "GreaterThan"
    },
    "applicationinsights_threshold": {
      "value": "10000"
    },
    "applicationinsights_windowSize": {
      "value": "PT5M"
    },
    "applicationinsights_timeAggregation": {
      "value": "Average"
    },
    "applicationinsights_alertSeverity": {
      "value": 3
    },
    "applicationinsights_recieverEmailId": {
      "value": "atf@stackoverfow.com"
    },
    "applicationinsights_webhookURI": {
      "value": "https://requestb.in/1bq62iu2"
    }
  }
}