{"id":655,"date":"2023-08-05T19:34:00","date_gmt":"2023-08-05T19:34:00","guid":{"rendered":"http:\/\/draith.com\/?p=655"},"modified":"2024-02-13T03:11:43","modified_gmt":"2024-02-13T03:11:43","slug":"creating-alert-rules-in-azure-with-az-powershell-some-samples","status":"publish","type":"post","link":"http:\/\/draith.com\/?p=655","title":{"rendered":"Creating Alert Rules in Azure with AZ PowerShell &#8211; Some Samples"},"content":{"rendered":"\n<p>Let go over a simple one &#8211; how to create various types of alert rules in Azure using the AZ PowerShell Module.<\/p>\n\n\n\n<p>Each example targets a different aspect of Azure monitoring, but doesn&#8217;t cover them all. Remember to tweak the parameters to match your environment.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Metric Alerts for Performance Monitoring<\/strong><\/h4>\n\n\n\n<p>To keep an eye on Azure service metrics:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$criteria = New-AzMetricAlertRuleV2Criteria -MetricName 'Percentage CPU' -TimeAggregation Average -Operator GreaterThan -Threshold 80\n\nAdd-AzMetricAlertRuleV2 -Name 'HighCPUAlert' -ResourceGroupName 'YourResourceGroupName' -WindowSize 00:05:00 -Frequency 00:01:00 -TargetResourceId '\/subscriptions\/yourSubscriptionId\/resourceGroups\/yourResourceGroupName\/providers\/Microsoft.Compute\/virtualMachines\/yourVMName' -Condition $criteria -ActionGroup '\/subscriptions\/yourSubscriptionId\/resourceGroups\/yourResourceGroupName\/providers\/microsoft.insights\/actionGroups\/yourActionGroupName' -Severity 3 -Description 'Alert on high CPU usage.'\n<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Log Alerts for Custom Log Queries<\/strong><\/h4>\n\n\n\n<p>For alerts based on log analytics:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$query = &quot;AzureActivity | where OperationName == 'Create or Update Virtual Machine' and ActivityStatus == 'Succeeded'&quot;\n\nSet-AzScheduledQueryRule -ResourceGroupName 'YourResourceGroupName' -Location 'East US' -ActionGroup '\/subscriptions\/yourSubscriptionId\/resourceGroups\/yourResourceGroupName\/providers\/microsoft.insights\/actionGroups\/yourActionGroupName' -ConditionQuery $query -Description &quot;VM creation alert&quot; -Enabled $true -EvaluationFrequency 'PT5M' -Severity 0 -WindowSize 'PT5M' -Name 'VMCreationAlert'\n<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Activity Log Alerts for Azure Resource Events<\/strong><\/h4>\n\n\n\n<p>To monitor specific Azure service events:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$condition = New-AzActivityLogAlertCondition -Field 'category' -Equal 'Administrative'\n$actionGroupId = &quot;\/subscriptions\/yourSubscriptionId\/resourceGroups\/yourResourceGroupName\/providers\/microsoft.insights\/actionGroups\/yourActionGroupName&quot;\n\nSet-AzActivityLogAlert -Location 'Global' -Name 'AdminActivityAlert' -ResourceGroupName 'YourResourceGroupName' -Scopes &quot;\/subscriptions\/yourSubscriptionId&quot; -Condition $condition -ActionGroupId $actionGroupId -Description &quot;Alert on administrative activities&quot;\n<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Application Insights Alerts for Application Performance<\/strong><\/h4>\n\n\n\n<p>Track application performance with a simple AppInsights web test<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$rule = New-AzApplicationInsightsWebTestAlertRule -Name 'AppPerfAlert' -ResourceGroupName 'YourResourceGroupName' -Location 'East US' -WebTestId '\/subscriptions\/yourSubscriptionId\/resourceGroups\/yourResourceGroupName\/providers\/microsoft.insights\/webtests\/yourWebTestId' -FailedLocationCount 3 -WindowSize 'PT5M' -Frequency 'PT1M' -Criteria $criteria\n\nSet-AzApplicationInsightsWebTestAlertRule -InputObject $rule\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Let go over a simple one &#8211; how to create various types of alert rules in Azure using the AZ PowerShell Module. Each example targets a different aspect of Azure monitoring, but doesn&#8217;t cover them all. Remember to tweak the parameters to match your environment. Metric Alerts for Performance Monitoring To keep an eye on &hellip; <a href=\"http:\/\/draith.com\/?p=655\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Creating Alert Rules in Azure with AZ PowerShell &#8211; Some Samples&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4,27,16],"class_list":["post-655","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-azure","tag-azure-monitor","tag-powershell"],"_links":{"self":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=655"}],"version-history":[{"count":1,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/655\/revisions"}],"predecessor-version":[{"id":656,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/655\/revisions\/656"}],"wp:attachment":[{"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=655"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}