Ubuntu 22.04 安装 Collectd 指南

在 Ubuntu 22.04 服务器上从零安装 Collectd 5.12.0 的完整指南,覆盖 apt 在线安装与官方源码编译两条实测路径,含 systemd 托管、RRD 输出验证、常见问题与卸载清理。

CollectdCollectdUbuntu监控系统监控2026/10/30

Ubuntu 22.04 安装 Collectd 指南

目标:在 Ubuntu 22.04 服务器上从零装好 collectd 系统统计采集守护进程并验证指标落盘,覆盖 apt 在线安装与官方源码编译安装两条完整路径。已在 Ubuntu 22.04(jammy)服务器上逐条实测通过(collectd 5.12.0-10ubuntu0.1)。

一、环境信息

  • 操作系统:Ubuntu 22.04 LTS(jammy),amd64 架构
  • 软件来源 A:Ubuntu 官方仓库 jammy-updates/universe(collectd 5.12.0-10ubuntu0.1,apt policy collectd 实查)
  • 软件来源 B:collectd.org 官方源码包 collectd-5.12.0.tar.bz2(2020-09-03 发布)
  • 安装路径 A:apt 包管理器托管(配置 /etc/collectd/,二进制 /usr/sbin/collectd)
  • 安装路径 B:--prefix=/opt/collectd-5.12.0(二进制 /opt/collectd-5.12.0/sbin/collectd)
  • 数据目录:/var/lib/collectd/rrd/(方案 A 默认)/ /opt/collectd-5.12.0/var/lib/collectd/rrd/(方案 B 指定)
  • 网络情况:apt 仓库(archive.ubuntu.com)与官方源码包(storage.googleapis.com)下载时均直连可用,未启用代理

💡 说明:collectd 是纯 C 守护进程,安装本体轻量;apt 方式会连带安装插件所需的一批共享库(含 rrdtool、OpenJDK 等),磁盘占用详见 4.2 实测回显。

二、前置条件

  1. 一台 Ubuntu 22.04 服务器,具有 root 或 sudo 权限(本文命令以 root 执行);
  2. 可访问 Ubuntu apt 仓库(方案 A 必需)与 collectd.org 官方下载地址 storage.googleapis.com(方案 B 必需);
  3. 方案 B 需要编译工具链:gcc/make(build-essential)、flex、bison、pkg-config,以及按需的插件依赖库(librrd-dev、libcurl4-openssl-dev、libssl-dev 等);
  4. 无端口占用顾虑:collectd 默认只读本机 /proc 等系统接口,不监听 TCP 端口;仅启用 network/write_* 插件时才产生出站流量。

先确认系统与仓库就绪:

# 查看系统版本
cat /etc/os-release | head -2

实测回显:

PRETTY_NAME="Ubuntu 22.04.5 LTS"
NAME="Ubuntu"
VERSION="22.04.5 LTS (Jammy Jellyfish)"
# 刷新软件源索引
apt-get update

实测回显(输出节选,实测首尾):

Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [82.9 kB]
Fetched 49.2 MB in 59s (835 kB/s)
Reading package lists... Done

三、版本选型说明

# 查看 apt 仓库中 collectd 的候选版本
apt policy collectd

实测回显:

collectd:
  Installed: (none)
  Candidate: 5.12.0-10ubuntu0.1
  Version table:
     5.12.0-10ubuntu0.1 500
        500 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages

collectd 的上游版本节奏(GitHub Releases 实查,查询日期 2026-10-30):

版本线最新版本发布日期说明
5.x 稳定线collectd-5.12.02020-09-03当前各发行版打包的稳定版本
5.x 上一版collectd-5.11.02020-03-19历史版本
6.0 预发布collectd-6.0.0.rc32024-02-216.0 系列 RC 预发布,尚无稳定版

