Ad Widget

Collapse

Zabbix 6.4 Restart a Windows service

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pcrock
    Junior Member
    • Jul 2023
    • 1

    #1

    Zabbix 6.4 Restart a Windows service


    I wanted to be able to get Zabbix to start a specific stopped Windows services with an action in 6.4 if it stops.
    It's a fairly simple solution but it's taken me hours to get there so hopefully this helps.
    I'm certain someone will point out a simpler way, but this is what worked for me.

    Step 1

    The first thing I had to do was add a new tag to the service discovery template.
    I opened the 'Windows by Zabbix agent' template. Went to Discovery>Windows service discovery>Trigger prototypes.
    There was only one entry in there for service not running, went into that and then tags and added a new one of servicename and {#SERVICE.NAME} for the value.

    I then ran a rediscovery on the host. This puts a tag on the trigger with the service name that you use later in the script. With enough time it would have sorted this out itself.

    Step 2

    I then had to create a script to restart the service. Alerts>Scripts

    I created a new one called 'Restart Windows Service' and selected action operation for scope, script for type and zabbix agent on execute on.
    In the commands I put
    c:\windows\system32\net start {EVENT.TAGS.servicename}

    Theory being when it runs it replaces the tag with the Windows service name, and it seems to work nicely.

    Step 3

    I then had to create a trigger action, Alerts>Actions>Trigger Actions.
    I created a new one called 'restart service action'.
    On the conditions I added an entry of value of tag 'servicerestart' = true and the operation to run the script from earlier.

    This part baffled me, until you create the script, the only option you get is to send a message, once you create the script that becomes an option in the operations.

    Step 4

    I then cloned the trigger for the Windows service I wanted to restart if it fails and added a new tag of servicerestart with a value of true.
    I've always wondered if there was an easier way of doing this but it works.

    When the trigger fires for that service, it kicks off the script and starts it and it seems to work nicely.
    You do have to have
    AllowKey=system.run[*]
    In the zabbix_agent2.conf file for it to allow it.

    As I say, hopefully this helps someone you should be able to adapt this for other jobs to run too.​
  • etaylor@gsp.net
    Junior Member
    • Aug 2023
    • 1

    #2
    Hi,

    I am new to Zabbix (6.4). I followed your instructions and unfortunately it is not restarting any services. I checked the logs and I do not see the action to run the script. Do you have any suggestions?​

    Comment

    • conny.lindqvist@presto.se
      Junior Member
      • Jun 2024
      • 1

      #3
      Hi. I know this thread/post is older but I thought I add a solution that works for Zabbix 7.0

      I wanted a solution that works on any windows host with any service (discovered) and that seamlessly starts a service that has stopped. I used PCROCKs solution with a bit of a twist at the end.

      Step 1

      Add new tags:
      Got to 'Windows by Zabbix agent active' template. (or whatever template you use)
      Go to Discovery>Windows service discovery>Trigger prototypes.

      Click image for larger version

Name:	image.png
Views:	3138
Size:	30.9 KB
ID:	491086
      There is only one entry in there for "service not running" Go into that and then tags and add
      "servicename" and {#SERVICE.NAME} for the value.
      "servicerestart" and "true" for the value

      This puts tags on the triggers with the service name that the script needs ({EVENT.TAGS.servicename}) and the "true" value that you use later in trigger conditions. Either run re-discovery or wait for a time and it will update all services with the two new tags.



      Step 2

      Create a script to restart the service. Alerts>Scripts

      Call it 'Restart Windows Service' (or whatever you fancy) and select "action operation" for scope, "script" for type and "zabbix agent" to execute on.
      In the commands:
      c:\windows\system32\net start {EVENT.TAGS.servicename}


      Note: This step is necessary for step 3 to work.

      Step 3

      Create a trigger action in Alerts>Actions>Trigger Actions.
      Call it something like 'restart service action'.
      On the conditions add an entry of value of tag 'servicerestart' = true and the operation to run the script from earlier.

      Looks like this in the condition view.



      Looks like this once you added it.

      This is where my solution differs. I did not clone the triggers. I only needed the two tags in step 1 to make this work in Zabbix 7.0.

      Go to Operations
      You now see a dropdown (the one that baffled PCROCK and me too before I realized how it worked) with your script.
      Choose it and check the box "Current host".

      Your now done with the action setting.

      Click image for larger version  Name:	image.png Views:	0 Size:	6.8 KB ID:	491084
      Note that this will trigger on any host windows service with the tag that we defined in conditions and only on the "current" host. Not on all hosts. If you use "Host groups" or "Hosts" with several hosts it will run the script on them all for that specific service regardless if its down or not. That's why I use "current host" only.
      ​Also, if you want to do this on Linux hosts I suggest to use another tag name for the condition to be able to separate them.

      Last but no least you need the:
      AllowKey=system.run[*] in the zabbix_agent.conf file on your local windows host for it to allow it.

      Hope this updated version helps someone.

      Note: I tested this with and without a local admin user on the "Zabbix Agent" service. It works either way.
      Last edited by [email protected]; 11-09-2024, 11:32.

      Comment

      • ismail.karagoz
        Junior Member
        • Aug 2024
        • 1

        #4
        Hi friend,

        We created the necessary objects but we are getting below error message.
        Click image for larger version

Name:	image.png
Views:	2287
Size:	13.9 KB
ID:	497470
        We have successfully checked the server names on tags ;
        Click image for larger version

Name:	image.png
Views:	2299
Size:	23.9 KB
ID:	497471​We also add that event tag on the script;
        Click image for larger version

Name:	image.png
Views:	2305
Size:	21.7 KB
ID:	497472

        Do you have any suggestions to us?

        By the way thanks for your amazing effort. We have learned a lots of good information from this article
        Attached Files

        Comment

        Working...