How to install and configure collectd with network plugin using Ansible

How to install and configure collectd with network plugin using Ansible

collectd_install_configure

Here in this article we will try to install collectd to collect the system statistics. We will be enabling a plugin for collectd to expose the metrics for other system to consume using ansible.

Test Environment

Fedora 39 workstation
Ansible 2.16.2

What is collectd

collectd is a system daemon used to collect the system and application performance metrics periodically and provide mechanism to store the data is different formats (eg. rrd files). It can collect metrics from OS, applications, logfiles and external devices. This metrics data can further to made available over network for other analytic engines or systems to consume.

It is written is C language for performance and portability, allowing it to run on systems without scripting language or cron daemon. The collectd daemon supports over 100 plugins.

Here is the project structure for collectd setup.

admin@fedser:collectd$ tree .
.
├── inventory
│   ├── hosts
│   └── hosts_template
├── linux_setup_collectd.yml
├── README.md
└── roles
    ├── linux_configure_collectd
    │   ├── defaults
    │   │   └── main.yml
    │   ├── tasks
    │   │   └── main.yml
    │   └── templates
    │       ├── collectd.conf
    │       └── network.conf
    ├── linux_install_collectd
    │   └── tasks
    │       └── main.yml
    ├── linux_ping
    │   └── tasks
    │       └── main.yml
    ├── linux_restart_collectd
    │   └── tasks
    │       └── main.yml
    ├── linux_start_collectd
    │   └── tasks
    │       └── main.yml
    └── linux_stop_collectd
        └── tasks
            └── main.yml

Procedure

Step1: Install collectd

As a first step we need to install the collectd. Here is the role “linux_install_collectd” which will help us in installing this package.

admin@fedser:collectd$ cat roles/linux_install_collectd/tasks/main.yml 
- name: Install collectd
  dnf:
    name: collectd
    state: present

Step2: Configure collectd

The most important configuration file which is used to configure collectd is /etc/collectd.conf file which comes with the default installation of the package. For each plugin, there is a LoadPlugin line in the configuration.

By default the following plugins are enabled: CPU, Interface, Load, and Memory. By default exactly one write plugin is enabled. The first plugin available will be taken in this order: RRDtool, Network, CSV.

The following role “linux_configure_collectd” is used to enable the Network plugin and configure the port “25286” to listen on for the system statistics.

Here is the default collectd.conf file configuration grepped without any comments. As you can see the cpu, syslog, interface, load and memory plugins are enabled. But there is no write plugin enable through which we would be able to collect the metrics. In the network.conf we will be enabling this plugin and configure it to list on host – 192.168.122.6 and port – 25826 as shown below. Please update host as per your system IP or FQDN and port to 25826. This network.conf plugin config we will be loading by copying it into “/etc/collectd.d” which is included in the main configuration file.

admin@fedser:collectd$ cat roles/linux_configure_collectd/templates/collectd.conf | grep -v ^# | grep -v '^$'
LoadPlugin syslog
LoadPlugin cpu
LoadPlugin interface
LoadPlugin load
LoadPlugin memory
Include "/etc/collectd.d"
admin@fedser:collectd$ cat roles/linux_configure_collectd/templates/network.conf
LoadPlugin network
<Plugin network>
       # client setup:
       Server "{{collectd_config_network_host}}" "{{collectd_config_network_port}}"
</Plugin>
admin@fedser:collectd$ cat roles/linux_configure_collectd/defaults/main.yml 
---
collectd_config_directory: /etc
collectd_config_template: collectd.conf
collectd_config_network: network.conf
collectd_config_network_host: 192.168.122.6
collectd_config_network_port: 25826
admin@fedser:collectd$ cat roles/linux_configure_collectd/tasks/main.yml 
- name: "upload collectd config"
  template:
    src: "{{collectd_config_template}}"
    dest: "{{collectd_config_directory}}/{{collectd_config_template}}"
    owner: "root"
    group: "root"
    mode: 0644

- name: "upload plugin specific config"
  template:
    src: "{{collectd_config_network}}"
    dest: "{{collectd_config_directory}}/collectd.d/{{collectd_config_network}}"
    owner: "root"
    group: "root"
    mode: 0644

Step3: Start collectd

Once the required configuration files have been uploaded to the server, we can start the collectd service using the following role “linux_start_collectd”.

admin@fedser:collectd$ cat roles/linux_start_collectd/tasks/main.yml 
- name: "ensure collectd service started"
  service:
    name: collectd
    state: started

Step4: Stop collectd

The following role “linux_stop_collectd” helps in stopping the collectd service.

admin@fedser:collectd$ cat roles/linux_stop_collectd/tasks/main.yml 
- name: "ensure collectd service stopped"
  service:
    name: collectd
    state: stopped

Step5: Restart collectd

