{"id":20901,"date":"2018-01-19T17:26:46","date_gmt":"2018-01-19T17:26:46","guid":{"rendered":"https:\/\/aidanfinn.com\/?p=20901"},"modified":"2018-01-20T09:47:11","modified_gmt":"2018-01-20T09:47:11","slug":"adding-azure-monitor-performance-alerts-using-powershell","status":"publish","type":"post","link":"https:\/\/aidanfinn.com\/?p=20901","title":{"rendered":"Adding Azure Monitor Performance Alerts Using PowerShell"},"content":{"rendered":"<p>Below is a sample script for adding Azure Metrics alerts using Azure Monitor. It is possible to create alerts using the Azure Portal, but that doesn\u2019t scale well because each alert is specific to one VM. For example, if you have 4 alerts per VM, and 10 VMs, then you have to create 40 alerts! One could say: Use Log Analytics, but there\u2019s a cost to that, and I find the OMS Workspace to be immature. Instead, one can continue to use Resource\/Azure Monitor metrics, but script the creation of the metrics alerts.<\/p>\n<p>Once could use JSON, but again, there\u2019s a scale-out issue there unless you build this into every deployment. But the advantage with PowerShell is that you can automatically vary thresholds based on the VM\u2019s spec, as you will see below \u2013 some metric thresholds vary depending on the spec of a machine, e.g. the number of cores.<\/p>\n<p>The magic cmdlet for doing this work is Add-AzureRmMetricAlertRule. And the key to making that cmdlet work is to know the name of the metric. Microsoft\u2019s docs state that you can query for available metrics using Get-AzureRmMetricDefinition, but I found that with VMs, it only returned back the Host metrics and not the Guest metrics. I had to do some experimenting, but I found that the names of the guest metrics are predictable; they\u2019re exactly what you see in the Azure Portal, e.g. \\System\\Processor Queue Length.<\/p>\n<p>The below script is made up of a start and 2 functions:<\/p>\n<ol>\n<li>The start is where I specify some variables to define the VM, resource group name, and query for the location of the VM. The start can then call a series of functions, one for each metric type. In this example, I call ProcessorQLength.<\/li>\n<li>The ProcessorQLength function takes the VM, queries for it\u2019s size, and then gets the number of cores assigned to that VM. We need that because the alert should be triggers if the average queue length per core is over 4, e.g. 12 for a 4 core VM. The AddMetric function is called with a configuration for the \\System\\Processor Queue Length alert.<\/li>\n<li>The AddMetric function is a generic function capable of creating any Azure metrics alert. It is configured by the parameters that are fed into it, in this case by the ProcessorQLength function.<\/li>\n<\/ol>\n<p>Here\u2019s my example:<\/p>\n<pre class=\"lang:default decode:true\">#A generic function to create an Azure Metrics alert\r\nfunction AddMetric ($FunMetricName, $FuncMetric, $FuncCondition, $FuncThreshold, $FuncWindowSize, $FuncTimeOperator, $FuncDescription)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0$VMID = (Get-AzureRmVM -ResourceGroupName $RGName -Name $VMName).Id\r\n\u00a0\u00a0\u00a0 Add-AzureRmMetricAlertRule -Name $FunMetricName -Location $VMLocation -ResourceGroup $RGName -TargetResourceId $VMID -MetricName $FuncMetric -Operator $FuncCondition -Threshold $FuncThreshold -WindowSize $FuncWindowSize -TimeAggregationOperator $FuncTimeOperator -Description $FuncDescription\r\n}\r\n\r\n#Create an alert for Processor Queue Length being 4x the number of cores in a VM\r\nfunction ProcessorQLength ()\r\n{\r\n\u00a0\u00a0\u00a0 $VMSize = (Get-AzureRMVM -ResourceGroupName $RGName -Name $VMName).HardwareProfile.VmSize\r\n\u00a0\u00a0\u00a0 $Cores = (Get-AzureRMVMSize -Location $VMLocation | Where-Object {$_.Name -eq $VMSize}).NumberOfCores\r\n\u00a0\u00a0\u00a0 $QThreshold = $Cores * 4\r\n\u00a0\u00a0\u00a0 AddMetric \"$VMname - CPU Q Length\" \"\\System\\Processor Queue Length\" \"GreaterThan\" $QThreshold \"00:05:00\" \"Average\" \"Created using PowerShell\"\r\n}\r\n\r\n#The script starts here\r\n#Specify a VM name\/resource group\r\n$VMName = \"vm-test-01\"\r\n$RGName = \"test\"\r\n$VMLocation = (Get-AzureRMVM -ResourceGroupName $RGName -Name $VMName).Location\r\n\r\n#Start running functions to create alerts\r\nProcessorQLength<\/pre>\n<h2>Was This Post Useful?<\/h2>\n<p>If you found this information useful, then imagine what 2 days of training might mean to you. I&#8217;m delivering a 2-day course in Amsterdam on April 19-20, teaching newbies and experienced Azure admins about Azure Infrastructure. There&#8217;ll be lots of in-depth information, covering the foundations, best practices, troubleshooting, and advanced configurations. You can learn more <a href=\"http:\/\/amsterdam.cloudmechanix.com\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p><a href=\"http:\/\/amsterdam.cloudmechanix.com\/\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-20919\" src=\"https:\/\/aidanfinn.com\/wp-content\/uploads\/2018\/01\/AMS-Apr-2018-Cloud-Mechanix.png\" alt=\"\" width=\"600\" height=\"318\" srcset=\"https:\/\/aidanfinn.com\/wp-content\/uploads\/2018\/01\/AMS-Apr-2018-Cloud-Mechanix.png 1352w, https:\/\/aidanfinn.com\/wp-content\/uploads\/2018\/01\/AMS-Apr-2018-Cloud-Mechanix-300x159.png 300w, https:\/\/aidanfinn.com\/wp-content\/uploads\/2018\/01\/AMS-Apr-2018-Cloud-Mechanix-768x407.png 768w, https:\/\/aidanfinn.com\/wp-content\/uploads\/2018\/01\/AMS-Apr-2018-Cloud-Mechanix-1024x543.png 1024w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is a sample script for adding Azure Metrics alerts using Azure Monitor. It is possible to create alerts using the Azure Portal, but that doesn\u2019t scale well because each alert is specific to one VM. For example, if you have 4 alerts per VM, and 10 VMs, then you have to create 40 alerts! &hellip; <a href=\"https:\/\/aidanfinn.com\/?p=20901\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Adding Azure Monitor Performance Alerts Using PowerShell&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":20903,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[5],"tags":[245,170,238,86,87,189,153],"class_list":["post-20901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","tag-alerts","tag-azure","tag-azure-monitor","tag-performance","tag-powershell","tag-scripting","tag-virtual-machines"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/aidanfinn.com\/wp-content\/uploads\/2018\/01\/AzureMonitor.png","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/posts\/20901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20901"}],"version-history":[{"count":5,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/posts\/20901\/revisions"}],"predecessor-version":[{"id":20922,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/posts\/20901\/revisions\/20922"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/media\/20903"}],"wp:attachment":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}