> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flashduty.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Nagios 告警事件

> 通过 webhook 的方式同步 Nagios 告警事件到 Flashduty

通过 webhook 的方式同步 Nagios 告警事件到 Flashduty。

## 在 Flashduty On-call

您可通过以下两种方式获取集成推送地址，任选其一即可。

### 使用专属集成

如果您只需将告警发往一个协作空间，选择此方式，配置更简单。

> [accordion] 查看专属集成的配置步骤
>
> 1. 进入 Flashduty 控制台，选择 **协作空间**，打开一个协作空间
> 2. 选择 **配置** → **集成数据** → **专属集成**，点击 **新增集成**
> 3. 选择 **Nagios** 集成，点击 **保存**，生成卡片。
> 4. 打开生成的集成卡片，复制 **推送地址**

### 使用共享集成

如果您需要根据告警事件的 Payload 将告警路由到不同协作空间，选择此方式。

> [accordion] 查看共享集成的配置步骤
>
> 1. 进入 Flashduty 控制台，选择 **集成中心 → 告警事件**
> 2. 选择 **Nagios** 集成：
>    * **集成名称**：为当前集成定义一个名称。
> 3. 配置默认路由并选择协作空间；创建后可在 **路由** 中增加更多规则
> 4. 点击 **保存**，复制生成的 **推送地址**

## 在 Nagios 中配置

不同的系统和安装方式，Nagios 的安装路径可能不同，请根据实际情况调整以下配置中的路径。

**1. 在 Nagios Server 上下载通知脚本**

**操作路径**：Nagios Server 所在主机的终端，Nagios 插件目录

下载地址要求请求携带 `Referer` 头，缺少时返回 403，请保留命令中的 `--header` 参数。

> [tab] Debian/Ubuntu
>
> 目录通常为 `/usr/lib/nagios/plugins/`：
>
> ```bash theme={null}
> cd /usr/lib/nagios/plugins/
> wget --header="Referer: https://console.flashcat.cloud" https://download.flashcat.cloud/flashduty/integration/nagios/send_to_flashduty.sh
> chmod +x send_to_flashduty.sh
> ```

> [tab] RHEL/CentOS
>
> 目录通常为 `/usr/lib64/nagios/plugins/`：
>
> ```bash theme={null}
> cd /usr/lib64/nagios/plugins/
> wget --header="Referer: https://console.flashcat.cloud" https://download.flashcat.cloud/flashduty/integration/nagios/send_to_flashduty.sh
> chmod +x send_to_flashduty.sh
> ```

> [tab] 源码安装
>
> 目录通常为 `/usr/local/nagios/libexec/`：
>
> ```bash theme={null}
> cd /usr/local/nagios/libexec/
> wget --header="Referer: https://console.flashcat.cloud" https://download.flashcat.cloud/flashduty/integration/nagios/send_to_flashduty.sh
> chmod +x send_to_flashduty.sh
> ```

> [!NOTE]
>
> 脚本使用 `curl` 命令，请确保 Nagios Server 上已安装 curl；脚本依赖 bash 4 及以上版本（使用了 `declare -A`）。

**2. 下载并修改 flashduty.cfg**

**操作路径**：Nagios Server 所在主机的终端，Nagios 配置目录 › `flashduty.cfg`

> [tab] Debian/Ubuntu
>
> 目录通常为 `/etc/nagios3/conf.d/`：
>
> ```bash theme={null}
> cd /etc/nagios3/conf.d/
> wget --header="Referer: https://console.flashcat.cloud" https://download.flashcat.cloud/flashduty/integration/nagios/flashduty.cfg
> ```

> [tab] RHEL/CentOS
>
> 目录通常为 `/etc/nagios/objects/`：
>
> ```bash theme={null}
> cd /etc/nagios/objects/
> wget --header="Referer: https://console.flashcat.cloud" https://download.flashcat.cloud/flashduty/integration/nagios/flashduty.cfg
> ```

> [tab] 源码安装
>
> 目录通常为 `/usr/local/nagios/etc/objects/`：
>
> ```bash theme={null}
> cd /usr/local/nagios/etc/objects/
> wget --header="Referer: https://console.flashcat.cloud" https://download.flashcat.cloud/flashduty/integration/nagios/flashduty.cfg
> ```

编辑下载的 `flashduty.cfg`：

| 位置 | 修改 |
| - | - |
| `pager`（必填） | 把 `<YOUR_FLASHDUTY_PUSH_URL>` 替换为 `{integration_url}` |
| 两处 `command_line`（按需） | 文件中写死了 `/usr/local/nagios/libexec/send_to_flashduty.sh`；脚本放在其他目录时，把两处 `command_line` 里的路径都改成实际位置 |

配置文件内容示例：

