|
| 1 | +(collectd-tutorial-base)= |
| 2 | +# Load data into CrateDB using collectd |
| 3 | + |
| 4 | +This tutorial walks you through configuring and starting the [collectd] |
| 5 | +agent and daemon, and CrateDB, to submit and store system metrics into |
| 6 | +CrateDB. |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | + |
| 10 | +Docker is used for running all components. This approach works consistently |
| 11 | +across Linux, macOS, and Windows. Alternatively, you can use Podman. |
| 12 | + |
| 13 | +### CLI |
| 14 | + |
| 15 | +Prepare shortcut for the psql command. |
| 16 | + |
| 17 | +::::{tab-set} |
| 18 | +:sync-group: os |
| 19 | + |
| 20 | +:::{tab-item} Linux and macOS |
| 21 | +:sync: unix |
| 22 | +To make the settings persistent, add them to your shell profile (`~/.profile`). |
| 23 | +```shell |
| 24 | +alias psql="docker run --rm -i --network=cratedb-demo docker.io/postgres psql" |
| 25 | +``` |
| 26 | +::: |
| 27 | +:::{tab-item} Windows PowerShell |
| 28 | +:sync: powershell |
| 29 | +To make the settings persistent, add them to your PowerShell profile (`$PROFILE`). |
| 30 | +```powershell |
| 31 | +function psql { docker run --rm -i --network=cratedb-demo docker.io/postgres psql @args } |
| 32 | +``` |
| 33 | +::: |
| 34 | +:::{tab-item} Windows Command |
| 35 | +:sync: dos |
| 36 | +```shell |
| 37 | +doskey psql=docker run --rm -i --network=cratedb-demo docker.io/postgres psql $* |
| 38 | +``` |
| 39 | +::: |
| 40 | + |
| 41 | +:::: |
| 42 | + |
| 43 | +### CrateDB |
| 44 | + |
| 45 | +Create a shared network. |
| 46 | +```shell |
| 47 | +docker network create cratedb-demo |
| 48 | +``` |
| 49 | + |
| 50 | +Start CrateDB. |
| 51 | +```shell |
| 52 | +docker run --name=cratedb --rm -it --network=cratedb-demo \ |
| 53 | + --publish=4200:4200 --publish=5432:5432 \ |
| 54 | + --env=CRATE_HEAP_SIZE=2g docker.io/crate -Cdiscovery.type=single-node |
| 55 | +``` |
| 56 | + |
| 57 | +## Configure |
| 58 | + |
| 59 | +### Provision database |
| 60 | + |
| 61 | +Create a database table that stores collected metrics. |
| 62 | +```shell |
| 63 | +psql "postgresql://crate:crate@cratedb:5432/" <<SQL |
| 64 | +CREATE TABLE doc.collectd_data ( |
| 65 | + p_time timestamp with time zone, |
| 66 | + p_host TEXT, |
| 67 | + p_plugin TEXT, |
| 68 | + p_plugin_instance TEXT, |
| 69 | + p_type TEXT, |
| 70 | + p_type_instance TEXT, |
| 71 | + p_value_names TEXT, |
| 72 | + p_type_names TEXT, |
| 73 | + p_values TEXT, |
| 74 | + month GENERATED ALWAYS AS date_trunc('month',p_time) |
| 75 | +) PARTITIONED BY (month); |
| 76 | +SQL |
| 77 | +``` |
| 78 | + |
| 79 | +### Build collectd OCI |
| 80 | + |
| 81 | +collectd is not available per OCI image, so either install standalone, |
| 82 | +or use these instructions for building an OCI to invoke on Docker or Podman. |
| 83 | +Store this file under the name `Dockerfile`, then invoke the command |
| 84 | +displayed below. |
| 85 | + |
| 86 | +:::{literalinclude} Dockerfile |
| 87 | +::: |
| 88 | +```shell |
| 89 | +docker build -t local/collectd -f Dockerfile . |
| 90 | +``` |
| 91 | + |
| 92 | +### Configure collectd |
| 93 | + |
| 94 | +To send the collected data to CrateDB, configure collectd by loading its |
| 95 | +[`postgresql` plugin] and supplying settings. Store this file under |
| 96 | +the name `collectd-cratedb.conf`. |
| 97 | + |
| 98 | +:::{literalinclude} collectd-cratedb.conf |
| 99 | +::: |
| 100 | + |
| 101 | +## Start services |
| 102 | + |
| 103 | +### collectd |
| 104 | + |
| 105 | +Start collectd. |
| 106 | +::::{tab-set} |
| 107 | +:sync-group: os |
| 108 | + |
| 109 | +:::{tab-item} Linux and macOS |
| 110 | +:sync: unix |
| 111 | +```shell |
| 112 | +docker run --name=collectd --rm -it --network=cratedb-demo \ |
| 113 | + --volume ${PWD}/collectd-cratedb.conf:/etc/collectd/collectd.conf.d/collectd-cratedb.conf \ |
| 114 | + local/collectd |
| 115 | +``` |
| 116 | +::: |
| 117 | +:::{tab-item} Windows PowerShell |
| 118 | +:sync: powershell |
| 119 | +```powershell |
| 120 | +docker run --name=collectd --rm -it --network=cratedb-demo ` |
| 121 | + --volume "${PWD}\collectd-cratedb.conf:/etc/collectd/collectd.conf.d/collectd-cratedb.conf" ` |
| 122 | + local/collectd |
| 123 | +``` |
| 124 | +::: |
| 125 | +:::{tab-item} Windows Command |
| 126 | +:sync: dos |
| 127 | +```shell |
| 128 | +docker run --name=collectd --rm -it --network=cratedb-demo ^ |
| 129 | + --volume "%cd%\collectd-cratedb.conf:/etc/collectd/collectd.conf.d/collectd-cratedb.conf" ^ |
| 130 | + local/collectd |
| 131 | +``` |
| 132 | +::: |
| 133 | +:::: |
| 134 | + |
| 135 | +## Explore data |
| 136 | + |
| 137 | +After starting the daemon, the first metrics will appear in the designated table in |
| 138 | +CrateDB, ready to be inspected. |
| 139 | +```shell |
| 140 | +psql "postgresql://crate:crate@cratedb:5432/" -c "SELECT * FROM doc.collectd_data LIMIT 5;" |
| 141 | +``` |
| 142 | +```psql |
| 143 | + p_time | p_host | p_plugin | p_plugin_instance | p_type | p_type_instance | p_value_names | p_type_names | p_values | month |
| 144 | +----------------------------+--------------+-----------+-------------------+------------+-----------------+---------------+-------------------+--------------+---------------------------- |
| 145 | + 2025-09-20 13:57:12.822+00 | 9cde293016c2 | interface | gre0 | if_errors | | {'rx','tx'} | {'gauge','gauge'} | {nan,nan} | 2025-09-01 00:00:00.000+00 |
| 146 | + 2025-09-20 13:57:12.822+00 | 9cde293016c2 | memory | | memory | cached | {'value'} | {'gauge'} | {4600500224} | 2025-09-01 00:00:00.000+00 |
| 147 | + 2025-09-20 13:57:12.822+00 | 9cde293016c2 | interface | gre0 | if_dropped | | {'rx','tx'} | {'gauge','gauge'} | {nan,nan} | 2025-09-01 00:00:00.000+00 |
| 148 | + 2025-09-20 13:57:12.822+00 | 9cde293016c2 | interface | erspan0 | if_octets | | {'rx','tx'} | {'gauge','gauge'} | {nan,nan} | 2025-09-01 00:00:00.000+00 |
| 149 | + 2025-09-20 13:57:12.822+00 | 9cde293016c2 | interface | ip_vti0 | if_dropped | | {'rx','tx'} | {'gauge','gauge'} | {nan,nan} | 2025-09-01 00:00:00.000+00 |
| 150 | +(5 rows) |
| 151 | +``` |
| 152 | + |
| 153 | + |
| 154 | +[collectd]: https://collectd.org/ |
| 155 | +[`postgresql` plugin]: https://collectd.org/documentation/manpages/collectd.conf.html#plugin-postgresql |
0 commit comments