Skip to content

Zabbix user-defined monitoring with PowerShell

Zabbix user-defined monitoring with PowerShell enables administrators to track custom Metrics, such as specific Java processes or application states, on Windows environments. This technique uses Zabbix's UserParameter directive to execute PowerShell scripts and return values to the monitoring server^[600-developer-operation-maintenance-zabbix-zabbix-key.md].

Core Configuration

To enable user-defined scripts and remote command execution, specific settings must be configured in the Zabbix agent configuration file (e.g., zabbix_agentd.conf).^[600-developer-operation-maintenance-zabbix-zabbix-key.md]

  • EnableRemoteCommands: Set to 1 to allow the Zabbix server to execute remote commands on the agent^[600-developer-operation-maintenance-zabbix-zabbix-key.md].
  • UnsafeUserParameters: Set to 1 to allow special characters (like spaces or quotes) to be passed in user-defined parameter arguments^[600-developer-operation-maintenance-zabbix-zabbix-key.md].
  • Timeout: May need to be adjusted (e.g., Timeout=30) if scripts take longer to execute than the default time^[600-developer-operation-maintenance-zabbix-zabbix-key.md].

The UserParameter directive defines the key and the command to run.^[600-developer-operation-maintenance-zabbix-zabbix-key.md]

UserParameter=mypay.robot[*],C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File C:\Zabbix\robot.ps1 $1 $2 $3 $4 $5 $6 $7 $8 $9

PowerShell Execution Policy

By default, Windows may restrict the execution of PowerShell scripts. To resolve "file cannot be loaded because scripting is disabled on this system" errors, the execution policy must be modified using administrator privileges^[600-developer-operation-maintenance-zabbix-zabbix-key.md].

Set-ExecutionPolicy RemoteSigned

Monitoring Logic

The PowerShell script performs the logic required to generate the metric^[600-developer-operation-maintenance-zabbix-zabbix-key.md]. A common pattern is checking for the existence of specific Java Virtual Machines (JVM) using tools like jps or jinfo.

For example, to count specific processes:^[600-developer-operation-maintenance-zabbix-zabbix-key.md]

$arr = $args[0]
$mark = 0
Get-Process -Name java | select -expand id | ForEach {
    $queryCondition = jinfo $_
    $isExist = $queryCondition.split(' ') | Select-String -include string $arr -CaseSensitive 
    if ($isExist.length -gt 0) {
        $mark += 1
    }
}
echo $mark

The script outputs the result (e.g., "1" for found, "0" for not found, or a count) to standard output using Write-Host or echo, which Zabbix reads^[600-developer-operation-maintenance-zabbix-zabbix-key.md].

  • [[Zabbix]]
  • [[PowerShell]]

Sources

^[600-developer-operation-maintenance-zabbix-zabbix-key.md]