使用Azure PowerShell为Azure日志分析和基于指标的条件创建多个警报吗?

时间:2018-08-31 04:14:19

标签: json azure azure-deployment azure-log-analytics

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

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

是否有更好/更优雅的方式来执行此操作(一个划线员命令行)?我发现没有必要创建另一对文件来满足我希望监视的不同VM的需要。

让我知道我是否可以在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"
        }
    }    
}

0 个答案:

没有答案