```
define contact {
    contact_name                    Flashduty
    alias                           Flashduty Alert Receiver
    service_notification_commands   notify-service-by-Flashduty
    host_notification_commands      notify-host-by-Flashduty
    service_notification_options    w,u,c,r
    host_notification_options       d,u,r
    service_notification_period     24x7
    host_notification_period        24x7
    pager                           {integration_url}
}

define command {
    command_name    notify-host-by-Flashduty
    command_line    /usr/local/nagios/libexec/send_to_flashduty.sh type=HOST WEBHOOK_URL="$CONTACTPAGER$" hostname="$HOSTNAME$" state="$HOSTSTATE$" output="$HOSTOUTPUT$" notification_type="$NOTIFICATIONTYPE$" time="$LONGDATETIME$" host_address="$HOSTADDRESS$" host_alias="$HOSTALIAS$" check_command="$HOSTCHECKCOMMAND$"
}

define command {
    command_name    notify-service-by-Flashduty
    command_line    /usr/local/nagios/libexec/send_to_flashduty.sh type=SERVICE WEBHOOK_URL="$CONTACTPAGER$" hostname="$HOSTNAME$" state="$SERVICESTATE$" output="$SERVICEOUTPUT$" notification_type="$NOTIFICATIONTYPE$" time="$LONGDATETIME$" host_address="$HOSTADDRESS$" service_desc="$SERVICEDESC$" host_alias="$HOSTALIAS$" max_attempts="$MAXSERVICEATTEMPTS$"
}
```

参数说明：

* `pager`：Flashduty 推送地址，即您在 Flashduty 控制台获取的集成推送地址
* `command_line`：脚本路径默认是 `/usr/local/nagios/libexec/send_to_flashduty.sh`，与实际位置不同时请修改
* `service_notification_options`：服务告警通知选项，w=警告，u=未知，c=严重，r=恢复
* `host_notification_options`：主机告警通知选项，d=宕机，u=不可达，r=恢复

`r`（恢复）必须保留，Flashduty 靠恢复通知关闭告警。

> [!TIP]
>
> 如需在告警中携带更多信息，可以在 `command_line` 末尾按照 `key=value` 格式追加参数，例如：`environment="production" region="$_HOSTREGION$"`。这些参数将作为标签（labels）推送到 Flashduty。

**3. 引入配置文件并加入联系人组**

**操作路径**：`nagios.cfg` 与 `contacts.cfg`

先在 Nagios 主配置文件中引入 Flashduty 配置文件：

> [tab] Debian/Ubuntu
>
> Debian/Ubuntu 通常会自动加载 `/etc/nagios3/conf.d/` 目录下的所有配置文件，无需手动引入。

> [tab] RHEL/CentOS
>
> 编辑 `/etc/nagios/nagios.cfg`，添加：
>
> ```
> cfg_file=/etc/nagios/objects/flashduty.cfg
> ```

> [tab] 源码安装
>
> 编辑 `/usr/local/nagios/etc/nagios.cfg`，添加：
>
> ```
> cfg_file=/usr/local/nagios/etc/objects/flashduty.cfg
> ```

再编辑联系人配置文件，将 Flashduty 联系人添加到 `admins` 联系人组（或您使用的其他联系人组）：

> [tab] Debian/Ubuntu
>
> `/etc/nagios3/conf.d/contacts_nagios2.cfg`

> [tab] RHEL/CentOS
>
> `/etc/nagios/objects/contacts.cfg`

> [tab] 源码安装
>
> `/usr/local/nagios/etc/objects/contacts.cfg`

找到联系人组定义，将 Flashduty 添加到成员列表：

```
define contactgroup {
    contactgroup_name       admins
    alias                   Nagios Administrators
    members                 nagiosadmin,Flashduty
}
```

**4. 验证配置、重启并确认**

**操作路径**：Nagios Server 所在主机的终端

先验证配置文件，通过后重启 Nagios 服务：

> [tab] Debian/Ubuntu
>
> ```bash theme={null}
> /usr/sbin/nagios3 -v /etc/nagios3/nagios.cfg
> # 验证通过后重启
> systemctl restart nagios3
> ```

> [tab] RHEL/CentOS
>
> ```bash theme={null}
> /usr/sbin/nagios -v /etc/nagios/nagios.cfg
> # 验证通过后重启
> systemctl restart nagios
> ```

> [tab] 源码安装
>
> ```bash theme={null}
> /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
> # 验证通过后重启
> systemctl restart nagios
> ```

重启后，Nagios 检测到告警时会自动推送到 Flashduty。要立即验证，可在脚本所在目录手动运行脚本，先发一条严重告警，再发一条恢复：

```bash theme={null}
./send_to_flashduty.sh type=SERVICE WEBHOOK_URL="{integration_url}" hostname="test-host" state="CRITICAL" output="Flashduty test" service_desc="test-service"
./send_to_flashduty.sh type=SERVICE WEBHOOK_URL="{integration_url}" hostname="test-host" state="OK" output="Flashduty test" service_desc="test-service"
```

脚本会丢弃 curl 的输出，推送失败时没有任何提示；如果 Flashduty 没有收到，请先在该主机上用 `curl` 确认能访问推送地址。几秒后回到 Flashduty，集成显示 **最新事件时间**，说明配置成功。

## 状态和告警等级

Nagios 到 Flashduty 告警等级映射关系：

| Nagios | Flashduty |
| - | - |
| CRITICAL | Critical |
| DOWN | Critical |
| UNREACHABLE | Critical |
| WARNING | Warning |
| OK | Ok |
| UP | Ok |
| UNKNOWN | Info |

其他取值按 Info 处理。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.