{"id":38699,"date":"2023-08-24T02:23:25","date_gmt":"2023-08-24T02:23:25","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=38699"},"modified":"2023-08-24T02:23:28","modified_gmt":"2023-08-24T02:23:28","slug":"how-to-monitor-a-service-of-windows-where-service-name-changes-on-every-restart-zabbix","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-monitor-a-service-of-windows-where-service-name-changes-on-every-restart-zabbix\/","title":{"rendered":"How to monitor a service of windows where service name changes on every restart zabbix?"},"content":{"rendered":"\n<p>Monitoring a Windows service with a changing name using Zabbix can be complex due to the changing service name. One approach is to use Zabbix&#8217;s LLD (Low-Level Discovery) feature along with scripting to dynamically discover and monitor the changing service.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method &#8211; 1<\/h2>\n\n\n\n<p>Here&#8217;s a step-by-step guide:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Create a PowerShell Script:<\/strong><\/h2>\n\n\n\n<p>Write a PowerShell script that discovers the service name each time it&#8217;s executed. The script should output JSON in a specific format that Zabbix understands for LLD.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$services = Get-Service | Where-Object { $_.Status -eq <span class=\"hljs-string\">\"Running\"<\/span> } | Select-Object -Property DisplayName\r\n\r\n$discoveryResult = @()\r\n<span class=\"hljs-keyword\">foreach<\/span> ($service in $services) {\r\n    $discoveryResult += @{\r\n        <span class=\"hljs-string\">\"{#SERVICE_NAME}\"<\/span> = $service.DisplayName\r\n    }\r\n}\r\n\r\n$discoveryResult | ConvertTo-Json\r\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Configure LLD Rule in Zabbix:<\/strong> In Zabbix, create a discovery rule that uses the PowerShell script to discover the running services. Define a LLD rule that processes the JSON output from the script.<\/li>\n\n\n\n<li><strong>Create Item Prototypes:<\/strong> Once services are discovered, you can create item prototypes based on the discovered services. These item prototypes will monitor the status of each service.<\/li>\n\n\n\n<li><strong>Create Trigger Prototypes:<\/strong> Similarly, create trigger prototypes for each item prototype to raise alerts based on the service status.<\/li>\n\n\n\n<li><strong>Configure Host:<\/strong> Set up a Zabbix host for the Windows machine you&#8217;re monitoring. Associate the discovery rule and item prototypes with this host.<\/li>\n\n\n\n<li><strong>Schedule Discovery:<\/strong> Configure a schedule for the discovery rule so that Zabbix runs the PowerShell script at regular intervals.<\/li>\n<\/ol>\n\n\n\n<p>With this setup, Zabbix will dynamically discover the running services and monitor them accordingly, even if the service name changes on every restart.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method &#8211; 2<\/h2>\n\n\n\n<p>There are a few ways to monitor a service of Windows where the service name changes on every restart in Zabbix.<\/p>\n\n\n\n<p>One way is to use the <code>service.active<\/code> item key. The <code>service.active<\/code> item key returns the name of the service that is currently running. So, if the service name changes, the Zabbix item will also change.<\/p>\n\n\n\n<p>To do this, you can create a Zabbix item that monitors the service using the <code>service.active<\/code> item key. In the <code>Params<\/code> field, specify the name of the service that you want to monitor.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"930\" height=\"207\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-575.png\" alt=\"\" class=\"wp-image-38700\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-575.png 930w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-575-300x67.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-575-768x171.png 768w\" sizes=\"auto, (max-width: 930px) 100vw, 930px\" \/><\/figure>\n\n\n\n<p>You can also create a Zabbix trigger that is based on the <code>service.active<\/code> item. The trigger will be triggered if the service name changes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"161\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-576-1024x161.png\" alt=\"\" class=\"wp-image-38701\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-576-1024x161.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-576-300x47.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-576-768x121.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-576.png 1168w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Method -3 <\/h2>\n\n\n\n<p>Another way to monitor a service of Windows where the service name changes on every restart in Zabbix is to use the <code>service.info<\/code> item key. The <code>service.info<\/code> item key returns the current name of the service. So, if the service name changes, the Zabbix item will also change.<\/p>\n\n\n\n<p>To do this, you can create a Zabbix item that monitors the service using the <code>service.info<\/code> item key. In the <code>Params<\/code> field, specify the name of the service that you want to monitor.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"209\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-577-1024x209.png\" alt=\"\" class=\"wp-image-38702\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-577-1024x209.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-577-300x61.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-577-768x157.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-577.png 1033w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You can also create a Zabbix trigger that is based on the <code>service.info<\/code> item. The trigger will be triggered if the service name changes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"160\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-578-1024x160.png\" alt=\"\" class=\"wp-image-38703\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-578-1024x160.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-578-300x47.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-578-768x120.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-578.png 1312w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Finally, you can also use the <code>service.state<\/code> item key to monitor a service of Windows where the service name changes on every restart in Zabbix. The <code>service.state<\/code> item key returns the state of the service as a number. The following values are possible:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>0: The service is not running.<\/li>\n\n\n\n<li>1: The service is running.<\/li>\n\n\n\n<li>2: The service is starting.<\/li>\n\n\n\n<li>3: The service is stopping.<\/li>\n<\/ul>\n\n\n\n<p>You can use the <code>service.state<\/code> item key to create a Zabbix trigger that monitors the state of the service and generates an alert if the state changes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"161\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-579-1024x161.png\" alt=\"\" class=\"wp-image-38704\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-579-1024x161.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-579-300x47.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-579-768x121.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-579.png 1150w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Can you use regular expression in service name?<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"393\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-580-1024x393.png\" alt=\"\" class=\"wp-image-38705\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-580-1024x393.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-580-300x115.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-580-768x294.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2023\/08\/image-580.png 1333w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Monitoring a Windows service with a changing name using Zabbix can be complex due to the changing service name. One approach is to use Zabbix&#8217;s LLD (Low-Level Discovery) feature along with scripting to dynamically discover and monitor the changing service. Method &#8211; 1 Here&#8217;s a step-by-step guide: Create a PowerShell Script: Write a PowerShell script&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-38699","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/38699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=38699"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/38699\/revisions"}],"predecessor-version":[{"id":38706,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/38699\/revisions\/38706"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=38699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=38699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=38699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}