diff --git a/README.md b/README.md index da216fd..81dabff 100644 --- a/README.md +++ b/README.md @@ -14,22 +14,30 @@ Design goals are to have a minimal memory footprint with a plugin system so that ## Requirements -### Operating systems -This role will work on the following operating systems: +### Supported systems +This role supports the following systems: * Red Hat * Debian * Ubuntu + * Docker container * Windows (Best effort) * (Open)Suse -So, you'll need one of those operating systems.. :-) -Please sent Pull Requests or suggestions when you want to use this role for other Operating systems. +So, you'll need one of those systems.. :-) +Please sent Pull Requests or suggestions when you want to use this role for other systems. ### InfluxDB You'll need an InfluxDB instance running somewhere on your network. +### Docker + +Docker needs to be installed on the target host. I can recommend these roles to install Docker: + +* [jgeusebroek.docker](https://galaxy.ansible.com/jgeusebroek/docker) +* [geerlingguy.docker](https://galaxy.ansible.com/geerlingguy/docker) + ## Upgrade ### 0.7.0 @@ -60,7 +68,14 @@ The following parameters can be set for the Telegraf agent: * `telegraf_agent_logfile`: The agent logfile name. Default: '' (means to log to stdout) (since v1.1) * `telegraf_agent_omit_hostname`: Do no set the "host" tag in the agent. Default: `False` (since v1.1) -Full agent settings reference: https://github.com/influxdata/telegraf/blob/master/docs/CONFIGURATION.md#agent-configuration +Docker specific role variables: + +* `telegraf_agent_docker`: Install Telegraf as a docker container. Default: `False` +* `telegraf_agent_docker_name`: Name of the docker container. Default: `telegraf` +* `telegraf_agent_docker_network_mode`: Networking mode of the docker container. Default: `bridge` +* `telegraf_agent_docker_restart_policy`: Docker container restart policy. Default: `unless-stopped` + +Full agent settings reference: [https://github.com/influxdata/telegraf/blob/master/docs/CONFIGURATION.md#agent-configuration](https://github.com/influxdata/telegraf/blob/master/docs/CONFIGURATION.md#agent-configuration). You can set tags for the host running telegraf: @@ -84,6 +99,55 @@ The config will be printed line by line into the configuration, so you could als - # Print an documentation line and it will be printed in the configuration file. + +### Docker specifics + +#### Docker image + +The official [Influxdata Telegraf image](https://hub.docker.com/_/telegraf) is used. `telegraf_agent_version` will translate to the image tag. + +#### Docker mounts + +Please note that the Docker container bind mounts basicly your whole system (read-only) to monitor the Docker Engine Host from within the container. To be precise: + + - /etc/telegraf:/etc/telegraf:ro + - /:/hostfs:ro + - /etc:/hostfs/etc:ro + - /proc:/hostfs/proc:ro + - /sys:/hostfs/sys:ro + - /var/run:/var/run:ro + +More information: [https://github.com/influxdata/telegraf/blob/master/docs/FAQ.md](https://github.com/influxdata/telegraf/blob/master/docs/FAQ.md). + +#### Example Docker configuration + + telegraf_agent_docker: True + # Force host networking mode, so Docker Engine Host traffic metrics can be gathered. + telegraf_agent_docker_network_mode: host + # Force a specific image tag. + telegraf_agent_version: 1.9.2-alpine + + telegraf_plugins_default: + - plugin: cpu + config: + - percpu = "true" + - plugin: disk + tags: + - diskmetrics = "true" + tagpass: + - fstype = [ "ext4", "xfs" ] + tagdrop: + - path = [ "/etc", "/etc/telegraf", "/etc/hostname", "/etc/hosts", "/etc/resolv.conf" ] + - plugin: io + - plugin: mem + - plugin: system + - plugin: swap + - plugin: netstat + - plugin: processes + - plugin: docker + config: + - endpoint = "unix:///var/run/docker.sock" + - timeout = "5s" ## Windows specific Variables @@ -177,6 +241,7 @@ The following have contributed to this Ansible role: * Romain BUREAU * TheCodeAssassin * tjend + * Jeroen Geusebroek Thank you all! diff --git a/defaults/main.yml b/defaults/main.yml index 751105d..659d514 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -14,6 +14,12 @@ telegraf_agent_flush_jitter: 0 telegraf_agent_aws_tags: False telegraf_agent_aws_tags_prefix: "" +# Docker specific settings +telegraf_agent_docker: False +telegraf_agent_docker_name: telegraf +telegraf_agent_docker_network_mode: bridge +telegraf_agent_docker_restart_policy: unless-stopped + # v0.13 settings (not sure if supported in older version): telegraf_agent_collection_jitter: 0 telegraf_agent_metric_batch_size: 1000 diff --git a/handlers/main.yml b/handlers/main.yml index 2dae45e..21473c4 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -6,8 +6,15 @@ name: telegraf state: restarted enabled: yes + when: not telegraf_agent_docker become: yes +- name: "Restart Telegraf container" + docker_container: + name: "{{ telegraf_agent_docker_name }}" + restart: True + when: telegraf_agent_docker + - name: "Restart Windows Telegraf" win_service: name: Telegraf diff --git a/tasks/Debian.yml b/tasks/Debian.yml index 0ce2ac9..f6fcd5a 100644 --- a/tasks/Debian.yml +++ b/tasks/Debian.yml @@ -45,7 +45,7 @@ when: - ansible_lsb is not defined or ansible_lsb.codename is not defined -- name: "Debian | Install telegraf package" +- name: "Debian | Install Telegraf package" apt: name: "{{ telegraf_agent_package }}" state: "{{ telegraf_agent_package_state }}" diff --git a/tasks/RedHat.yml b/tasks/RedHat.yml index af51b3d..0ee9c37 100644 --- a/tasks/RedHat.yml +++ b/tasks/RedHat.yml @@ -20,7 +20,7 @@ gpgcheck: yes gpgkey: https://repos.influxdata.com/influxdb.key -- name: "RedHat | Install telegraf package" +- name: "RedHat | Install Telegraf package" package: name: "{{ telegraf_agent_package }}" state: "{{ telegraf_agent_package_state }}" diff --git a/tasks/Suse.yml b/tasks/Suse.yml index d962676..f3ea02d 100644 --- a/tasks/Suse.yml +++ b/tasks/Suse.yml @@ -1,11 +1,11 @@ --- -- name: "Suse | Adding telegraf group" +- name: "Suse | Adding Telegraf group" group: name: telegraf state: present -- name: "Suse | Adding telegraf user" +- name: "Suse | Adding Telegraf user" user: name: telegraf group: telegraf diff --git a/tasks/configure_linux.yml b/tasks/configure_linux.yml index 0806ad5..59f222f 100644 --- a/tasks/configure_linux.yml +++ b/tasks/configure_linux.yml @@ -25,7 +25,9 @@ become: yes when: - telegraf_agent_version is version_compare('0.10.0', '<') - notify: "Restart Telegraf" + notify: + - Restart Telegraf + - Restart Telegraf container - name: "Copy the template for versions >= 0.10.0" template: @@ -37,7 +39,9 @@ become: yes when: - telegraf_agent_version is version_compare('0.10.0', '>=') - notify: "Restart Telegraf" + notify: + - Restart Telegraf + - Restart Telegraf container - name: "Check if extra plugins directory exists in case of exclusive" stat: @@ -54,7 +58,9 @@ - telegraf_plugins_extra_exclusive - telegraf_directory.stat.exists become: yes - notify: "Restart Telegraf" + notify: + - Restart Telegraf + - Restart Telegraf container - name: "Create telegraf extra plugin path" file: @@ -67,7 +73,9 @@ - telegraf_plugins_extra_exclusive - telegraf_directory.stat.exists become: yes - notify: "Restart Telegraf" + notify: + - Restart Telegraf + - Restart Telegraf container - name: "Copy telegraf extra plugins" template: @@ -84,7 +92,9 @@ - telegraf_plugins_extra is iterable - item.value.state|default('present') != 'absent' become: yes - notify: "Restart Telegraf" + notify: + - Restart Telegraf + - Restart Telegraf container - name: "Remove telegraf extra plugins" file: @@ -98,7 +108,9 @@ - telegraf_plugins_extra is iterable - item.value.state|default('present') == 'absent' become: yes - notify: "Restart Telegraf" + notify: + - Restart Telegraf + - Restart Telegraf container - name: "Force restart service after reread config" meta: flush_handlers @@ -109,3 +121,4 @@ state: started enabled: yes become: yes + when: not telegraf_agent_docker diff --git a/tasks/docker.yml b/tasks/docker.yml new file mode 100644 index 0000000..364d9df --- /dev/null +++ b/tasks/docker.yml @@ -0,0 +1,57 @@ +--- + +- name: Adding Telegraf group + group: + name: telegraf + state: present + gid: 995 + +- name: Adding Telegraf user + user: + name: telegraf + group: telegraf + state: present + create_home: False + home: /etc/telegraf + uid: 998 + system: True + become: yes + +- name: Create /etc/telegraf (home) directory + file: + path: /etc/telegraf + owner: telegraf + group: telegraf + mode: 0750 + state: directory + become: yes + +- name: Create /etc/telegraf.d directory + file: + path: /etc/telegraf/telegraf.d + owner: telegraf + group: telegraf + mode: 0750 + state: directory + become: yes + +- name: Ensure Telegraf Docker container is running + docker_container: + name: "{{ telegraf_agent_docker_name }}" + image: "telegraf:{{ telegraf_agent_version }}" + state: started + restart_policy: "{{ telegraf_agent_docker_restart_policy }}" + command: -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d + network_mode: "{{ telegraf_agent_docker_network_mode }}" + volumes: + - /etc/telegraf:/etc/telegraf:ro + - /:/hostfs:ro + - /etc:/hostfs/etc:ro + - /proc:/hostfs/proc:ro + - /sys:/hostfs/sys:ro + - /var/run:/var/run:ro + env: + HOST_MOUNT_PREFIX: /hostfs + HOST_ETC: /hostfs/etc + HOST_PROC: /hostfs/proc + HOST_SYS: /hostfs/sys diff --git a/tasks/main.yml b/tasks/main.yml index 42abf5e..cf89ad0 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -4,21 +4,24 @@ - name: "Install on RedHat" include_tasks: "RedHat.yml" when: - - ansible_os_family == "RedHat" + - ansible_os_family == "RedHat" and not telegraf_agent_docker - name: "Install on Debian" include_tasks: "Debian.yml" when: - - ansible_os_family == "Debian" + - ansible_os_family == "Debian" and not telegraf_agent_docker tags: - telegraf - packages - name: "Install on Suse" include_tasks: "Suse.yml" - when: ansible_os_family == "Suse" + when: ansible_os_family == "Suse" and not telegraf_agent_docker -- name: "Configure telegraf" +- include_tasks: "docker.yml" + when: telegraf_agent_docker + +- name: "Configure Telegraf" include_tasks: "configure_linux.yml" when: - ansible_os_family != "Windows" @@ -26,4 +29,4 @@ - name: "Install / Configure telegraf on Windows" include_tasks: "configure_windows.yml" when: - - ansible_os_family == "Windows" + - ansible_os_family == "Windows" and not telegraf_agent_docker