Search code examples
powershellscriptingmonitoringzabbix

Zabbix script timeout


I'm trying to run powershell script for cleaning disk on agent from Zabbix. Seems that script is running too long and instead response I got error.

powershell -NonInteractive C:\Scripts\CleanDisk.ps1 -deleteLogsOlderThanDays 10

Script deletes some logs and temp folders and prints statistics. If there is only few folders to delete it work fines. But if script runs too long then dialog windows with script result ends with error Get value from agent failed: ZBX_TCP_READ

Guess that it's because connection to client timeout. Is there some way how to get over this limitation? Thx


Solution

  • My colleagues found usable workaround. Instead of starting script which will run long time it's better to only schedule script with schtask.exe. So I modified script, now it contains two parts. One is responsible for scheduling and starting scheduled task (schedule it self but with different arguments), second heavy and long running does the action. Result of scheduling will appear in execution script dialog box in zabbix, result of long running action is going to log file...

    Here is example of powershell script StartCleanDisk.ps1. In this case task will be scheduled and immediately executed by scheduler.

    StartCleanDisk.ps1 -deleteLogsOlderThanDays 10 -startAsTask 1
    

    In this case task will directly executed.

    StartCleanDisk.ps1 -deleteLogsOlderThanDays 10 -startAsTask 0
    

    StartCleanDisk.ps1 content:

    [CmdletBinding()]
    param
    (
        [parameter(Mandatory=$true)]
        [int]
        $deleteLogsOlderThanDays,
        [bool]
        $startAsTask = $false
    )
    
    if ($startAsTask)
    {
        Write-Output "Scheduling task for cleaning disk ...";
    
      $taskname = "CleanDisk"
        $logFile = "X:\logs\Tasks\cleaningDisk.log";
        $task = "powershell $PSScriptRoot\StartCleanDisk.ps1 -deleteLogsOlderThanDays $deleteLogsOlderThanDays -startAsTask 0 > $logFile 2>&1";
        & schtasks /create /ru "System" /tn $taskname /tr $task /sc once /ST 23:59 /F /V1 /Z;
        Write-Output "Task Clean disk created...";
        & schtasks /run /tn $taskname;
        Write-Output "Task $taskname started... Please chek $logFile";
        exit 0;
    }
    
    #####################
    # Script begins here
    # PUT HERE COMMANDS FOR DELETING
    #####################