Ad Widget

Collapse

Restarting Zabbix Agent remotely via zabbix server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • f0x0ff
    Junior Member
    • Aug 2015
    • 11

    #1

    Restarting Zabbix Agent remotely via zabbix server

    Hello,

    There is a windows machine (2012R2) with zabbix agent installed as a service. In the configuration file, I've allowed remote commands and it seems that it works.
    I can run a simple commands with zabbix_get like "system.run['dir']" and so on. My question is - is it possible to restart "Zabbix Agent" service from zabbix server?
    I tryied with "system.run['net stop "Zabbix Agent" & net start "Zabbix Agent"']" on a test Windows machine, but this only stops the agent without starting it again and it was needed to start it manually from RDP session.
    Because I'm not administrating most of the servers I would like to avoid reaching MS admins every time I need to do something simple like zabbix agent's configuration file changes.

    Regards
  • geniepage
    Member
    • Sep 2015
    • 34

    #2
    Hello,
    I see the best solution write script which do whole restart of agent. In time of restart you have situation:
    - sending to agent port stop
    - sending to agent port start
    First one is ok, but second one is not possible due to stoped agent. It means you have to run on host script which do whole restart.

    Regards
    Genie

    Comment

    • f0x0ff
      Junior Member
      • Aug 2015
      • 11

      #3
      Originally posted by geniepage
      Hello,
      I see the best solution write script which do whole restart of agent. In time of restart you have situation:
      - sending to agent port stop
      - sending to agent port start
      First one is ok, but second one is not possible due to stoped agent. It means you have to run on host script which do whole restart.

      Regards
      Genie
      I thought the same Now wondering how can I simply transfer this to the MS host via Remote Commands... if it was linux it could be really simple (also here we have kill -9, service restart and so on...), but on Windows, even simple output redirect with ">>" is not working....

      Comment

      • geniepage
        Member
        • Sep 2015
        • 34

        #4
        Hello,
        don't tell me anything about windows. It is very problematic os from my perspective, but it is very popular. We have to survive it :-)

        Regards
        Genie

        Comment

        • khansari
          Member
          • Mar 2019
          • 33

          #5
          Hello,

          Anybody haven't solution for this ?
          After some edits Remotely on zabbix_agentd.conf via Powershell, I need to restart zabbix agent service.
          But Restart command not fully work and only can stop this service.

          Regards

          Comment

          • wolfman89
            Junior Member
            • Apr 2019
            • 1

            #6
            Try to add it as a scheduled job like (restart service zabbix-agent after 3 sec ) and then the OS will take care of the service restart

            Comment

            • mauro
              Member
              • Jan 2017
              • 94

              #7
              you write:
              I've allowed remote commands and it seems that it works.
              then you can modify the agent/file system
              a batch called from user parameter is it's useless: as soon as the agent is closed, all child processes are terminated.
              However...
              you can write a batch file:
              echo 123 > c:\rebootAgentSpy.txt
              net stop "zabbix agent"

              now the first job is done.
              a second bacth verify the presence of the file (c:\rebootAgentSpy.txt), if exist remove it, wait few seconds (it could be that the service is still stopping, you can do it with a ping at 127.0.0.1 ten times) and then net start "zabbix agent"
              this second batch file are needed to be run in sheduled task, every 1-2-3-x minute...





              Comment

              • khansari
                Member
                • Mar 2019
                • 33

                #8
                Sure, this solution work, But i have more than 300 customer with zabbix agent. if i want to create task schedule, must be setup task manually for all agent servers one by one.

                I was try to create scheduler task remotely with powershell script, but this script don't worked because need to run as administrator.
                Anyway Thanks for your answers.

                Regards,
                Last edited by khansari; 17-04-2019, 09:10.

                Comment

                • mauro
                  Member
                  • Jan 2017
                  • 94

                  #9
                  you can try in other 2 ways:
                  via power shell
                  powershell -command "Restart-Service servicename -Force"
                  or via pstool (standalone executables)
                  psservice [-accepteula] [\\Computer [-u Username [-p Password]]] restart <service-name>

                  Comment

                  • khansari
                    Member
                    • Mar 2019
                    • 33

                    #10
                    Originally posted by mauro
                    you can try in other 2 ways:
                    via power shell
                    powershell -command "Restart-Service servicename -Force"
                    or via pstool (standalone executables)
                    psservice [-accepteula] [\\Computer [-u Username [-p Password]]] restart <service-name>
                    Dear mauro,
                    This solution not work too
                    When Agent Stopped, all child processes are terminated, So cannot to Start service.

                    Comment

                    • mauro
                      Member
                      • Jan 2017
                      • 94

                      #11
                      I am aware of it.
                      for that I proposed two single commands that "restart" the service instead of stopping it and then starting it.
                      personally I have not tried, so actually I do not know if they work because maybe in turn they open two processes (stop and start) which are then closed simultaneously.

                      I can propose another way ...
                      install two agents on the same machine. the first (A) is active, the second (B) is stopped.
                      instead of trying to restart A, you start B and then turn off A, the second time you start A and then turn off B.
                      multiple installation is possible thanks to the agent -m option.

                      Comment

                      • maand
                        Junior Member
                        • Mar 2019
                        • 15

                        #12
                        Hi,
                        This is working for me. and is triggered in custom.agent[restart], also building and custom.agent[update] to update the agent and configuration.

                        taskkill /im zabbix_agentd.exe /f
                        net start "zabbix server"

                        //Mathias

                        Comment

                        • scstechZ
                          Junior Member
                          • Sep 2020
                          • 2

                          #13
                          Hello,
                          Putting various pieces together, this is how you can either restart, or update the zabbix agent on a windows machine. Tested on Windows 7, but should work anywhere:
                          Assuming that IP_ADDRESS is your client computer
                          To restart the agent service:
                          zabbix_get -s IP_ADDRESS -p 10050 -k system.run['taskkill /im zabbix_agentd.exe /f && net start "zabbix agent"']
                          (note the single quote in front of the word task ('), the double quote in front of the word zabbix (")... and the double quote (") followed by a single quote (') after the word agent)

                          If you want to remotely update the agent, we can add a copy command in the middle of this (assuming that the updated agent is in c:\zabbix\newagent):
                          zabbix_get -s IP_ADDRESS -p 10050 -k system.run['taskkill /im zabbix_agentd.exe /f && copy c:\zabbix\newagent\zabbix_agentd.exe c:\zabbix\bin\win64\zabbix_agentd.exe /y && net start "zabbix agent"']

                          Both commands obviously require "EnableRemoteCommands = 1" on the client
                          I have the timeout set to 8 seconds - I have not tested with a faster timeout.
                          Both commands immediately return a failure code of "connection reset by peer", but the end result is that it works.
                          Hope this saves someone else hours of scrounging around.
                          Thank you Mathias for supplying the final piece of the puzzle

                          Comment

                          Working...