这是原厂英文文档的翻译页面. 欢迎帮助我们 完善文档.

12 限制 agent 检查

概述

可以通过创建 监控项 来限制 agent 端的检查。 黑名单、白名单或白名单/黑名单的组合。

为此,请使用两个 agent 的组合。 configuration 参数:

  • AllowKey=<pattern> - 允许哪些检查;<pattern> 是 使用通配符(*)表达式指定
  • DenyKey=<pattern> - 禁止哪些检查;<pattern> 是 使用通配符(*)表达式指定

注意:

  • 所有系统.run[*] 监控项(远程命令、脚本)默认处于禁用状态, 即使未指定任何拒绝键,也应假定隐式追加了DenyKey=system.run[*]。
  • 自 Zabbix 5.0.2 起,EnableRemoteCommands agent 参数为:
    • 由 Zabbix agent 弃用
    • Zabbix 不支持 agent2

因此,若要允许远程命令,请指定 AllowKey=system.run[<command>,*] 对于每个允许的命令,* 表示 wait 和 nowait 模式。也可以指定 AllowKey=system.run[*] 允许使用 wait 和 nowait 模式的全部命令的参数。若要禁止特定的远程命令,请添加带有 system.run[] 的 DenyKey 参数。 在 AllowKey=system.run[*] 参数之前的命令。

重要规则

  • 不带拒绝规则的白名单仅允许用于 system.run[*] 监控项。对于所有其他 监控项,不允许使用 AllowKey 参数 不带DenyKey参数;在这种情况下,Zabbix agent将不会 以 AllowKey 参数开头。
  • 顺序很重要。指定的参数将被逐个检查 根据其在配置文件中的出现顺序:
    • 一旦某个监控项键匹配了允许/拒绝规则,则该监控项将被 允许或拒绝;规则检查随即停止。因此,如果某个监控项 同时匹配允许规则和拒绝规则,则结果将 取决于哪条规则排在前面。
    • 顺序还会影响EnableRemoteCommands参数(如果使用该参数)。
  • 支持无限制数量的 AllowKey/DenyKey 参数。
  • AllowKey 和 DenyKey 规则不会影响 HostnameItem, HostMetadataItem、HostInterfaceItem 配置参数。
  • 键值模式是一个通配符表达式,其中通配符为(*) 字符匹配在特定位置的任意数量的任意字符。 它可以在键名称和参数中使用。
  • 如果在 agent 配置中禁止了特定的 监控项 键,则 监控项 将被报告为不受支持(不会给出任何提示) 原因);
  • Zabbix agent 使用 --print (-p) 命令行选项将不会显示 配置不允许的监控项键值;
  • Zabbix agent 使用 --test (-t) 命令行选项将返回 “不支持的 监控项 键。”状态用于未被允许的键。 配置;
  • 被拒绝的远程命令不会记录在 agent 日志中(如果 LogRemoteCommands=1)。

Allow/deny rule order

You can specify an unlimited number of AllowKey or DenyKey rules, though their order matters.

  • Rules are evaluated one by one, from top to bottom.
  • When an item key matches a rule, it is either allowed or denied, and rule evaluation stops.

For example, when evaluating vfs.file.contents[/etc/passwd], the rules are processed as follows:

AllowKey=vfs.file.contents[/tmp/app.log]    # Item key pattern does not match, agent proceeds to the next rule.
       AllowKey=vfs.file.contents[/etc/passwd]     # Item key pattern matches; agent allows the item check and stops rule evaluation.
       DenyKey=vfs.file.*[*]                       # Agent ignores the rule, as the evaluation has stopped.

The following rule order will deny the item check:

DenyKey=vfs.file.*[*]                       # Item key pattern matches; agent denies the item check and stops rule evaluation.
       AllowKey=vfs.file.contents[/etc/passwd]     # Agent ignores the rule, as the evaluation has stopped.
       AllowKey=vfs.file.contents[/tmp/app.log]    # Agent ignores the rule, as the evaluation has stopped.

使用场景

Allowing specific checks and commands

Allow only two vfs.file item checks and two system.run commands:

AllowKey=vfs.file.contents[/tmp/app.log]
       AllowKey=vfs.file.size[/tmp/app.log]
       AllowKey=system.run[/usr/bin/uptime]
       AllowKey=system.run[/usr/bin/df -h /]
       DenyKey=vfs.file.*[*]

Setting DenyKey=system.run[*] is unnecessary, because all other system.run commands are denied by default.

Allowing scripts

Allow Zabbix agent to execute scripts on hosts via all available methods:

  • Global scripts that can be executed in the frontend or via API (this method always uses the system.run[myscript.sh] key)
  • Remote commands from action operations (this method always uses the system.run[myscript.sh,nowait] key)
  • system.run Zabbix agent items with the script, for example:
    • system.run[myscript.sh]
    • system.run[myscript.sh,wait]
    • system.run[myscript.sh,nowait]
AllowKey=system.run[myscript.sh,*]

