{"id":8350,"date":"2024-10-09T03:23:12","date_gmt":"2024-10-09T03:23:12","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=8350"},"modified":"2024-10-09T03:23:14","modified_gmt":"2024-10-09T03:23:14","slug":"how-to-use-azure-monitor-to-alert-for-when-an-azure-vm-has-been-left-on-for-x-days","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/how-to-use-azure-monitor-to-alert-for-when-an-azure-vm-has-been-left-on-for-x-days","title":{"rendered":"How to Use Azure Monitor to alert for when an Azure VM has been left on for X Days"},"content":{"rendered":"\n<p>Create a new Resource group and then new Log Analytics Workspace <\/p>\n\n\n\n<p>Export the Admin Diagnostics of the Resource group the server sits in to the Log Analytics Workspace you created<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"409\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-2-1024x409.png\" alt=\"\" class=\"wp-image-8351 img-responsive\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-2-1024x409.png 1024w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-2-300x120.png 300w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-2-768x307.png 768w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-2.png 1097w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"438\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-3-1024x438.png\" alt=\"\" class=\"wp-image-8352 img-responsive\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-3-1024x438.png 1024w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-3-300x128.png 300w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-3-768x328.png 768w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-3.png 1092w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>Setup the Alert <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"751\" height=\"273\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-4.png\" alt=\"\" class=\"wp-image-8353 img-responsive\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-4.png 751w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-4-300x109.png 300w\" sizes=\"auto, (max-width: 751px) 100vw, 751px\" \/><\/a><\/figure>\n\n\n\n<p>Choose the Subscription <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">let DaysOfLogsToCheck = ago(7days);\nlet MaxUptime = ago(3d); \/\/ If the VM has been up for this long we want to know about it\nAzureActivity\n| where TimeGenerated > DaysOfLogsToCheck\n\/\/ ActivityStatus == \"Succeeded\" makes more sense, but in practice it can be out of order, so \"Started\" is better in the real world\n| where OperationName in (\"Deallocate Virtual Machine\", \"Start Virtual Machine\") and ActivityStatus == \"Started\"\n\/\/ We need to keep only the most recent entry of type 'Deallocate Virtual Machine' or 'Start Virtual Machine'\n| top 1 by TimeGenerated desc\n\/\/ Check if the most recent entry was \"Start Virtual Machine\" and is older than MaxUpTime\n| where OperationName == \"Start Virtual Machine\" and TimeGenerated &lt;= MaxUptime\n| project TimeGenerated, Resource, OperationName, ActivityStatus, ResourceId\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-5.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"696\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-5-1024x696.png\" alt=\"\" class=\"wp-image-8354 img-responsive\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-5-1024x696.png 1024w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-5-300x204.png 300w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-5-768x522.png 768w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2024\/10\/image-5.png 1160w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Create a new Resource group and then new Log Analytics Workspace Export the Admin Diagnostics of the Resource group the server sits in to the Log Analytics [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8350","post","type-post","status-publish","format-standard","hentry","category-research"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=8350"}],"version-history":[{"count":1,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8350\/revisions"}],"predecessor-version":[{"id":8355,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8350\/revisions\/8355"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=8350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=8350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=8350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}