选型结论:

  1. 生产服务器选 apt 仓库的 5.12.0-10ubuntu0.1:与 Ubuntu 22.04 的库版本匹配、有安全更新(-10ubuntu0.1 即发行版修订号),systemd 单元、TypesDB、conf.d 目录结构开箱即用;
  2. 源码编译适合:需要非默认编译插件组合、要安装到独立前缀、或发行版仓库不可用的内网环境;
  3. 6.0 系列 2024 年起仅有 RC 预发布,暂不建议生产采用。

💡 说明:collectd -h 横幅显示 collectd 5.12.0.git,这是 Debian/Ubuntu 从上游 git 快照打包的构建标识;dpkg 记录的准确版本是 5.12.0-10ubuntu0.1,以 dpkg -l collectd 为准。

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

4.1 方案说明

使用 Ubuntu 官方仓库在线安装 collectd 主包与工具包。collectd 主包(collectd)会拉取元包 collectd-core(守护进程+全部插件 .so),collectd-utils 提供 collectdctl/collectd-nagios/collectd-tg 三个客户端工具。

4.2 安装步骤

# 安装 collectd 主包与工具包
DEBIAN_FRONTEND=noninteractive apt-get install -y collectd collectd-utils

实测回显(输出节选,实测首部):

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  alsa-topology-conf alsa-ucm-conf ca-certificates ca-certificates-java
  collectd-core default-jre-headless fontconfig fontconfig-config
  fonts-dejavu-core intel-cmt-cat java-common libabsl20210324 libasound2
  libasound2-data libatasmart4 libavahi-client3 libavahi-common-data
  libavahi-common3 libbrotli1 libbsd0 libbson-1.0-0 libc-ares2 libcairo2
  libcollectdclient1 libcups2 libcurl3-gnutls libdatrie1 libdbi1 libdeflate0
  libesmtp6 libfdt1 libfontconfig1 libfreetype6 libfribidi0 libgdbm-compat4
  libgdbm6 libgdk-pixbuf-2.0-0 libgdk-pixbuf2.0-bin libgdk-pixbuf2.0-common
  libgps28 libgraphite2-3 libgrpc++1 libgrpc10 libharfbuzz0b libhiredis0.14
  libi2c0 libip6tc2 libjansson4 libjbig0 libjpeg-turbo8 libjpeg8 liblcms2-2
  libldap-2.5-0 libldap-common liblua5.3-0 libmd0 libmemcached11
  libmicrohttpd12 libmnl0 libmodbus5 libmongoc-1.0-0 libmongocrypt0
  libmosquitto1 libmysqlclient21 libnghttp2-14 libnl-3-200 libnotify4 libnspr4
  libnss3 libnuma1 libopenipmi0 liboping0 libpango-1.0-0 libpangocairo-1.0-0
  libpangoft2-1.0-0 libpcap0.8 libpci3 libpcsclite1 libperl5.34 libpixman-1.0-0
  libpng16-16 libpq5 libprotobuf-c1 libprotobuf23 libpsl5 libpython3.10
  libqpid-proton11 librabbitmq4 librdkafka1 libriemann-client0 librrd8
  librte-eal22 librte-ethdev22 librte-kvargs22 librte-mbuf22 librte-mempool22
  librte-meter22 librte-net22 librte-ring22 librte-telemetry22 librtmp1
  libsasl2-2 libsasl2-modules libsasl2-modules-db libsensors-config
  libsensors5 libsnappy1v5 libsnmp-base libsnmp40 libssh-4 libthai-data
  libthai0 libtiff5 libvarnishapi2 libvirt0 libwebp7 libwrap0 libx11-6 libx11-data
  libxau6 libxcb-render0 libxcb-shm0 libxcb1 libxdmcp6 libxencall1
  libxendevicemodel1 libxenevtchn1 libxenforeignmemory1 libxengnttab1
  libxenhypfs1 libxenmisc4.16 libxenstore4 libxentoolcore1 libxentoollog1
  libxext6 libxrender1 libyajl2 mysql-common netbase openjdk-11-jre-headless
  openssl pci.ids perl perl-modules-5.34 publicsuffix rrdtool ucf
