2

假设我为 Azure 准备了一个通用模板文件来创建基于度量的警报,并为 2 个不同的度量警报(例如 CPU 利用率、内存使用)提供了 2 个单独的参数文件。

请参考我下面的模板文件示例,以及我打算运行的命令以通过 Powershell 创建这两种类型的警报。

有没有更好/更优雅的方式来做这件事(一个线性命令行)?我发现没有必要创建另一对文件来满足我希望监控的不同虚拟机的需求。

让我知道是否有任何文档可以在 Azure 网站上参考。

Azure CLI 命令:

az login

az group deployment create --name AlertTemplateDeployment --resource-group TestRscGrp --output tsv --template-file common_metricalert.template.json --parameters cpu_metricalert.parameters.json

az group deployment create --name AlertTemplateDeployment --resource-group TestRscGrp --output tsv --template-file common_metricalert.template.json --parameters memory_metricalert.parameters.json

common.template.json

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "alertName": {
            "type": "string",
            "metadata": {
                "description": "Name of the alert"
            }
        },
        "alertDescription": {
            "type": "string",
            "defaultValue": "This is a metric alert",
            "metadata": {
                "description": "Description of alert"
            }
        },
        "alertSeverity": {
            "type": "int",
            "defaultValue": 3,
            "allowedValues": [
                0,
                1,
                2,
                3,
                4
            ],
            "metadata": {
                "description": "Severity of alert {0,1,2,3,4}"
            }
        },
        "isEnabled": {
            "type": "bool",
            "defaultValue": true,
            "metadata": {
                "description": "Specifies whether the alert is enabled"
            }
        },
        "resourceId": {
            "type": "string",
            "defaultValue": "",
            "metadata": {
                "description": "Resource ID of the resource emitting the metric that will be used for the comparison."
            }
        },
        "criterion1":{
            "type": "object",
            "metadata": {
                "description": "Criterion includes metric name, dimension values, threshold and an operator. The alert rule fires when ALL criteria are met"
            }
        },
        "windowSize": {
            "type": "string",
            "defaultValue": "PT5M",
            "metadata": {
                "description": "Period of time used to monitor alert activity based on the threshold. Must be between five minutes and one day. ISO 8601 duration format."
            }
        },
        "evaluationFrequency": {
            "type": "string",
            "defaultValue": "PT1M",
            "metadata": {
                "description": "how often the metric alert is evaluated represented in ISO 8601 duration format"
            }
        },
        "actionGroupId": {
            "type": "string",
            "defaultValue": "",
            "metadata": {
                "description": "The ID of the action group that is triggered when the alert is activated or deactivated"
            }
        }
    },
    "variables": { 
        "criterion1": "[array(parameters('criterion1'))]",
        "criteria": "[concat(variables('criterion1'))]"
     },
    "resources": [
        {
            "name": "[parameters('alertName')]",
            "type": "microsoft.insights/metricAlerts",
            "location": "global",
            "apiVersion": "2018-03-01",
            "tags": {},
            "properties": {
                "description": "[parameters('alertDescription')]",
                "severity": "[parameters('alertSeverity')]",
                "enabled": "[parameters('isEnabled')]",
                "scopes": ["[parameters('resourceId')]"],
                "evaluationFrequency":"[parameters('evaluationFrequency')]",
                "windowSize": "[parameters('windowSize')]",
                "criteria": {
                    "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
                    "allOf": "[variables('criteria')]"
                },
                "actions": [
                    {
                        "actionGroupId": "[parameters('actionGroupId')]"                
                    }
                ]
            }
        }
    ]
}

cpu_metricalert.parameters.json:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "alertName": {
            "value": "High CPU usage Alert (Computer-Your-PC-Name)"
        },
        "alertDescription": {
            "value": "High CPU usage Alert (Computer-Your-PC-Name) created via template"
        },
        "alertSeverity": {
            "value":1
        },
        "isEnabled": {
            "value": true
        },
        "resourceId": {
            "value": "/subscriptions/your-subscription-id-here/resourceGroups/TestRscGrp/providers/Microsoft.OperationalInsights/workspaces/TestingLAWorkspace"
        },
        "windowSize": {
            "value": "PT1M"
        },
        "criterion1": {
            "value": {
                    "name": "Average_% Processor Time exceeds 50 Percent",
                    "metricName": "Average_% Processor Time",
                    "dimensions": [
                        {
                            "name":"Computer",
                            "operator": "Includes",
                            "values": ["Your-PC-Name"]
                        },
                        {
                            "name":"ObjectName",
                            "operator": "Includes",
                            "values": ["Processor"]
                        }
                    ],
                    "operator": "GreaterThan",
                    "threshold": "50",
                    "timeAggregation": "Average"
                }
        },          
        "actionGroupId": {
            "value": "/subscriptions/your-subscription-id-here/resourceGroups/default-activitylogalerts/providers/Microsoft.Insights/actionGroups/testalertag"
        }
    }    
}

memory_metricalert.parameters.json :

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "alertName": {
            "value": "High Memory usage Alert (Computer-Your-PC-Name)"
        },
        "alertDescription": {
            "value": "High Memory usage Alert (Computer-Your-PC-Name) created via template"
        },
        "alertSeverity": {
            "value":1
        },
        "isEnabled": {
            "value": true
        },
        "resourceId": {
            "value": "/subscriptions/your-subscription-id-here/resourceGroups/TestRscGrp/providers/Microsoft.OperationalInsights/workspaces/TestingLAWorkspace"
        },
        "windowSize": {
            "value": "PT1M"
        },
        "criterion1": {
            "value": {
                    "name": "Average_% Committed Bytes In Use exceeds 50 Percent",
                    "metricName": "Average_% Committed Bytes In Use",
                    "dimensions": [
                        {
                            "name":"Computer",
                            "operator": "Includes",
                            "values": ["Your-PC-Name"]
                        },
                        {
                            "name":"ObjectName",
                            "operator": "Includes",
                            "values": ["Memory"]
                        }
                    ],
                    "operator": "GreaterThan",
                    "threshold": "50",
                    "timeAggregation": "Average"
                }
        },          
        "actionGroupId": {
            "value": "/subscriptions/your-subscription-id-here/resourceGroups/default-activitylogalerts/providers/Microsoft.Insights/actionGroups/testalertag"
        }
    }    
}
4

0 回答 0