Ad Widget

Collapse

Agent remote script not working

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • markfree
    Senior Member
    • Apr 2019
    • 868

    #1

    Agent remote script not working

    I have created a global script that runs on Zabbix Agent with a scope of manual host action to remotely reboot a Windows host at a specific time, 1 AM.
    Code:
    @echo off
    setlocal
    :: SECONDS COUNT UNTIL THE NEXT 1H AM,
    :: EITHER IN THE SAME DAY OR THE NEXT ONE.
    :: by diasdm
    for /f "delims=:. tokens=1,2,3" %%a in ("%time%") do (
    set /a "hours=%%a"
    set /a "minutes=%%b"
    set /a "seconds=%%c"
    )
    if %hours% LSS 1 ( set /a "hoursLeft=1" ) else ( set /a "hoursLeft=25" )
    set /a "secondsUntil1AM=(hoursLeft - hours) * 3600 - minutes * 60 - seconds"
    :: RESTART COMMAND IS SENT TO HOST.
    shutdown /t %secondsUntil1AM% /r /f /m \\{HOST.CONN} /c "System will reboot at 1AM. Command initiated via Zabbix ({USER.FULLNAME})."
    echo TIME LEFT TO REBOOT: %secondsUntil1AM%s
    Click image for larger version  Name:	script_agent.png Views:	0 Size:	14.3 KB ID:	469611

    The host's Agent 2 already has the "system.run[*]" parameter.
    Whenever a user needs to run this script, they should click on the host name in the problem list and run it.

    If I run the script locally, it runs fine.
    Code:
    c:\zabbix> reboot.bat
    TIME LEFT TO REBOOT: 32176S
    Click image for larger version  Name:	script_event.png Views:	0 Size:	4.3 KB ID:	469612

    When I simulate an item (system.run[c:\zabbix\reboot.bat]) and test it, it also runs without issues.
    Click image for larger version  Name:	script_from_item.png Views:	0 Size:	21.3 KB ID:	469609


    However, when I run the global script from the problem list, it shows a success message, but the reboot command is not actually issued.
    Click image for larger version  Name:	script_from_problem.png Views:	0 Size:	4.5 KB ID:	469610

    Also, there are no messages in the Agent or Server log.

    Any thoughts on this?
    Thank you.​
    Last edited by markfree; 30-08-2023, 17:46.
  • Answer selected by markfree at 01-09-2023, 01:58.
    markfree
    Senior Member
    • Apr 2019
    • 868

    Referring to the documentation,
    Zabbix reads from the pipe until timeout occurs or no one is writing to the other end (ALL handles/file descriptors have been closed). (...)
    If the timeout has not been reached, Zabbix waits until the initial child process exits or timeout occurs.
    Also,
    Zabbix assumes that a command/script has done processing when the initial child process has exited AND no other process is still keeping the output handle/file descriptor open.
    The remote command documentation says:
    Remote commands on Zabbix agent are executed without timeout by the system.run[,nowait] key and are not checked for execution results.
    From my perspective, the CMD process serves as the child process, and its completion should coincide with the execution completion of all enclosed commands.
    This functionality holds true for Linux, including multi line scripts.
    However, my knowledge is limited when it comes to Windows processes, handles, and file descriptors.
    So, I wonder why is it exiting the initial child process with only one line?

    In any case, I managed to execute the script by converting it to PowerShell and condensing it into a single line. Nevertheless, the presence of "shutdown.exe" persists.

    The initial script turned out like this:
    Code:
    powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "& { $currentTime = Get-Date ; $hours = $currentTime.Hour ; $minutes = $currentTime.Minute ; $seconds = $currentTime.Second ; if ($hours -lt 1) { $hoursLeft = 1 } else { $hoursLeft = 25 } ; $secondsUntil1AM = ($hoursLeft - $hours) * 3600 - $minutes * 60 - $seconds ; shutdown.exe /t $secondsUntil1AM /r /f /m \\{HOST.CONN} /c 'System will reboot at 1AM. Command initiated by Zabbix ({USER.FULLNAME}).' ; Write-Host "TIME LEFT TO REBOOT: $($secondsUntil1AM)s" }"
    Another option would be to use a Base64 encoded command to make it a character string, but I thought that would be just too much hassle.
    ​​
    Last edited by markfree; 01-09-2023, 01:55.

    Comment

    • markfree
      Senior Member
      • Apr 2019
      • 868

      #2
      I'm not a Windows Batch expert, but it seems neither is Zabbix.
      I've tried a number of syntax combinations already.

      When I tried simple echo commands, one by line, I found that only the first one was sent to the host.
      Click image for larger version  Name:	image.png Views:	0 Size:	25.2 KB ID:	469623
      Click image for larger version  Name:	image.png Views:	0 Size:	91.4 KB ID:	469624


      However, If I try the same echo commands in one line, they are all sent.
      Click image for larger version  Name:	image.png Views:	0 Size:	25.8 KB ID:	469625
      Click image for larger version  Name:	image.png Views:	0 Size:	93.6 KB ID:	469626

      I'm not sure why that is and couldn't find specific information in the documentation.​
      Last edited by markfree; 31-08-2023, 01:31.

      Comment

      • Hamardaban
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • May 2019
        • 2713

        #3
        IMHO When executing the script, zabbix expects a return code from it. As soon as he received it, it means that the execution is finished.
        Adding the task worked and returned the code - that's it! Zabbix is not interested in what happens next.

        Try use "call" or "start" command
        or "shutdown ​bla-bla_bla > NUL 2>&1 "
        Last edited by Hamardaban; 31-08-2023, 08:29.

        Comment

        • markfree
          Senior Member
          • Apr 2019
          • 868

          #4
          Referring to the documentation,
          Zabbix reads from the pipe until timeout occurs or no one is writing to the other end (ALL handles/file descriptors have been closed). (...)
          If the timeout has not been reached, Zabbix waits until the initial child process exits or timeout occurs.
          Also,
          Zabbix assumes that a command/script has done processing when the initial child process has exited AND no other process is still keeping the output handle/file descriptor open.
          The remote command documentation says:
          Remote commands on Zabbix agent are executed without timeout by the system.run[,nowait] key and are not checked for execution results.
          From my perspective, the CMD process serves as the child process, and its completion should coincide with the execution completion of all enclosed commands.
          This functionality holds true for Linux, including multi line scripts.
          However, my knowledge is limited when it comes to Windows processes, handles, and file descriptors.
          So, I wonder why is it exiting the initial child process with only one line?

          In any case, I managed to execute the script by converting it to PowerShell and condensing it into a single line. Nevertheless, the presence of "shutdown.exe" persists.

          The initial script turned out like this:
          Code:
          powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "& { $currentTime = Get-Date ; $hours = $currentTime.Hour ; $minutes = $currentTime.Minute ; $seconds = $currentTime.Second ; if ($hours -lt 1) { $hoursLeft = 1 } else { $hoursLeft = 25 } ; $secondsUntil1AM = ($hoursLeft - $hours) * 3600 - $minutes * 60 - $seconds ; shutdown.exe /t $secondsUntil1AM /r /f /m \\{HOST.CONN} /c 'System will reboot at 1AM. Command initiated by Zabbix ({USER.FULLNAME}).' ; Write-Host "TIME LEFT TO REBOOT: $($secondsUntil1AM)s" }"
          Another option would be to use a Base64 encoded command to make it a character string, but I thought that would be just too much hassle.
          ​​
          Last edited by markfree; 01-09-2023, 01:55.

          Comment

          Working...