Skip to content

Commit

Permalink
Merge pull request #85 from jgeusebroek/master
Browse files Browse the repository at this point in the history
Add Docker container support
  • Loading branch information
dj-wasabi authored Jan 22, 2019
2 parents 35cd290 + 5785e4a commit 2c444d9
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 20 deletions.
75 changes: 70 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:

Expand All @@ -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

Expand Down Expand Up @@ -177,6 +241,7 @@ The following have contributed to this Ansible role:
* Romain BUREAU
* TheCodeAssassin
* tjend
* Jeroen Geusebroek

Thank you all!

Expand Down
6 changes: 6 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tasks/Debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
2 changes: 1 addition & 1 deletion tasks/RedHat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
4 changes: 2 additions & 2 deletions tasks/Suse.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 19 additions & 6 deletions tasks/configure_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -109,3 +121,4 @@
state: started
enabled: yes
become: yes
when: not telegraf_agent_docker
57 changes: 57 additions & 0 deletions tasks/docker.yml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 8 additions & 5 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@
- 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"

- 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

0 comments on commit 2c444d9

Please sign in to comment.