{"id":4564,"date":"2019-07-19T11:29:19","date_gmt":"2019-07-19T09:29:19","guid":{"rendered":"http:\/\/contoso.se\/blog\/?p=4564"},"modified":"2019-07-19T11:31:37","modified_gmt":"2019-07-19T09:31:37","slug":"monitoring-windows-services-with-azure-monitor","status":"publish","type":"post","link":"http:\/\/contoso.se\/blog\/?p=4564","title":{"rendered":"Monitoring Windows services with Azure Monitor"},"content":{"rendered":"\n<p>Another question we are asked regularly is\nhow to use the Azure Monitor tools to create visibility on Windows service\nhealth. One of the best options for monitoring of services across Windows and\nLinux leverages off the <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/automation\/change-tracking\">Change\nTracking solution<\/a> in Azure Automation.<\/p>\n\n\n\n<p class=\"has-text-color has-black-color\">The solution can track changes on both Windows and Linux. On Windows, it supports tracking changes on files, registry keys, services, and installed software. On Linux, it tracks changes to files, software, and daemons. There are a couple of ways to onboard the solution, from a virtual machine, Automation account, or an Azure Automation runbook. Read more about Change tracking and how to onboard at <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/automation\/change-tracking\">Microsoft Docs<\/a><a>.<\/a><\/p>\n\n\n\n<p>This blog post will focus on monitoring of a Window service, but the\nconcept works the same for Linux daemons. <\/p>\n\n\n\n<p>Changes to Windows Services are collected by default every 30 minutes but can be configured to be collected down to every 10 seconds. It is important that the agent only track changes, not the current state. If there is no change, then there is no data sent to Log Analytics and Azure Automation. Collecting only changes optimizes the performance of the agent. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-5.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-5.png\" alt=\"\" class=\"wp-image-4566\" width=\"480\" height=\"106\" srcset=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-5.png 480w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-5-300x66.png 300w\" sizes=\"(max-width: 480px) 100vw, 480px\" \/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Query collected data<\/strong><\/p>\n\n\n\n<p>To list the latest collected data, we can\nrun the following query. Note that we use \u00e2\u20ac\u0153let\u00e2\u20ac\u009d to set offset between UTC\n(default time zone in Log Analytics) and our current time zones. An important\nthing to remember is what we said earlier; only changes are reported. In the\nexample below, we can see that at 2019-07-15 the service changed state to\nrunning. But after this record, we have no information. If the VM suddenly\ncrashes, there is a risk no \u00e2\u20ac\u0153Stopped\u00e2\u20ac\u009d event will be reported, and from a logging\nperspective, it will look like the service is running. <\/p>\n\n\n\n<p>It is therefore important to monitoring\neverything from a different point of views, for example, in this example also\nmonitor the heartbeat from the VM. <\/p>\n\n\n\n<p><code>let utcoffset = 2h; \/\/ difference between local time zone and UTC<br \/> ConfigurationData <br \/> | where ConfigDataType == \"WindowsServices\"<br \/> | where SvcDisplayName == \"Print Spooler\" <br \/> | extend localTimestamp = TimeGenerated + utcoffset<br \/> | project localTimestamp, Computer, SvcDisplayName, SvcState <br \/> | order by localTimestamp desc <br \/> | summarize arg_max(localTimestamp, *) by SvcDisplayName<\/code><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-6.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-6.png\" alt=\"\" class=\"wp-image-4567\" width=\"480\" height=\"371\" srcset=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-6.png 480w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-6-300x232.png 300w\" sizes=\"(max-width: 480px) 100vw, 480px\" \/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Configure alert on service changes<\/strong><\/p>\n\n\n\n<p>As with other collected data, it is possible to configure an alert rule based on service changes. Below is a query that can be used to alert if the Print Spooler service is stopped. For more steps how to configure the alert, see <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/automation\/change-tracking#alert-on-changes\">Microsoft Docs<\/a>. <\/p>\n\n\n\n<p><code>ConfigurationChange <br \/> | where ConfigChangeType == \"WindowsServices\" and SvcDisplayName == \"Print Spooler\" and SvcState == \"Stopped\"<\/code><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-7.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-7.png\" alt=\"\" class=\"wp-image-4568\" width=\"442\" height=\"480\" srcset=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-7.png 442w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-7-276x300.png 276w\" sizes=\"(max-width: 442px) 100vw, 442px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>You may be tempted to use a query to look\nfor Event 7036 in the Application log instead, but there are a few reasons why\nwe would recommend you use the ConfigurationChange data instead:<\/p>\n\n\n\n<ul><li>To be able to alert on Event\n7036, you will need to collect informational level events from the Application\nlog across all Windows servers, which becomes impractical very quickly when you\nhave a larger number of Virtual Machines<\/li><li>It requires more complex\nqueries to alert on specific services<\/li><li>It is only available on Windows\nservers<\/li><\/ul>\n\n\n\n<p><strong>Workbook report<\/strong><\/p>\n\n\n\n<p>With Azure Monitor workbooks, we can create interactive reports based on collected data. Read more about Workbooks at <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-monitor\/app\/usage-workbooks\">Microsoft Docs<\/a>.<\/p>\n\n\n\n<p>For our service monitoring scenario, this is a great way to build a\nreport of current status and a dashboard. <\/p>\n\n\n\n<p>The following query can be used to list the latest event for each Windows\nservice on each server. With the \u00e2\u20ac\u0153case\u00e2\u20ac\u009d operator, we can display 1 for running\nservices and 0 for stopped services. <\/p>\n\n\n\n<p>let utcoffset = 2h; \/\/ difference between local time zone and UTC<br \/> ConfigurationData<br \/> | where ConfigDataType == &#8220;WindowsServices&#8221;<br \/> | extend localTimestamp = TimeGenerated + utcoffset<br \/> | extend Status = case(SvcState == &#8220;Stopped&#8221;, &#8220;0&#8221;,<br \/> SvcState == &#8220;Running&#8221;, &#8220;1&#8221;,<br \/> &#8220;NA&#8221;<br \/> )<br \/> | project localTimestamp, Computer, SvcDisplayName, Status<br \/> | summarize arg_max(localTimestamp, *) by Computer, SvcDisplayName<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-8.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-8.png\" alt=\"\" class=\"wp-image-4569\" width=\"480\" height=\"116\" srcset=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-8.png 480w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-8-300x73.png 300w\" sizes=\"(max-width: 480px) 100vw, 480px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>1 and 0 can easily be used as thresholds in a workbook to colour set\ncells depending on status.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-9.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-9.png\" alt=\"\" class=\"wp-image-4570\" width=\"480\" height=\"246\" srcset=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-9.png 480w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-9-300x154.png 300w\" sizes=\"(max-width: 480px) 100vw, 480px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>Workbooks can also be pinned to an Azure Dashboard, either all parts of a workbook or just some parts of it. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-10.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-10.png\" alt=\"\" class=\"wp-image-4571\" width=\"480\" height=\"253\" srcset=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-10.png 480w, http:\/\/contoso.se\/blog\/wp-content\/uploads\/2019\/07\/image-10-300x158.png 300w\" sizes=\"(max-width: 480px) 100vw, 480px\" \/><\/a><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Another question we are asked regularly is how to use the Azure Monitor tools to create visibility on Windows service health. One of the best options for monitoring of services across Windows and Linux leverages off the Change Tracking solution in Azure Automation. The solution can track changes on both Windows and Linux. On Windows, &hellip; <a href=\"http:\/\/contoso.se\/blog\/?p=4564\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":4571,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[64,75,71],"tags":[77,72,76,73],"_links":{"self":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4564"}],"collection":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4564"}],"version-history":[{"count":3,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4564\/revisions"}],"predecessor-version":[{"id":4574,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4564\/revisions\/4574"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/media\/4571"}],"wp:attachment":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4564"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}