[comment]: # ({ba48d9ce-843681e4})
# 7 脚本

[comment]: # ({/ba48d9ce-843681e4})

[comment]: # ({beadb9f7-931d02aa})
#### 概述

在 *Administration → Scripts（管理 → 脚本）*
部分中，可以配置和维护用户定义的全局脚本。每个脚本可以根据需要应用于不同的主机。另请参见[命令执行](/zh/manual/appendix/command_execution).

根据设置的用户权限，可以通过在各个前端位置 *（Dashboard, Problems,
Latest data,
Maps）*上单击主机来执行脚本，这些脚本也可以作为操作操作运行。这些脚本在Zabbix
server (proxy) 或agent上执行

默认情况下，Zabbix agent和Zabbix
proxy远程脚本均处于禁用状态。可以通过以下方式启用它们：

-   `AllowKey=system.run[*]`在代理配置中添加参数；
-   在代理配置中（还将在Zabbix
    5.0.2之前的代理配置中）将EnableRemoteCommands参数设置为“1”

将显示现有脚本的列表及其详细信息。

![](../../../../../assets/zh/manual/web_interface/scripts0.png){width="600"}

显示数据:

|参数            描|说明|
|---------------------|------|
|*Name*|脚本的名称。单击脚本名称将打开脚本[配置表单](/zh/manual/web_interface/frontend_sections/administration/scripts).|
|*Type*|“脚本类型”显示为“脚本”或“IPMI命令”。|
|*Execute on*|显示该脚本是在 Zabbix server 或 agent上执行。|
|*Commands*|显示脚本中需要执行的所有命令。|
|*User group*|显示脚本可用的用户组(或所有用户组)。|
|*Host group*|显示脚本可用的主机组(或所有主机组)。|
|*Host access*|主机组的权限级别显示为“读”或“写”。只有具有所需权限级别的用户才能访问脚本的执行。|

要配置新脚本，请单击右上角的 *Create script（创建脚本）*按钮。

[comment]: # ({/beadb9f7-931d02aa})

[comment]: # ({ed47eb68-44c45ea0})
##### 编辑选项

列表下方的按钮提供了一个批量编辑选项:

-   *Delete* - 删除脚本

要使用此选项，请在各自的脚本之前标记复选框并单击*Delete*。

[comment]: # ({/ed47eb68-44c45ea0})

[comment]: # ({13ba18fc-aacbf6a8})
##### 筛选

由于列表可能包含许多脚本，因此可能需要过滤掉您真正需要的脚本。

该 *Filter(过滤器)*
链接可用脚本列表上方。如果单击它，将提供一个过滤器，您可以在其中按名称过滤脚本。

![](../../../../../assets/zh/manual/web_interface/scripts_filter1.png){width="600"}

[comment]: # ({/13ba18fc-aacbf6a8})

[comment]: # ({43c992cd-375e8ada})
#### 配置全局脚本

![](../../../../../assets/zh/manual/web_interface/script.png)

脚本属性：