The following NEW packages will be installed:
  alsa-topology-conf alsa-ucm-conf ca-certificates ca-certificates-java
  collectd collectd-core collectd-utils default-jre-headless fontconfig
  fontconfig-config fonts-dejavu-core intel-cmt-cat java-common
  libabsl20210324 libasound2 libasound2-data libatasmart4 libavahi-client3
  (输出节选,实测首部:完整清单约 140 项依赖,此处省略)
0 upgraded, 149 newly installed, 0 to remove and 2 not upgraded.
Need to get 77.6 MB of archives.
After this operation, 318 MB of additional disk space will be used.

(输出节选,实测首部;依赖清单过长做省略展示,实际执行时完整列出。)

安装动作回显(关键 Setting up 行,实测照抄):

Setting up collectd-core (5.12.0-10ubuntu0.1) ...
Setting up collectd-utils (5.12.0-10ubuntu0.1) ...
Setting up librrd8:amd64 (1.7.2-3ubuntu6) ...
Setting up collectd (5.12.0-10ubuntu0.1) ...
Setting up rrdtool (1.7.2-3ubuntu6) ...
Setting up default-jre-headless (2:1.11-72build2) ...
Setting up openjdk-11-jre-headless (11.0.32.1+1-1ubuntu1~22.04) ...

⚠️ 注意:apt 方式会连带安装 OpenJDK 11(collectd 的 java 插件运行时依赖被打进依赖闭包)与 rrdtool 1.7.2,共解包 149 个包、占磁盘约 318 MB。若不希望引入 JVM,只能走方案 B 源码编译(不启用 java 插件则完全不需要 Java)。

4.3 安装内容核对

# 核对包版本与安装文件清单
dpkg -l collectd collectd-utils | tail -3

实测回显:

+++-==============-==================-============-=======================================================
ii  collectd       5.12.0-10ubuntu0.1 amd64        statistics collection and monitoring daemon
ii  collectd-utils 5.12.0-10ubuntu0.1 amd64        statistics collection and monitoring daemon (utilities)
# 查看 collectd-utils 提供的命令行工具
dpkg -L collectd-utils | grep /usr/bin

实测回显:

/usr/bin
/usr/bin/collectd-nagios
/usr/bin/collectd-tg
/usr/bin/collectdctl
# 查看插件目录规模(每个 .so 是一个可 LoadPlugin 的插件)
ls /usr/lib/collectd/ | wc -l
ls /usr/lib/collectd/ | head -6

实测回显:

152
aggregation.so
amqp.so
amqp1.so
apache.so
apcups.so
ascent.so
# 确认配置文件与类型字典位置
ls -la /etc/collectd/

实测回显:

total 56
drwxr-xr-x 3 root root  4096 Oct 30 14:19 .
drwxr-xr-x 1 root root  4096 Oct 30 14:19 ..
-rw-r--r-- 1 root root 36107 May 30  2018 collectd.conf
drwxr-xr-x 2 root root  4096 Oct 30 14:19 collectd.conf.d
-rw-r--r-- 1 root root    64 Jan 14  2018 collection.conf

关键路径一览:

