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

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

[comment]: # ({e233f5a6-cbce69fe})
#### 概述

在 Administration → Scripts 中，可以配置和维护用户定义的全局脚本。

这些脚本取决于设置的用户权限，之后可以通过单击主机上各个前端位置(Dashboard,
Problems, Latest data, Status of triggers,
Maps)便可执行，同时也可以用作行动操作来运行。脚本在Zabbix
sever或agent上执行。 显示现有脚本及其详细信息的列表

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

显示的数据：

|列|述|
|---|---|
|*Name*|脚本名。点击该脚本名打开脚本 [配置表格.](scripts#configuring_a_global_script).|
|*Type*|显示脚本类型- Script 或者 IPMI 命令|
|*Execute on*|显示脚本执行在Zabbix sever或者agent上。|
|*Commands*|显示在脚本中执行的所有命令。|
|*User group*|显示该脚本可用的用户组(或者 All 针对所有用户组)。|
|*Host group*|将显示该脚本可用的主机组(或者 All 针对所有主机组)。|
|*Host access*|显示主机组的权限级别 Read 或者 Write. 只有具备所需权限级别的用户才能访问执行脚本。|

配置新的脚本，请单击顶部右上角的 Create script 按键。

[comment]: # ({/e233f5a6-cbce69fe})

[comment]: # ({05c6ead5-44c45ea0})
##### 批量编辑选项

列表下面的按键会提供一些批量编辑选项：

-   *Delete* -删除脚本

要使用这个选项，请在各个脚本之前标记复选框，然后单击 Delete.

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

[comment]: # ({420f8704-ecfde338})
##### 过滤器

因为列表中可能包含许多脚本，所以可能需要通过过滤得到您需要的内容。

Filter过滤器 链接位于脚本列表之上。
如果您点击它，则可以使用过滤器，您可以通过名称过滤媒脚本。

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

[comment]: # ({/420f8704-ecfde338})

[comment]: # ({7961cf10-8d5ec606})
#### 配置全局脚本

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

脚本属性：

|参数                          描|<|
|-----------------------------------|-|
|*Name*|脚本的唯一名称。\\ 从Zabbix 2.2起，名称可以以所需的路径为前缀，例如 Default/，将脚本放入相应的目录。 通过监控部分中的菜单访问脚本时，将根据给定的目录进行组织。<br>脚本不能与现有目录名称相同（反之亦然）。 脚本名称在其目录中必须是唯一的。<br>未转义的脚本名称具有唯一性验证，即“Ping”和“\\ Ping”无法添加到同一文件夹中。 单个反斜杠在其后直接转义任何符号。 例如，字符'/'和'\\'可以用反斜杠转义，即\\ /或\\\\。|
|*Type*|点击相应的按键，来选择脚本类型 - [IPMI command](/manual/config/notifications/action/operation/remote_command#ipmi_remote_commands)或者 Script.|
|*Execute on*|单击对应的按键来在Zabbix server或agent上执行脚本。<br>从Zabbix 2.0版本起，（在Zabbix agent[配置文件](/manual/appendix/config/zabbix_agentd)中的EnableRemoteCommands参数中启用远程命令），可以使用Zabbix agent 执行脚本的选项。|
|*Commands*|输入脚本执行命令的完整路径。<br>命令中支持以下宏： {HOST.CONN}, {HOST.IP}, {HOST.DNS}, {HOST.HOST}, {HOST.NAME}.如果宏可能解析为具有空格的值(例如, host name)，不要忘记使用引号。<br>从Zabbix 2.2起，脚本命令开始支持[用户宏](/manual/config/macros/usermacros) 。|
|*Description*|为脚本添加描述|
|*User group*|选择脚本可用的用户组 ( All 是对所有的用户组)。|
|*Host group*|选择脚本可用的主机组 ( All 是对所有主机组)。|
|*Required host permissions*|选择主机组的权限级别 -- Read 或 Write。 只有具有所需权限级别的用户才能访问执行脚本。|
|*Enable confirmation*|在执行脚本之前选中复选框以显示确认消息。 对于潜在的危险操作（如重新启动脚本）可能需要很长的操作时间，此功能因此会特别有用。|
|*Confirmation text*|使用复选框，输入确认弹出窗口的自定义确认文本 (例如， Remote system远程系统 将要重启，您确定吗？). 要查看文字的效果，请点击该字段旁边的 Test confirmation 。<br>从Zabbix 2.2起, 确认文本讲扩展到主机名宏 - {HOST.HOST}, {HOST.NAME}, 主机连接宏 - {HOST.IP}, {HOST.DNS}, {HOST.CONN} 和用户宏。 Note注意: 测试确认消息时，宏不会被扩展。|

[comment]: # ({/7961cf10-8d5ec606})

[comment]: # ({3a1b5e21-5bc3bc84})
#### 脚本的执行和结果

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

*Note:*脚本的返回值是标准输出以及标准错误。

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

    uname
    uname --non-existing-flag
    /tmp/non_existing_script.sh

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

[comment]: # ({/3a1b5e21-5bc3bc84})

[comment]: # ({bcbf8351-96d6e598})
### 7 Scripts

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

[comment]: # ({ad9b5651-9ebab623})
#### Overview

In the *Administration → Scripts* section user-defined global scripts
can be configured and maintained.

These scripts, depending on the set user permissions, then become
available for execution by clicking on the host in various frontend
locations (*Dashboard*, *Problems*, *Latest data*, *Maps*) and can also
be run as an action operation. The scripts are executed on the Zabbix
server or agent.

A listing of existing scripts with their details is displayed.

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

Displayed data:

|Column|Description|
|------|-----------|
|*Name*|Name of the script. Clicking on the script name opens the script [configuration form](scripts#configuring_a_global_script).|
|*Type*|Script type is displayed - *Script* or *IPMI* command.|
|*Execute on*|It is displayed whether the script will be executed on Zabbix server or agent.|
|*Commands*|All commands to be executed within the script are displayed.|
|*User group*|The user group that the script is available to is displayed (or *All* for all user groups).|
|*Host group*|The host group that the script is available for is displayed (or *All* for all host groups).|
|*Host access*|The permission level for the host group is displayed - *Read* or *Write*. Only users with the required permission level will have access to executing the script.|

To configure a new script, click on the *Create script* button in the
top right-hand corner.

[comment]: # ({/ad9b5651-9ebab623})

[comment]: # ({80b1aa53-465ca050})
##### Mass editing options

A button below the list offers one mass-editing option:

-   *Delete* - delete the scripts

To use this option, mark the checkboxes before the respective scripts
and click on *Delete*.

##### Filter

As the list may contain a number of scripts, it may be needed to filter
out the ones you really need.

The *Filter* link is available above the list of scripts. If you click
on it, a filter becomes available where you can filter scripts by name.

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

#### Configuring a global script

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

Script attributes:

|Parameter|Description|
|---------|-----------|
|*Name*|Unique name of the script.<br>Since Zabbix 2.2 the name can be prefixed with the desired path, for example, `Default/`, putting the script into the respective directory. When accessing scripts through the menu in monitoring sections, they will be organized according to the given directories.<br>A script cannot have the same name as an existing directory (and vice versa). A script name must be unique within its directory.<br>Unescaped script names are validated for uniqueness, i.e. "Ping" and "\\Ping" cannot be added in the same folder. A single backslash escapes any symbol directly after it. For example, characters '/' and '\\' can be escaped by backslash, i.e. \\/ or \\\\.|
|*Type*|Click the respective button to select script type - [IPMI command](/manual/config/notifications/action/operation/remote_command#ipmi_remote_commands) or Script.|
|*Execute on*|Click the respective button to execute the script on:<br>**Zabbix agent** - the script will be executed by Zabbix agent on the host<br>**Zabbix server (proxy)** - the script will be executed by Zabbix server or proxy - depending on whether the host is monitored by server or proxy<br>**Zabbix server** - the script will be executed by Zabbix server only<br>The option to execute scripts on Zabbix agent is available since Zabbix 2.0 version (providing remote commands are enabled in the EnableRemoteCommands parameter in Zabbix agent [configuration file](/manual/appendix/config/zabbix_agentd)).|
|*Commands*|Enter full path to the commands to be executed within the script.<br>The following macros are supported in the commands: {HOST.CONN}, {HOST.IP}, {HOST.DNS}, {HOST.HOST}, {HOST.NAME}. If a macro may resolve to a value with spaces (for example, host name), don't forget to quote as needed.<br>Since Zabbix 2.2, [user macros](/manual/config/macros/usermacros) are supported in script commands.|
|*Description*|Enter a description for the script.|
|*User group*|Select the user group that the script will be available to (or *All* for all user groups).|
|*Host group*|Select the host group that the script will be available for (or *All* for all host groups).|
|*Required host permissions*|Select the permission level for the host group - *Read* or *Write*. Only users with the required permission level will have access to executing the script.|
|*Enable confirmation*|Mark the checkbox to display a confirmation message before executing the script. This feature might be especially useful with potentially dangerous operations (like a reboot script) or ones that might take a long time.|
|*Confirmation text*|Enter a custom confirmation text for the confirmation popup enabled with the checkbox above (for example, *Remote system will be rebooted. Are you sure?*). To see how the text will look like, click on *Test confirmation* next to the field.<br>Since Zabbix 2.2, the confirmation text will expand host name macros - {HOST.HOST}, {HOST.NAME}, host connection macros - {HOST.IP}, {HOST.DNS}, {HOST.CONN} and user macros. *Note:* The macros will not be expanded when testing the confirmation message.|

#### Script execution and result

Scripts run by Zabbix server are executed by the order described in
[Command execution](/manual/appendix/command_execution) section
including exit code checking. The script result will be displayed in a
pop-up window that will appear after the script is run.

*Note:* The return value of the script is standard output together with
standard error.

See example of a script and the result window below:

    uname
    uname --non-existing-flag
    /tmp/non_existing_script.sh

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

#### Script timeout

##### Zabbix agent

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

See 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)

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).

##### Zabbix server/proxy

See 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]: # ({/80b1aa53-465ca050})
