如何在 thruk_local.conf 中添加第二个 Core 的 Livestatus 路径?

解读

国内中大型 Nagios 集群普遍采用 多核心(Core)分布式架构,每个核心运行独立的 Naemon/Nagios 进程与 Livestatus 插件。Thruk 作为统一前端,需要把所有核心的 MK Livestatus socket 聚合到同一界面,才能做跨机房、跨网络的集中告警与仪表盘展示。面试时,考官不仅想知道你会不会改配置,更关注你对 Livestatus 通信机制、权限模型、性能影响、故障隔离 的理解。回答必须体现“可落地、可回滚、可审计”的运维思路。

知识点

  1. Livestatus 原理:Unix Domain Socket 或 TCP 封装 Nagios 内部状态,查询速度比数据库快 10 倍以上,支持 NagiosQL、 thruk、Dashing 等前端。
  2. thruk_local.conf 加载顺序:thruk 启动时先读 thruk.conf,再读 etc/conf.d/ 下的 thruk_local.conf,后者优先级高,避免直接修改主配置文件,保证 yum/apt 升级不丢定制。
  3. <Component> 段:每个 Core 对应一个独立 <Component> 块,name 必须全局唯一,backend 类型选 “livestatus”,<options> 里填 socket 路径或 tcp 地址。
  4. 权限与 SELinux:socket 文件需 thruk 用户可读(一般属主 nagios:nagios 模式 0640),TCP 端口要在 firewalld/iptables 白名单 内;若启 SELinux,需打 setsebool -p httpd_can_network_connect 1。
  5. 高可用细节:生产环境建议 TCP over TLS,并在 <options> 里加 tls = 1 与 verify = 1,证书走公司内网 CA,防止跨机房明文传输。
  6. 性能调优:每增加一个 Core,thruk 启动时会并发拉取全部状态,建议 max_parallel_queries ≤ 8,否则容易触发 Livestatus 的 “MaxConcurrentChecks” 限制导致 502 错误。
  7. 故障隔离:通过 disabled = 1 可临时下线单核心,方便做 蓝绿升级;同时配置 retry_interval = 30s 避免网络抖动频繁报警。

答案

  1. 登录 thruk 服务器,切换至配置目录
    cd /etc/thruk && cp thruk_local.conf thruk_local.conf.$(date +%F)
  2. 在 thruk_local.conf 末尾追加第二个 Core 的 <Component> 段,确保 name 与第一个不重复:
    <Component>
        name = core_bj_dx
        backend = livestatus
        <options>
            peer = 10.45.12.33:6557
            # 若用本地 socket 可写 /var/lib/naemon2/livestatus
            timeout = 15
            tls = 1
            verify = 1
            ca_file = /etc/pki/ca-trust/source/anchors/internal-ca.crt
        </options>
    </Component>
    
  3. 检查语法
    thruk -a configtest
    若返回 “OK” 则继续;若提示 “duplicate name” 或 “permission denied”,按日志修正。
  4. 重启 thruk 服务
    systemctl restart thruk
  5. 验证
    在 Web 右上角 “Server Status” 应看到 core_bj_dx 状态为 “UP”,且仪表盘可查询到该 Core 下的主机与服务。
  6. 回滚预案
    若新 Core 拉取失败,只需把 disabled = 1 写入对应 <Component> 段,再次 systemctl reload thruk 即可秒级回滚,无需重启主进程,保障 SLA。

拓展思考

  1. 超过 5 个核心时,建议启用 Thruk Cluster 模式(使用 memcached 共享 session),并在 HAProxy 层做 TCP 健康检查,防止单核心夯住影响整体。
  2. Livestatus 与 InfluxDB 联动:可在 thruk_local.conf 里再加一个 <Component> backend = influxdb,把历史性能数据分流到 时序数据库,减轻 Livestatus 实时查询压力。
  3. 权限最小化:通过 xinetd 仅允许 thruk 服务器 IP 访问 6557 端口,并配合 fail2ban 防止扫描;同时把 socket 文件放到 /run/naemon/livestatus tmpfs,减少磁盘 IO。
  4. 自动化交付:用 Ansible 模板渲染 thruk_local.conf,变量文件按机房分组,GitLab CI 触发语法检查 + 灰度发布,实现 “配置即代码”。
  5. 面试加分项:主动提到 “监控的监控”——在 thruk 里再建一个 <Component> 指向 自身 naemon 进程,一旦 thruk 无法连接任何核心,立即通过 企业微信/钉钉群机器人 发告警,形成闭环。