Ubuntu 22.04 安装 Thruk 3.30 指南

在 Ubuntu 22.04 服务器从零部署 Thruk 3.30 监控 Web 界面的完整实录:OBS 仓库在线安装与离线 deb 双方案、Apache/FastCGI 集成、Naemon+Livestatus 后端 peer 配置、socket 权限修复,含 Apache 2.4.52 cookie 认证全站 403(UnsafeAllow3F)排障实录。

ThrukThruk自托管监控2026/11/01

Ubuntu 22.04 安装 Thruk 3.30 指南

目标:在一台 Ubuntu 22.04 服务器上从零部署 Thruk 3.30 监控 Web 界面,并挂载 Naemon 1.5.2 监控核心与 Livestatus 后端,形成完整可用的监控图形化入口。全文覆盖官方 OBS 仓库在线安装与离线 deb 包安装两种方案、Apache/FastCGI 集成、后端 peer 配置、登录验证,并沉淀 Apache 2.4.52 与 Thruk cookie 认证兼容性问题(全站 403)的完整排障实录。全部命令与回显均在 Ubuntu 22.04.5 LTS 服务器上逐条执行采集。

一、环境信息

  • 操作系统:Ubuntu 22.04.5 LTS(PRETTY_NAME="Ubuntu 22.04.5 LTS")
  • 架构:amd64(x86_64)
  • 目标软件:Thruk 3.30-1(OBS home:naemon 仓库,2026-10-22 发布的最新稳定版)
  • 配套软件:naemon-core 1.5.2-1、naemon-livestatus 1.5.1-1、libnaemon 1.5.2-1、Apache 2.4.52(Ubuntu 官方源)
  • Web 服务:Apache 2(mod_fcgid + FastCGI),站点端口 8141
  • 主配置目录:/etc/thruk/;数据目录:/var/lib/thruk/;缓存目录:/var/cache/thruk/
  • 包管理:apt(deb);服务管理:systemd(Web 进程由 apache2.service 承担)
  • 查询日期:2026-11-01(版本线以 GitHub Releases 与 OBS 仓库目录实查为准)

二、前置条件

  1. 一台 Ubuntu 22.04 服务器(物理机或虚拟机),具备 root 权限;
  2. 服务器时间正常(监控状态时间戳依赖系统时钟);
  3. 方案 A(在线仓库)需可访问 download.opensuse.org 与 build.opensuse.org;方案 B(离线包)仅需一台能联网的下载机;
  4. 基础工具齐备:
apt-get update
apt-get install -y curl gnupg lsb-release wget apache2
  1. 确认 systemd 与 Apache 可用:
systemctl is-active apache2 && apache2 -v

实测回显:

active
Server version: Apache/2.4.52 (Ubuntu)
Server built:   2026-10-08T19:59:46

三、版本选型说明

版本信息实查于 2026-11-01:

包名最新版本来源
thruk / thruk-base / libthruk3.30-1OBS home:naemon 仓库 xUbuntu_22.04 目录
thruk-plugin-reporting3.30-1同上
naemon-core / libnaemon1.5.2-1同上
naemon-livestatus1.5.1-1同上

要点说明:

  • 最新稳定版是 3.30:GitHub sni/thruk releases.atom 与源码 Changes.txt 均确认 v3.30 于 2026-10-22 发布;但官网首页与下载页截至查询日仍停留在 v3.28(2026-04-09 发布),thruk.org/pkg/v3.30/ 目录甚至尚未同步(404)。deb/rpm 二进制仓库(OBS)已同步 3.30-1,应优先以 OBS 仓库与 GitHub 为准。
  • Thruk 无法独立使用:它是 Web 前端,必须搭配支持 Livestatus 的监控核心。本实验选择 Naemon(官方默认 GUI 即 Thruk),一次性安装 thruk naemon-core naemon-livestatus monitoring-plugins apache2 组合。
  • Ubuntu 22.04 的 Apache 是 2.4.52:该版本收紧了重写规则安全策略,与 Thruk 的 cookie 认证 include 存在兼容性问题,第 6.1 节给出完整排障实录。

四、方案 A:OBS 在线仓库安装(推荐)

4.1 导入仓库签名密钥

wget -qO /etc/apt/trusted.gpg.d/thruk.gpg https://download.opensuse.org/repositories/home:/naemon/xUbuntu_22.04/Release.key

4.2 添加 apt 源

echo 'deb https://download.opensuse.org/repositories/home:/naemon/xUbuntu_22.04/ /' > /etc/apt/sources.list.d/thruk.list
apt-get update

实测回显(源内 thruk 候选版本):

thruk:
  Installed: 3.30-1
  Candidate: 3.30-1
  Version table:
 *** 3.30-1 500
        500 https://download.opensuse.org/repositories/home:/naemon/xUbuntu_22.04  Packages
        100 /var/lib/dpkg/status

4.3 安装 Thruk + Naemon 核心 + Livestatus + 插件集 + Apache