路径作用
/usr/sbin/collectd守护进程本体
/etc/collectd/collectd.conf主配置文件(Ubuntu 自带全量注释模板)
/etc/collectd/collectd.conf.d/*.confInclude 引入的配置片段目录
/usr/lib/collectd/*.so插件动态库(152 个文件)
/usr/share/collectd/types.dbTypesDB 类型字典
/var/lib/collectd/rrd/rrdtool 插件默认数据目录

4.4 systemd 托管与启动验证

Ubuntu 包自带 systemd 单元 /lib/systemd/system/collectd.service,安装即注册且默认开机自启(enabled),但安装后不会自动启动:

# 查看服务单元状态
systemctl status collectd --no-pager -l 2>&1 | head -7

实测回显:

* collectd.service - Statistics collection and monitoring daemon
     Loaded: loaded (/lib/systemd/system/collectd.service; enabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: man:collectd(1)
             man:collectd.conf(5)
             https://collectd.org
# 启动服务并确认开机自启
systemctl enable collectd
systemctl start collectd

实测回显(enable 的同步提示):

Synchronizing state of collectd.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable collectd
# 验证运行状态
systemctl is-enabled collectd
systemctl is-active collectd

实测回显:

enabled
active
# 查看启动日志(插件逐个加载成功的记录)
journalctl -u collectd --no-pager -n 16 -o cat

实测回显(输出节选,实测首部):

plugin_load: plugin "cpu" successfully loaded.
plugin_load: plugin "df" successfully loaded.
plugin_load: plugin "disk" successfully loaded.
plugin_load: plugin "entropy" successfully loaded.
plugin_load: plugin "interface" successfully loaded.
plugin_load: plugin "irq" successfully loaded.
plugin_load: plugin "load" successfully loaded.
plugin_load: plugin "memory" successfully loaded.
plugin_load: plugin "processes" successfully loaded.
plugin_load: plugin "rrdtool" successfully loaded.
plugin_load: plugin "swap" successfully loaded.
plugin_load: plugin "users" successfully loaded.
Systemd detected, trying to signal readiness.
Started Statistics collection and monitoring daemon.
Initialization complete, entering read-loop.

💡 说明:Ubuntu 默认配置启用了 battery/cpu/df/disk/entropy/interface/irq/load/memory/processes/rrdtool/swap/users 十余个读插件。「Systemd detected, trying to signal readiness」表示 collectd 以 Type=notify 方式向 systemd 报告就绪,systemctl 会等服务真正进入 read-loop 才返回。

4.5 数据落盘验证

等 1~2 个采集间隔(默认 Interval 10 秒)后检查 RRD 输出:

# 查看按主机名组织的 RRD 指标文件(输出节选,实测首部)
find /var/lib/collectd/rrd -name "*.rrd" | head -12

实测回显(本机 6 颗逻辑核,输出节选):

/var/lib/collectd/rrd/collectd-server/disk-loop2/disk_octets.rrd
/var/lib/collectd/rrd/collectd-server/disk-loop2/disk_ops.rrd
/var/lib/collectd/rrd/collectd-server/disk-loop2/disk_io_time.rrd
/var/lib/collectd/rrd/collectd-server/disk-loop2/disk_time.rrd
/var/lib/collectd/rrd/collectd-server/cpu-2/cpu-steal.rrd
/var/lib/collectd/rrd/collectd-server/cpu-2/cpu-softirq.rrd
/var/lib/collectd/rrd/collectd-server/cpu-2/cpu-wait.rrd
/var/lib/collectd/rrd/collectd-server/cpu-2/cpu-user.rrd
/var/lib/collectd/rrd/collectd-server/cpu-2/cpu-system.rrd
/var/lib/collectd/rrd/collectd-server/cpu-2/cpu-nice.rrd
/var/lib/collectd/rrd/collectd-server/cpu-2/cpu-interrupt.rrd
/var/lib/collectd/rrd/collectd-server/cpu-2/cpu-idle.rrd
# 统计 RRD 文件总数
find /var/lib/collectd/rrd -name "*.rrd" | wc -l

实测回显:

280

目录名 collectd-server 即本机主机名,一级子目录是「插件-实例」(如 cpu-2 为第 3 颗逻辑核)。用 rrdtool 直接读取文件头验证采样周期:

# 查看 load 指标 RRD 的步长与心跳(rrdtool 由本安装连带提供)
rrdtool info /var/lib/collectd/rrd/collectd-server/load/load.rrd | head -12

实测回显:

filename = "/var/lib/collectd/rrd/collectd-server/load/load.rrd"
rrd_version = "0003"
step = 10
last_update = 1793371640
header_size = 6360
ds[shortterm].index = 0
ds[shortterm].type = "GAUGE"
ds[shortterm].minimal_heartbeat = 20
ds[shortterm].min = 0.0000000000e+00
ds[shortterm].max = 5.0000000000e+03
ds[shortterm].last_ds = "3.0869140625"
ds[shortterm].value = 0.0000000000e+00

step = 10 与全局 Interval 10 对应、minimal_heartbeat = 20 是两个间隔的超时线——至此方案 A 安装验证闭环完成。

五、方案 B:官方源码包编译安装

5.1 方案说明

从 collectd.org 官方下载地址获取 5.12.0 源码包,编译安装到独立前缀 /opt/collectd-5.12.0,不触碰系统 apt 环境,并手写 systemd 单元托管。适合需要自定义插件组合或与发行版隔离的场景。

5.2 安装编译依赖

最小依赖:编译工具链 + 想启用的插件库。本文启用 cpu/memory/load/interface/disk/rrdtool/write_graphite/write_http/unixsock 等核心插件,对应需要 librrd-dev(rrdtool)、libcurl4-openssl-dev 与 libssl-dev(write_http 的 HTTPS 与 curl 系列):

# 安装编译工具链与插件依赖库
DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential flex bison pkg-config librrd-dev libcurl4-openssl-dev libssl-dev

实测回显(输出节选,实测尾部):

Setting up build-essential (12.9ubuntu3) ...
Processing triggers for libc-bin (2.35-0ubuntu3.15) ...

💡 技术拓展:collectd 的插件编译遵循「依赖自动发现」——configure 检测到哪个库就编入哪个插件,缺库的插件自动跳过。因此依赖装得越多、能启用的插件越多;最小化系统只会编出核心读写插件,体积也最小。

5.3 下载源码包并校验

# 下载官方 5.12.0 源码包
cd /root
wget https://storage.googleapis.com/collectd-tarballs/collectd-5.12.0.tar.bz2

实测回显(输出节选,实测首尾):

--2026-10-30 14:51:22--  https://storage.googleapis.com/collectd-tarballs/collectd-5.12.0.tar.bz2
Resolving storage.googleapis.com (storage.googleapis.com)... 142.250.69.187, 142.250.73.123, 142.250.73.91, ...
Connecting to storage.googleapis.com (storage.googleapis.com)|142.250.69.187|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1902756 (1.8M) [application/x-bzip]
Saving to: 'collectd-5.12.0.tar.bz2'

     0K .......... .......... .......... .......... ..........  2% 74.9K 24s
  …(进度条省略)…
  1850K ........                                              100%  165M=2.2s

2026-10-30 14:51:12 (862 KB/s) - 'collectd-5.12.0.tar.bz2' saved [1902756/1902756]
# 校验文件大小与 sha256
ls -la collectd-5.12.0.tar.bz2
sha256sum collectd-5.12.0.tar.bz2

实测回显:

-rw-r--r-- 1 root root 1902756 Sep  3  2020 collectd-5.12.0.tar.bz2
5bae043042c19c31f77eb8464e56a01a5454e0b39fa07cf7ad0f1bfc9c3a09d6  collectd-5.12.0.tar.bz2

⚠️ 网络说明:官方下载地址 storage.googleapis.com 实测直连可用(1.8 MB,秒级完成),未启用代理;若你的服务器访问该地址慢或失败,可改走代理后再试。

5.4 解压与 configure

# 解压并进入源码目录
tar xjf collectd-5.12.0.tar.bz2
cd collectd-5.12.0

# 配置安装前缀(--prefix 决定安装位置)
./configure --prefix=/opt/collectd-5.12.0

实测回显(configure 末尾的插件汇总,输出节选,实测尾部):

    turbostat . . . . . . yes
    ubi . . . . . . . . . yes
    unixsock  . . . . . . yes
    uptime  . . . . . . . yes
    users . . . . . . . . yes
    uuid  . . . . . . . . yes
    varnish . . . . . . . no (pkg-config doesn't know varnishapi)
    virt  . . . . . . . . no
    vmem  . . . . . . . . yes
    vserver . . . . . . . yes
    wireless  . . . . . . yes
    write_graphite  . . . yes
    write_http  . . . . . yes
    write_influxdb_udp. . yes
    write_kafka . . . . . no (librdkafka/rdkafka.h not found)
    write_log . . . . . . yes
    write_sensu . . . . . yes
    write_tsdb  . . . . . yes
    zookeeper . . . . . . yes

核心插件汇总(同一份 configure 输出中实查):

    cpu . . . . . . . . . yes
    df  . . . . . . . . . yes
    disk  . . . . . . . . yes
    interface . . . . . . yes
    load  . . . . . . . . yes
    memory  . . . . . . . yes
    rrdtool . . . . . . . yes
    unixsock  . . . . . . yes
    write_graphite  . . . yes
    write_http  . . . . . yes

💡 说明:标注 no 的插件(如 write_kafka)是因为缺少对应开发库;需要哪个再 apt-get install librdkafka-dev 等后重新 configure 即可。configure 的完整汇总即是「本次编译会得到哪些插件」的清单。

5.5 编译与安装

# 编译(6 并发)并安装到 /opt/collectd-5.12.0
make -j6
make install

实测回显(make install 输出节选,实测首部):

make[1]: Entering directory '/root/collectd-5.12.0'
 /usr/bin/mkdir -p '/opt/collectd-5.12.0/lib'
 /bin/bash ./libtool   --mode=install /usr/bin/install -c   libcollectdclient.la '/opt/collectd-5.12.0/lib'
libtool: install: /usr/bin/install -c .libs/libcollectdclient.so.1.1.0 /opt/collectd-5.12.0/lib/libcollectdclient.so.1.1.0
# 核对安装结果
ls /opt/collectd-5.12.0/
ls /opt/collectd-5.12.0/lib/collectd/ | wc -l

实测回显:

bin  etc  include  lib  sbin  share  var
198
# 确认二进制版本与默认路径(注意默认路径已指向 --prefix)
/opt/collectd-5.12.0/sbin/collectd -h 2>&1 | tail -6

实测回显:

Builtin defaults:
  Config file       /opt/collectd-5.12.0/etc/collectd.conf
  PID file          /opt/collectd-5.12.0/var/run/collectd.pid
  Plugin directory  /opt/collectd-5.12.0/lib/collectd
  Data directory    /opt/collectd-5.12.0/var/lib/collectd

collectd 5.12.0, http://collectd.org/

与 apt 版横幅不同,源码编译版显示精确的 collectd 5.12.0。

5.6 最小配置与语法自检

写一个最小可用的 /opt/collectd-5.12.0/etc/collectd.conf(覆盖 make install 自带的样例):

# /opt/collectd-5.12.0/etc/collectd.conf —— 源码编译版最小配置
Hostname "src-build-server"
Interval 10
BaseDir "/opt/collectd-5.12.0/var/lib/collectd"
PIDFile "/opt/collectd-5.12.0/var/run/collectd.pid"
PluginDir "/opt/collectd-5.12.0/lib/collectd"
TypesDB "/opt/collectd-5.12.0/share/collectd/types.db"

LoadPlugin logfile
<Plugin logfile>
	LogLevel info
	File "/opt/collectd-5.12.0/var/log/collectd.log"
	Timestamp true
</Plugin>

LoadPlugin cpu
LoadPlugin load
LoadPlugin memory
LoadPlugin interface
LoadPlugin disk
LoadPlugin rrdtool
<Plugin rrdtool>
	DataDir "/opt/collectd-5.12.0/var/lib/collectd/rrd"
</Plugin>

配置要点:Hostname 决定 RRD 一级目录名;BaseDir/PIDFile/PluginDir/TypesDB 四项显式指向前缀内路径;logfile 插件把日志落到文件便于排查(否则默认 syslog 需 journalctl 查看)。

# 创建日志目录并做语法自检(-t 无输出即通过)
mkdir -p /opt/collectd-5.12.0/var/log
/opt/collectd-5.12.0/sbin/collectd -t
echo $?

实测回显:

0

5.7 systemd 单元托管

源码包在 contrib/systemd.collectd.service 提供官方单元模板(含 Type=notify 与能力收敛建议)。生产服务器上按实际 ExecStart 路径改写即可。本例写入 /etc/systemd/system/collectd.service:

[Unit]
Description=Collectd statistics daemon (source build)
Documentation=man:collectd(1)
After=network-online.target

[Service]
ExecStart=/opt/collectd-5.12.0/sbin/collectd
Type=notify
Restart=on-failure

[Install]
WantedBy=multi-user.target
# 重载单元、启用并启动
systemctl daemon-reload
systemctl enable collectd
systemctl start collectd

实测回显(enable 的建链提示):

Created symlink /etc/systemd/system/multi-user.target.wants/collectd.service -> /etc/systemd/system/collectd.service.
# 验证运行与数据落盘
systemctl is-active collectd
ls /opt/collectd-5.12.0/var/lib/collectd/rrd/src-build-server/

实测回显(数据目录随采集逐步生成,输出节选):

active
interface-eth0
interface-lo
load
memory

⚠️ 注意:若同一台服务器先前用 apt 装过 collectd,两套安装并存时会抢占同一个 systemd 单元名。共存场景请用不同单元名(如 collectd-src.service),或将 apt 版 systemctl disable --now collectd 后再启用源码版单元。

5.8 方案 B 验证闭环

# 查看日志确认插件加载与就绪
tail -4 /opt/collectd-5.12.0/var/log/collectd.log

实测回显:

[2026-10-30 14:47:26] Systemd detected, trying to signal readiness.
[2026-10-30 14:47:26] Initialization complete, entering read-loop.
[2026-10-30 14:47:26] unixsock plugin: No such group: `collectd'

(第三行为本机实验环境缺少 collectd 用户组的可复现提示,含义与处理见《Collectd 常用 CLI 命令与排障详解》篇第六章;服务运行不受影响。)

六、方案对比与选择建议

维度方案 A:apt 在线安装方案 B:源码编译安装
版本5.12.0-10ubuntu0.1(含发行版修订)上游 5.12.0 原版
是否需联网需 Ubuntu 仓库需 collectd.org 下载地址 + 编译依赖仓库
安装方式apt 包管理器,可升级可回退make install 到独立前缀,手动管理
磁盘占用约 318 MB(含 OpenJDK/rrdtool 等依赖闭包)编译产物数十 MB(按插件取舍)
systemd 单元包自带 /lib/systemd/system/collectd.service官方模板 contrib/systemd.collectd.service 手工适配
插件取舍152 个 .so 全量预编译,按 LoadPlugin 启用configure 依依赖决定,可最小化
升级维护apt upgrade 跟随发行版自行重新下载编译

选择建议:常规服务器一律方案 A,享受发行版安全更新与依赖管理;只有「最小化插件集」「隔离前缀」「仓库不可达的内网」三类诉求才选方案 B。两方案不要混装在同一台服务器的同一单元名下。

七、安装后的基础配置说明

  1. 主配置文件:/etc/collectd/collectd.conf(方案 A)。Ubuntu 自带的 36 KB 模板覆盖了全部全局参数与常用插件示例,绝大多数以 # 注释呈现;生效行可用 grep -vE '^\s*(#|$)' /etc/collectd/collectd.conf 查看。方案 B 的配置位置是 $PREFIX/etc/collectd.conf。
  2. 配置片段目录:/etc/collectd/collectd.conf.d/*.conf 通过主配置末尾的 Include 指令引入(默认启用),自定义插件段落建议放这里,便于包升级时合并。
  3. TypesDB:/usr/share/collectd/types.db 定义了全部指标类型(数据源名称、DS 类型、取值范围),自定义类型时可拷贝后追加,再用 TypesDB 指令指向自定义文件。
  4. 数据目录:/var/lib/collectd/rrd/(方案 A 默认)。主机名变更后 rrdtool 会在新主机名目录重新建文件,旧目录不会自动清理。
  5. 常用验证命令:collectd -t(语法自检)、systemctl restart collectd 后 journalctl -u collectd -o cat | tail 看插件加载、find /var/lib/collectd/rrd -name "*.rrd" | wc -l 看落盘规模。命令级详解见《Collectd 常用 CLI 命令与排障详解》篇。

八、常见问题(FAQ)

Q1:执行 collectd -t 显示正常,重启后服务却起不来,为什么? -t 只做配置语法解析,不实际加载插件,也不校验 TypesDB。插件不存在这类错误要在启动阶段才暴露(实测:配置里写 LoadPlugin foobarbaz 时 -t 返回 0 无任何输出,前台运行则报 plugin_load: Could not find plugin "foobarbaz" in /usr/lib/collectd 与 Error: Parsing the config file failed!)。排查时用 systemctl start collectd + journalctl -u collectd -o cat | tail -20 看真实错误。

Q2:apt 安装为什么装了 OpenJDK? Ubuntu 的 collectd 包把 java 插件的运行时依赖(default-jre-headless → openjdk-11-jre-headless)打进了依赖闭包,属于包管理行为。不启用 java 插件它也不会运行,但磁盘占用确实增加;介意的话选方案 B 编译(不装 librrd-dev 之外的重依赖)。

Q3:安装后 systemctl is-active collectd 是 inactive,是不是安装失败? 不是。Ubuntu 包默认 enabled(开机自启)但不立即启动,属预期行为,手动 systemctl start collectd 即可。

Q4:改了 collectd.conf 后重启,旧的 RRD 目录还在而且还在变吗? 旧目录保留但不再更新。实测:给 disk 插件加 Disk "sda" 选择后,disk-sda 目录 mtime 持续前进,disk-loop2 等旧目录 mtime 冻结在重启时刻。这些陈旧文件可用 find /var/lib/collectd/rrd/collectd-server -name "*.rrd" -newermt ... 判断活跃度后手工归档清理。

Q5:源码编译时 configure 提示某插件 no(如 write_kafka),如何补上? 安装对应开发库后重新 configure。write_kafka 缺 librdkafka、write_redis 缺 hiredis、write_mongodb 缺 libmongoc——报错信息会写明缺哪个(实测回显 write_kafka . . . . . . no (librdkafka/rdkafka.h not found))。

Q6:服务器改了主机名,RRD 目录会怎样? rrdtool 按主机名建一级目录,改名重启后新目录重新从零积累,旧目录保留。建议在首次部署时就固定 Hostname 指令(显式指定而非依赖 FQDN 反解),避免主机名漂移导致数据断档。

九、卸载与清理

方案 A 卸载:

# 停止并卸载 collectd(--purge 一并删除配置)
systemctl stop collectd
apt-get purge -y collectd collectd-utils
apt-get autoremove -y

实测回显(输出节选):

Removing collectd (5.12.0-10ubuntu0.1) ...
Removing collectd-utils (5.12.0-10ubuntu0.1) ...
# 删除残留数据目录(按需保留历史 RRD 则跳过)
rm -rf /var/lib/collectd

方案 B 卸载:

# 停用单元并删除安装前缀
systemctl disable --now collectd
rm -f /etc/systemd/system/collectd.service
systemctl daemon-reload
rm -rf /opt/collectd-5.12.0 /root/collectd-5.12.0 /root/collectd-5.12.0.tar.bz2

卸载后核对:dpkg -l | grep collectd 与 ls /usr/lib/collectd /opt/collectd-5.12.0 均应为空。

十、参考链接