|参数                          描|说明|
|-----------------------------------|------|
|*Name*|脚本的唯一名称。<br>从Zabbix 2.2开始，该名称可以带有所需路径的前缀，例如`Default/`，将脚本放入相应目录中。通过监视部分中的菜单访问脚本时，将根据给定目录对其进行组织。<br>脚本的名称不能与现有目录相同（反之亦然）。脚本名称在其目录中必须唯一。<br>未转义的脚本名称经过唯一性验证，即“ Ping”和“ \\ Ping”不能添加到同一文件夹中。单个反斜杠会在其后直接转义任何符号。例如，字符“ /”和“ \\”可以用反斜杠（即\\ /或\\\\）转义。|
|*Type*|单击相应的按钮以选择脚本类型-[IPMI命令](/zh/manual/config/notifications/action/operation/remote_command)或脚本。|
|*Execute on*|单击相应的按钮以在以下脚本上执行脚本：<br>**Zabbix agent** - 该脚本将由Zabbix agent在主机上执行（如果允许system.run项）<br>**Zabbix server (proxy)** - 该脚本将由Zabbix server或proxy(如果启用了[enableremotecommand](/zh/manual/appendix/config/zabbix_proxy)) - 取决于主机是由server 或 proxy。<br>**Zabbix server** - 该脚本仅由Zabbix server执行|
|*Commands*|输入脚本中要执行的命令的完整路径。<br><br>支持以下宏:<br>host-related - {HOST.CONN}, {HOST.IP}, {HOST.DNS}, {HOST.HOST}, {HOST.NAME};<br>user-related - {USER.ALIAS}, {USER.FULLNAME}, {USER.NAME}, {USER.SURNAME} \\\\[用户自定义宏。](/zh/manual/config/macros/user_macros)<br>说明：<br>如果宏可能会解析为带有空格的值(例如，主机名)，不要忘记在需要时引用引号。<br>从Zabbix 5.0.2开始，就支持与用户相关的宏，以允许传递启动脚本的用户的信息。如果脚本在操作操作下自动执行，这些宏将不会被解析。|
|*Description*|输入脚本的描述。|
|*User group*|选择脚本将可用的用户组(或者对所有用户组都可用)。|
|*Host group*|选择脚本将用于的主机组(或者对所有用户组都可用)。|
|*Required host permissions*|选择主机组的权限级别—读或写。只有具有所需权限级别的用户才能访问脚本的执行。|
|*Enable confirmation*|在执行脚本之前，标记复选框以显示确认消息。对于有潜在危险的操作(如重启脚本)或可能需要很长时间的操作，此特性可能特别有用。|
|*Confirmation text*|为上面的复选框启用的确认弹出框输入自定义确认文本(例如，远程系统将重新启动)。你确定吗?)。要查看文本的样子，请单击字段旁边的 *Test confirmation*。<br><br>确认文本也支持脚本命令所支持的所有宏。<br>注意:当测试确认消息时，宏将不会被展开。|

[comment]: # ({/43c992cd-375e8ada})

[comment]: # ({af587f74-61456f7f})
#### 脚本执行和结果

Zabbix服务器运行的脚本按照[命令执行](/zh/manual/appendix/command_execution)部分中描述的顺序执行，包括退出代码检查。脚本结果将显示在运行脚本后出现的弹出窗口中。

*注意：* 脚本的返回值是标准输出以及标准错误。

请参见下面的脚本示例和结果窗口：

    uname -v
    /tmp/non_existing_script.sh
    echo "This script was started by {USER.ALIAS}"

![scripts\_result\_1.png](../../../../../assets/zh/manual/web_interface/frontend_sections/administration/scripts_result_1.png)

[comment]: # ({/af587f74-61456f7f})

[comment]: # ({96d6e598-96d6e598})
#### Script timeout

[comment]: # ({/96d6e598-96d6e598})

[comment]: # ({0a96a89d-0a96a89d})
##### Zabbix agent

You may encounter a situation when a timeout occurs while executing a
script.

See an example of a script running on Zabbix agent and the result window
below:

![](../../../../../assets/en/manual/web_interface/frontend_sections/administration/script_timeout_1.png)

The error message, in this case, is the following:

    Timeout while executing a shell script.

In order to avoid such a situation, it is advised to optimize the script
itself (instead of adjusting Timeout parameter to a corresponding value
(in our case, > ‘5’) by modifying the [Zabbix agent
configuration](/manual/appendix/config/zabbix_agentd) and [Zabbix server
configuration](/manual/appendix/config/zabbix_server)).

In case still the Timeout parameter is changed in [Zabbix agent
configuration](/manual/appendix/config/zabbix_agentd) following error
message appears:

    Get value from agent failed: ZBX_TCP_READ() timed out.

It means that modification was made in [Zabbix agent
configuration](/manual/appendix/config/zabbix_agentd) and it is required
to modify Timeout setting also in [Zabbix server
configuration](/manual/appendix/config/zabbix_server).

[comment]: # ({/0a96a89d-0a96a89d})

[comment]: # ({3e43f3fc-3e43f3fc})
##### Zabbix server/proxy

See an example of a script running on Zabbix server and the result
window below:

![](../../../../../assets/en/manual/web_interface/frontend_sections/administration/script_timeout_3.png)

It is also advised to optimize the script itself (instead of adjusting
TrapperTimeout parameter to a corresponding value (in our case, >
‘11’) by modifying the [Zabbix server
configuration](/manual/appendix/config/zabbix_server)).

[comment]: # ({/3e43f3fc-3e43f3fc})