apt-get install -y thruk naemon-core naemon-livestatus monitoring-plugins apache2

安装耗时较长(依赖链含 perl 全套),完成后用 dpkg 核对版本:

dpkg -l | grep -E "thruk|naemon" | awk '{print $2, $3}'

实测回显:

libnaemon:amd64 1.5.2-1
libthruk 3.30-1
naemon-core 1.5.2-1
naemon-livestatus 1.5.1-1
thruk 3.30-1
thruk-base 3.30-1
thruk-plugin-reporting 3.30-1

4.4 安装器自动完成的集成

thruk-base 的 postinst 脚本会自动:

  1. 生成 /etc/apache2/conf-available/thruk.conf(mod_fcgid 的 Alias/Location 配置)并启用;
  2. 将 Include /usr/share/thruk/thruk_cookie_auth.include 写入 000-default.conf(第 30 行),启用 cookie 认证;
  3. 创建默认 Web 账号 thrukadmin(初始密码同为 thrukadmin,见 postinst 脚本第 51 行),密码文件为 /etc/thruk/htpasswd。

许可证确认:

grep -m3 -iE 'license|GPL' /usr/share/doc/thruk-base/copyright

实测回显(GPL-2.0):

License:
   it under the terms of the GNU General Public License version 2 as
   GNU General Public License for more details.

五、方案 B:离线 deb 包安装

适合隔离网络环境。在有网机器上从 OBS 仓库目录下载全套 deb:

BASE=http://download.opensuse.org/repositories/home:/naemon/xUbuntu_22.04/amd64
for p in thruk_3.30-1 thruk-base_3.30-1 libthruk_3.30-1 \
         naemon-core_1.5.2-1 libnaemon_1.5.2-1 naemon-livestatus_1.5.1-1 \
         thruk-plugin-reporting_3.30-1; do
  wget -q "$BASE/${p}_amd64.deb"
done

拷贝到目标服务器后一次性安装(apt 自动处理依赖顺序):

