2022 Zabbix中国峰会
2022 Zabbix中国峰会

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服务器上执行。这些脚本位于服务器配置文件中定义的目录中AlertScriptsPath.

Alert scripts are executed on Zabbix server. These scripts are located in the directory defined in the server configuration file AlertScriptsPath variable.

这是一个示例警报脚本:

Here is an example alert script:

#!/bin/bash
       
       to=$1
       subject=$2
       body=$3
       
       cat <<EOF | mail -s "$subject" "$to"
       $body
       EOF
#!/bin/bash
       
       to=$1
       subject=$2
       body=$3
       
       cat <<EOF | mail -s "$subject" "$to"
       $body
       EOF

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

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 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:

  • 进入 管理 - >媒介类型
  • 点击创建媒介类型
  • Go to Administration → Media types
  • Click on Create media type

The Media type tab contains general media type attributes:

媒介类型属性:

All mandatory input fields are marked with a red asterisk.

参数 说
Name 输入媒介类型的名称.
Type 选择 Script 作为媒介类型.
Script name 输入脚本的名称.
Script parameters 向脚本添加命令行参数.
{ALERT.SENDTO}, {ALERT.SUBJECT} and {ALERT.MESSAGE} 在脚本参数中是支持的
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.
{ALERT.SENDTO}, {ALERT.SUBJECT} and {ALERT.MESSAGE} macros are supported in script parameters.
Customizing script parameters is supported since Zabbix 3.0.

Options标签页包含了所以媒介类型常见的告警设置

The Options tab contains alert processing settings that are common for all media types.

从Zabbix 3.4.0版本开始实现了多个告警媒介并行处理, 所以需要注意的是,当配置了多个告警脚本时,这些脚本是可以被告警进程并行处理的. 告警进程的进程数可以通过配置项StartAlertersparameter进行限制。

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.

用户媒介

User media

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

To assign custom alertscripts to the user:

  • 进入 管理 - >用户
  • 打开用户属性窗体
  • 在媒介选项卡中,单击 Add
  • 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).\\格式说明参见时间段规格.页面.
Use if severity 勾选复选框(Not classified/Information/Warning/Average/High/Disaster),标识您要接收通知的触发严重性.
Status 用户媒介的状态
Enabled - 使用中.
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).
See the Time period specification page for description of the format.
Use if severity Mark the checkboxes of trigger severities that you want to activate the alertscript for.
Note that the default severity ('Not classified') must be checked if you want to receive notifications for non-trigger events.
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.
Enabled - is in use.
Disabled - is not being used.