The following role “linux_restart_collectd” helps in restarting the collectd service.

admin@fedser:collectd$ cat roles/linux_restart_collectd/tasks/main.yml 
- name: "ensure collectd service restarted"
  service:
    name: collectd
    state: restarted

Step6: Collectd Ansible Playbook

Here is the main playbook from which the required roles can be triggered based on the tag information that we pass to the ansible playbook. The instructions to execute are provided in the README.md file below.

admin@fedser:collectd$ cat linux_setup_collectd.yml 
---
- hosts: "collectd"
  serial: 1
  become: true
  become_user: root
  roles:
  - { role: "linux_ping", tags: "linux_ping" }
  - { role: "linux_install_collectd", tags: "linux_install_collectd" }
  - { role: "linux_configure_collectd", tags: "linux_configure_collectd" }
  - { role: "linux_restart_collectd", tags: "linux_restart_collectd" }
  - { role: "linux_stop_collectd", tags: "linux_stop_collectd" }
  - { role: "linux_start_collectd", tags: "linux_start_collectd" }

Step7: README Instructions

admin@fedser:collectd$ cat README.md 
# Instructions for execution

ansible-playbook linux_setup_collectd.yml -i inventory/hosts --tags "linux_ping" -v
ansible-playbook linux_setup_collectd.yml -i inventory/hosts --tags "linux_install_collectd" -v 
ansible-playbook linux_setup_collectd.yml -i inventory/hosts --tags "linux_configure_collectd" -v
ansible-playbook linux_setup_collectd.yml -i inventory/hosts --tags "linux_stop_collectd" -v
ansible-playbook linux_setup_collectd.yml -i inventory/hosts --tags "linux_start_collectd" -v
ansible-playbook linux_setup_collectd.yml -i inventory/hosts --tags "linux_restart_collectd" -v

Step8: Execute Playbook

We can execute specific role by mentioning the “–tags” option as shown in the instructions. But if we want to execute all the roles in a playbook we can run the playbook command without providing the “–tags” option as shown below.

admin@fedser:collectd$ ansible-playbook linux_setup_collectd.yml -i inventory/hosts

PLAY [collectd] *************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [fedres.stack.com]

TASK [linux_ping : ansible ping pong validation] ****************************************************************************************************************************
ok: [fedres.stack.com]

TASK [linux_install_collectd : Install collectd] ****************************************************************************************************************************
ok: [fedres.stack.com]

TASK [linux_configure_collectd : upload collectd config] ********************************************************************************************************************
ok: [fedres.stack.com]

TASK [linux_configure_collectd : upload plugin specific config] *************************************************************************************************************
ok: [fedres.stack.com]

TASK [linux_restart_collectd : ensure collectd service restarted] ***********************************************************************************************************
changed: [fedres.stack.com]

TASK [linux_stop_collectd : ensure collectd service stopped] ****************************************************************************************************************
changed: [fedres.stack.com]

TASK [linux_start_collectd : ensure collectd service started] ***************************************************************************************************************
changed: [fedres.stack.com]

PLAY RECAP ******************************************************************************************************************************************************************
fedres.stack.com           : ok=8    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Step9: Validate results

As per the collectd service logs, we can see that the network plugin and other default plugins have been loaded successfully and the service is started.
Further the statistics that are collected can be captured by listing on udp port 25826.

admin@fedres:/etc/collectd.d$ journalctl -fu collectd.service
Dec 23 15:27:07 fedres.stack.com systemd[1]: Starting collectd.service - Collectd statistics daemon...
Dec 23 15:27:07 fedres.stack.com collectd[6915]: plugin_load: plugin "syslog" successfully loaded.
Dec 23 15:27:07 fedres.stack.com collectd[6915]: plugin_load: plugin "cpu" successfully loaded.
Dec 23 15:27:07 fedres.stack.com collectd[6915]: plugin_load: plugin "interface" successfully loaded.
Dec 23 15:27:07 fedres.stack.com collectd[6915]: plugin_load: plugin "load" successfully loaded.
Dec 23 15:27:07 fedres.stack.com collectd[6915]: plugin_load: plugin "memory" successfully loaded.
Dec 23 15:27:07 fedres.stack.com collectd[6915]: plugin_load: plugin "network" successfully loaded.
Dec 23 15:27:07 fedres.stack.com collectd[6915]: Systemd detected, trying to signal readiness.
Dec 23 15:27:07 fedres.stack.com systemd[1]: Started collectd.service - Collectd statistics daemon.
Dec 23 15:27:07 fedres.stack.com collectd[6915]: Initialization complete, entering read-loop.

Further the statistics that are collected can be captured by listing on udp port 25826. In our next article we will see how we can configure fluent-bit to get metrics data from the collectd service and output it to console.

Hope you enjoyed reading this article. Thank you..