dpkg -i ./*.deb || apt-get install -f

⚠️ 注意:离线方式仍需 Ubuntu 官方源的 apache2、libapache2-mod-fcgid、perl 依赖等基础包,建议先在有网机器上备齐或配置本地镜像。

六、配置后端:让 Thruk 连上 Livestatus

6.1 启用 Naemon 的 Livestatus 模块

确认 /etc/naemon/module-conf.d/livestatus.cfg 存在且内容如下:

broker_module=/usr/lib/naemon/naemon-livestatus/livestatus.so /var/cache/naemon/live

6.2 在 Thruk 中注册后端 peer

编辑 /etc/thruk/thruk_local.conf(该文件是官方指定的本地覆盖文件,主文件 thruk.conf 标注 "DONT CHANGE THIS FILE",升级不会被覆盖)。注意 Thruk 配置采用 XML 式嵌套段而非 INI:

<Component Thruk::Backend>
    <peer>
        name   = naemon
        type   = livestatus
        <options>
            peer          = /var/cache/naemon/live
            resource_file = /etc/naemon/resource.cfg
        </options>
        <configtool>
            core_conf      = /etc/naemon/naemon.cfg
            obj_check_cmd  = /usr/bin/naemon -v /etc/naemon/naemon.cfg
            obj_reload_cmd = systemctl reload naemon
        </configtool>
    </peer>
</Component>

⚠️ 两个实测踩坑:

  1. 用 INI 风格写 [Backend] 段会直接报错 unknown config entry: [Backend](Config.pm 解析器只认 XML 式段);
  2. 闭合标签必须写 </Component>,写成 </Component Thruk::Backend> 会报 unexpected closing block found。

6.3 校验后端连通性

thruk backend list

实测回显(首次运行时 Status 为 491: failed to connect - Permission denied,原因与修复见 6.4):

+--------+---------+-------+---------+------------------------+--------------------------------+--------+
| Name   | Section | Key   | Enabled | Address                | Version                        | Status |
+--------+---------+-------+---------+------------------------+--------------------------------+--------+
| naemon | Default | 2918f | Yes     | /var/cache/naemon/live | Livestatus 1.5.1.source-naemon | OK     |
+--------+---------+-------+---------+------------------------+--------------------------------+--------+

6.4 修复 socket 权限(关键步骤)

Livestatus socket 属主属组为 naemon:naemon、权限 srw-rw----,而 Apache 以 www-data 身份运行,默认无权访问:

ls -l /var/cache/naemon/live
srw-rw---- 1 naemon naemon 0 Nov  1 09:08 /var/cache/naemon/live

将 www-data 加入 naemon 组并重启 Apache:

usermod -aG naemon www-data
id www-data
systemctl restart apache2

实测回显:

uid=33(www-data) gid=33(www-data) groups=33(www-data),108(naemon)

再次 thruk backend list,Status 变为 OK 即后端打通。

七、验证与常见问题

7.1 排障实录:Apache 2.4.52 全站 403(UnsafeAllow3F)

现象:安装完成、后端配置正确,但浏览器访问 http://<服务器IP>:8141/thruk/ 任意页面均返回 403 Forbidden。

定位过程:临时开启 rewrite 调试日志:

sed -i '143s/^LogLevel warn/LogLevel warn rewrite:trace4/' /etc/apache2/apache2.conf
systemctl restart apache2
tail /var/log/apache2/error.log

错误日志关键行(实测原样):

[Sun Nov 01 09:09:11.128940 2026] [rewrite:error] [pid 4540:tid 127316714985024] [client 10.89.61.1:39376] AH: Unsafe URL with %3f URL rewritten without UnsafeAllow3F

根因:Thruk 的 cookie 认证通过 RewriteMap thruk_users prg:/usr/share/thruk/thruk_auth 实现,重写产物 URL 中含编码后的 ?(%3f)。Apache 2.4.46 起安全策略收紧,此类重写必须显式加 UnsafeAllow3F 标志,否则直接拒绝请求。

修复:编辑 /usr/share/thruk/thruk_cookie_auth.include 第 14 行,给标志链追加 UnsafeAllow3F:

# 修改前
RewriteRule ^(.*)$                   ${thruk_users:$1|/loginbad/} [C,NS]
# 修改后
RewriteRule ^(.*)$                   ${thruk_users:$1|/loginbad/} [C,NS,UnsafeAllow3F]
systemctl restart apache2

修复后访问 /thruk/ 返回 302 跳转登录页,全站恢复。排障完成后记得还原 LogLevel 调试配置。由于该文件由 deb 包管理,升级 thruk 包后需重新打此补丁(或改用 thruk_local.conf 中 auth = internal 并关闭 cookie-auth include 的方案)。

7.2 登录验证与口令整改

安装器生成的默认账号为 thrukadmin / thrukadmin(非 thruk,见 postinst 脚本)。用 curl 验证:

curl -s -u thrukadmin:thrukadmin -o /dev/null -w "%{http_code}\n" \
  http://localhost:8141/thruk/cgi-bin/restricted.cgi

实测回显:200

抓取状态总览页:

curl -s -u thrukadmin:thrukadmin http://localhost:8141/thruk/cgi-bin/status.cgi | grep -o '<title>[^<]*</title>'

实测回显:

<title>Current Network Status</title>

浏览器访问 http://<服务器IP>:8141/thruk/,用 thrukadmin/thrukadmin 登录后即可看到 Naemon 主机/服务状态页。默认口令必须立即整改(演示环境统一改为 thrukadmin / q1w2e3r4):

htpasswd -b -B /etc/thruk/htpasswd thrukadmin q1w2e3r4

实测回显:

Updating password for user thrukadmin

整改后新旧口令验证(实测):新口令访问 restricted.cgi 返回 200,旧口令返回 401。

7.3 服务自检查

thruk selfcheck

实测回显:

OK - backends, filesystem, logfiles, recurring_downtimes, reports
Backends:
  - no errors in 1 backends

Filesystem:
  - var path /var/lib/thruk is writable
  - tmp path /var/cache/thruk is writable

Logfiles:
  - /var/lib/thruk/cron.log: no errors

Recurring Downtimes:
  - no errors in 0 downtimes

Reports:
  - no errors in 0 reports

该命令输出即 Nagios 插件格式,可让 Thruk 监控自身。

7.4 其他常见问题

  • thruk --version 与包版本不一致:以 thruk --version 为准(实测输出 thruk v3.30);
  • 404 访问 /thruk/status.cgi:正确路径是 /thruk/cgi-bin/status.cgi,根路径短 URL 依赖 cookie-auth rewrite 正常(见 7.1);
  • 登录 POST 被弹回登录页:确认浏览器已允许 cookie,Thruk 登录流程含 nocookie 探测跳转。

八、卸载清理

apt-get remove --purge thruk thruk-base libthruk thruk-plugin-reporting
apt-get remove --purge naemon-core naemon-livestatus libnaemon
rm -f /etc/apt/sources.list.d/thruk.list /etc/apt/trusted.gpg.d/thruk.gpg
rm -rf /etc/thruk /var/lib/thruk /var/cache/thruk
systemctl restart apache2

九、参考链接

  1. Thruk 官网下载页(deb/rpm 包与源码):https://www.thruk.org/download.html (查询日期 2026-11-01)
  2. Thruk GitHub Releases(v3.30,2026-10-22):https://github.com/sni/thruk/releases (查询日期 2026-11-01)
  3. OBS home:naemon 仓库 xUbuntu_22.04 目录(3.30-1 deb 包):http://download.opensuse.org/repositories/home:/naemon/xUbuntu_22.04/ (查询日期 2026-11-01)
  4. Naemon 官方下载页(Thruk 为默认 GUI 的说明):https://www.naemon.io/download (查询日期 2026-11-01)
  5. 站内关联:《Naemon 介绍与选型指南》、《Ubuntu 22.04 安装 Naemon 1.5 指南》
Ubuntu 22.04 安装 Thruk 3.30 指南 - Quzuo