To control the wait/nowait parameter, you must set a different rule. For example, you can allow only system.run[myscript.sh,wait] items, thus excluding other methods:

AllowKey=system.run[myscript.sh,wait]
Securing allow/deny rules

This example shows how to secure overly permissive AllowKey or DenyKey rules.

Consider the following rules:

AllowKey=system.run["C:\Program^ Files\Zabbix^ Agent^ 2\scripts\test.bat*"]
       DenyKey=vfs.file.*
       DenyKey=system.cpu.load[*]

On Windows, you must escape spaces in the path using a caret (^).

These rules contain a wildcard (*), which can be misused:

  • The test.bat script can be executed with any arguments, including unintended ones.
  • The vfs.file.* pattern matches only item keys without parameters; however, all vfs.file items require parameters.
  • The system.cpu.load[*] pattern matches only item keys with parameters; however system.cpu.load items do not require parameters.

To secure these rules, explicitly allow executing test.bat only with specific arguments, and deny correct item key patterns; for example:

AllowKey=system.run["C:\Program^ Files\Zabbix^ Agent^ 2\scripts\test.bat status"]
       AllowKey=system.run["C:\Program^ Files\Zabbix^ Agent^ 2\scripts\test.bat version"]
       DenyKey=vfs.file.*[*]
       DenyKey=system.cpu.load
       DenyKey=system.cpu.load[*]

You can test the rules by running the following commands, which will return ZBX_NOTSUPPORTED.

cd "C:\Program Files\Zabbix Agent 2"
       zabbix_agent2.exe -t system.run["C:\Program^ Files\Zabbix^ Agent^ 2\scripts\test.bat debug"]
       zabbix_agent2.exe -t vfs.file.size["C:\ProgramData\MyApp\config.ini"]
       zabbix_agent2.exe -t vfs.file.contents["C:\Windows\System32\drivers\etc\hosts"]
       zabbix_agent2.exe -t system.cpu.load
       zabbix_agent2.exe -t system.cpu.load[all,avg1]

模式示例

模式 描述 匹配 不匹配
* 匹配所有可能的键(无论是否带有参数)。 任意
vfs.file.contents 匹配 vfs.file.contents 且不带参数。 vfs.file.contents vfs.file.contents[/etc/passwd]
vfs.file.contents[] 匹配 vfs.file.contents 且带空参数。 vfs.file.contents[] vfs.file.contents
vfs.file.contents[*] 匹配 vfs.file.contents 且带任意参数;不会匹配不带方括号的 vfs.file.contents vfs.file.contents[]
vfs.file.contents[/path/to/file]
vfs.file.contents
vfs.file.contents[/etc/passwd,*] 匹配 vfs.file.contents 且第一个参数匹配 /etc/passwd,其余参数可以是任意值(包括空值)。 vfs.file.contents[/etc/passwd,]
vfs.file.contents[/etc/passwd,utf8]
vfs.file.contents[/etc/passwd]
vfs.file.contents[/var/log/zabbix_server.log]
vfs.file.contents[]
vfs.file.contents[*passwd*] 匹配 vfs.file.contents 且第一个参数匹配 *passwd*,没有其他参数。 vfs.file.contents[/etc/passwd] vfs.file.contents[/etc/passwd,]
vfs.file.contents[/etc/passwd, utf8]
vfs.file.contents[*passwd*,*] 匹配 vfs.file.contents 且第一个参数匹配 *passwd*,后续参数可以是任意值(包括空值)。 vfs.file.contents[/etc/passwd,]
vfs.file.contents[/etc/passwd, utf8]
vfs.file.contents[/etc/passwd]
vfs.file.contents[/tmp/test]
vfs.file.contents[/var/log/zabbix_server.log,*,abc] 匹配 vfs.file.contents 且第一个参数匹配 /var/log/zabbix\_server.log,第三个参数匹配 abc,第二个参数可以是任意值(包括空值)。 vfs.file.contents[/var/log/zabbix_server.log,,abc]
vfs.file.contents[/var/log/zabbix_server.log,utf8,abc]
vfs.file.contents[/var/log/zabbix_server.log,,abc,def]
vfs.file.contents[/etc/passwd,utf8] 匹配 vfs.file.contents 且第一个参数匹配 /etc/passwd,第二个参数匹配 utf8,没有其他参数。 vfs.file.contents[/etc/passwd,utf8] vfs.file.contents[/etc/passwd,]
vfs.file.contents[/etc/passwd,utf16]
vfs.file.* 匹配以 vfs.file. 开头且不带任何参数的所有键。 vfs.file.contents
vfs.file.size
vfs.file.contents[]
vfs.file.size[/var/log/zabbix_server.log]
vfs.file.*[*] 匹配以 vfs.file. 开头且带有任意参数的所有键。 vfs.file.size.bytes[]
vfs.file.size[/var/log/zabbix_server.log, utf8]
vfs.file.size.bytes
vfs.*.contents 匹配以 vfs. 开头并以 .contents 结尾且不带任何参数的所有键。 vfs.mount.point.file.contents
vfs..contents
vfs.contents