# 5 自定义警报提示

### 5 Custom alertscripts

#### 概述

#### Overview

如果您需更多告警媒介类型，则可以使用其它方式来执行此操作。您可以创建一个事件通知自定义的脚本。

If you are not satisfied with existing media types for sending alerts
there is an alternative way to do that. You can create a script that
will handle the notification your way.

告警脚本在Zabbix服务器上执行。这些脚本位于服务器[配置文件](/manual/appendix/config/zabbix_server)中定义的目录中**AlertScriptsPath**.

Alert scripts are executed on Zabbix server. These scripts are located
in the directory defined in the server [configuration
file](/manual/appendix/config/zabbix_server) **AlertScriptsPath**
variable.

这是一个示例警报脚本：

Here is an example alert script:

``` {.bash}
#!/bin/bash

to=$1
subject=$2
body=$3

cat <<EOF | mail -s "$subject" "$to"
$body
EOF
```

``` {.bash}
#!/bin/bash

to=$1
subject=$2
body=$3

cat <<EOF | mail -s "$subject" "$to"
$body
EOF
```

<note
important>从版本3.4开始，Zabbix检查执行的命令和脚本的退出代码。任何与\*\*
0
\*\*不同的退出代码都被视为[命令执行](/manual/appendix/command_execution)错误.
在这种情况下，Zabbix会尝试重复执行失败。
:::

::: noteimportant
Starting from version 3.4 Zabbix checks for the
exit code of the executed commands and scripts. Any exit code which is
different from **0** is considered as a [command
execution](/manual/appendix/command_execution) error. In such case
Zabbix will try to repeat failed execution.
:::

环境变量不会为脚本保留或创建，因此它们应该被明确处理。

Environment variables are not preserved or created for the script, so
they should be handled explicitly.

#### 配置

#### Configuration

将自定义告警文本配置为媒介类型：

To configure custom alertscripts as the media type:

-   进入 *管理 - >媒介类型*
-   点击*创建媒介类型*

```{=html}
<!-- -->
```
-   Go to *Administration → Media types*
-   Click on *Create media type*

The **Media type** tab contains general media type attributes:

![](../../../../../assets/en/manual/config/media_script.png)

媒介类型属性：

All mandatory input fields are marked with a red asterisk.

|参数                  说|<|
|---------------------------|-|
|*Name*|输入媒介类型的名称.|
|*Type*|选择 *Script* 作为媒介类型.|
|*Script name*|输入脚本的名称.|
|*Script parameters*|向脚本添加命令行参数.<br>{ALERT.SENDTO}, {ALERT.SUBJECT} and {ALERT.MESSAGE} [宏](/manual/appendix/macros/supported_by_location) 在脚本参数中是支持的<br>Zabbix 3.0支持自定义脚本参数.|

|Parameter|Description|
|---------|-----------|
|*Name*|Enter name of the media type.|
|*Type*|Select *Script* as the type.|
|*Script name*|Enter the name of the script.|
|*Script parameters*|Add command-line parameters to the script.<br>{ALERT.SENDTO}, {ALERT.SUBJECT} and {ALERT.MESSAGE} [macros](/manual/appendix/macros/supported_by_location) are supported in script parameters.<br>Customizing script parameters is supported since Zabbix 3.0.|

Options标签页包含了所以媒介类型常见的[告警设置](/manual/config/notifications/media/email#options)
。

The **Options** tab contains alert [processing
settings](/manual/config/notifications/media/email#options) that are
common for all media types.

::: noteimportant
从Zabbix 3.4.0版本开始实现了多个告警媒介并行处理,
所以需要注意的是，当配置了多个告警脚本时，这些脚本是可以被告警进程并行处理的.
告警进程的进程数可以通过配置项StartAlerters[parameter](/manual/appendix/config/zabbix_server)进行限制。
:::

::: noteimportant
As parallel processing of media types is
implemented since Zabbix 3.4.0, it is important to note that with more
than one script media type configured, these scripts may be processed in
parallel by alerter processes. The total number of alerter processes is
limited by the StartAlerters
[parameter](/manual/appendix/config/zabbix_server).
:::

#### 用户媒介

#### User media

为用户分配自定义告警提示符：

To assign custom alertscripts to the user:

-   进入 *管理 - >用户*
-   打开用户属性窗体
-   在媒介选项卡中，单击 *Add*

```{=html}
<!-- -->
```
-   Go to *Administration → Users*
-   Open the user properties form
-   In Media tab, click on *Add*

用户媒介属性：

User media attributes:

|参数                说|<|
|-------------------------|-|
|*Type*|选择自定义的alertcripts媒介类型.|
|*Send to*|指定收件人接收警报.|
|*When active*|您可以限制执行警示标记的时间，例如，仅限工作日（1-5,09：00-18：00）.\\\\格式说明参见[时间段规格.](/manual/appendix/time_period)页面.|
|*Use if severity*|勾选复选框(Not classified/Information/Warning/Average/High/Disaster),标识您要接收通知的触发严重性.|
|*Status*|用户媒介的状态<br>**Enabled** - 使用中.<br>**Disabled** -禁用.|

|Parameter|Description|
|---------|-----------|
|*Type*|Select the custom alertscripts media type.|
|*Send to*|Specify the recipient to receive the alerts.|
|*When active*|You can limit the time when alertscripts are executed, for example, the working days only (1-5,09:00-18:00).<br>See the [Time period specification](/manual/appendix/time_period) page for description of the format.|
|*Use if severity*|Mark the checkboxes of trigger severities that you want to activate the alertscript for.<br>*Note* that the default severity ('Not classified') **must be** checked if you want to receive notifications for non-trigger [events](/manual/config/events).<br>After saving, the selected trigger severities will be displayed in the corresponding severity colours while unselected ones will be greyed out.|
|*Status*|Status of the user media.<br>**Enabled** - is in use.<br>**Disabled** - is not being used.|
