From 497c70a3bf7dccffc8f0c43d65dcc9970883e2de Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sun, 27 Oct 2019 18:33:06 -0500 Subject: [PATCH] Add telegraf role --- roles/telegraf/defaults/main.yaml | 56 +++++++++++++ roles/telegraf/handlers/main.yaml | 14 ++++ roles/telegraf/tasks/Debian.yaml | 12 +++ roles/telegraf/tasks/main.yaml | 52 ++++++++++++ roles/telegraf/templates/telegraf.conf.j2 | 96 +++++++++++++++++++++++ 5 files changed, 230 insertions(+) create mode 100644 roles/telegraf/defaults/main.yaml create mode 100644 roles/telegraf/handlers/main.yaml create mode 100644 roles/telegraf/tasks/Debian.yaml create mode 100644 roles/telegraf/tasks/main.yaml create mode 100644 roles/telegraf/templates/telegraf.conf.j2 diff --git a/roles/telegraf/defaults/main.yaml b/roles/telegraf/defaults/main.yaml new file mode 100644 index 0000000..70fe022 --- /dev/null +++ b/roles/telegraf/defaults/main.yaml @@ -0,0 +1,56 @@ +--- +telegraf_package_name: telegraf +telegraf_package_state: present + +telegraf_service_name: telegraf +telegraf_service_state: started +telegraf_service_enabled: yes + +telegraf_config_global_tags: {} +telegraf_config_agent: + interval: 10s + round_interval: true + metric_batch_size: 1000 + metric_buffer_limit: 10000 + collection_jitter: 0s + flush_interval: 10s + flush_jitter: 0s + precision: + hostname: + omit_hostname: false +telegraf_config_outputs: + influxdb: {} +telegraf_config_inputs: + cpu: + - percpu: true + totalcpu: true + collect_cpu_time: false + report_active: false + disk: + - ignore_fs: + - tmpfs + - devtmpfs + - devfs + - iso9660 + - overlay + - aufs + - squashfs + diskio: [] + kernel: [] + mem: [] + processes: [] + swap: [] + system: [] + conntrack: [] + ipset: [] + iptables: + - table: filter + chains: [ "INPUT", "OUTPUT", "FORWARD", "LOG_DROP", "LOG_ACCEPT", "ICMP_FLOOD", "LIMIT_SSH" ] + - table: filter + name_override: ip6tables + chains: [ "INPUT", "OUTPUT", "FORWARD", "LOG_DROP", "LOG_ACCEPT", "ICMP_FLOOD", "LIMIT_SSH" ] + kernel_vmstat: [] + net: [] + netstat: [] + procstat: + - systemd_unit: minecraft.service diff --git a/roles/telegraf/handlers/main.yaml b/roles/telegraf/handlers/main.yaml new file mode 100644 index 0000000..0025e68 --- /dev/null +++ b/roles/telegraf/handlers/main.yaml @@ -0,0 +1,14 @@ +--- +- name: reload telegraf + service: + name: "{{ telegraf_service_name }}" + state: restarted + when: telegraf_service_enabled + +- name: telegraf daemon-reload + systemd: + name: "{{ telegraf_service_name }}" + daemon_reload: yes + state: restarted + +# vim:ft=yaml.ansible: diff --git a/roles/telegraf/tasks/Debian.yaml b/roles/telegraf/tasks/Debian.yaml new file mode 100644 index 0000000..33f574f --- /dev/null +++ b/roles/telegraf/tasks/Debian.yaml @@ -0,0 +1,12 @@ +--- +- name: add influx apt key + apt_key: + url: https://repos.influxdata.com/influxdb.key + state: present + +- name: configure apt repository + apt_repository: + repo: "deb https://repos.influxdata.com/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable" + filename: influxdb + update_cache: yes + state: present diff --git a/roles/telegraf/tasks/main.yaml b/roles/telegraf/tasks/main.yaml new file mode 100644 index 0000000..ce52740 --- /dev/null +++ b/roles/telegraf/tasks/main.yaml @@ -0,0 +1,52 @@ +--- +#- name: gather OS specific variables +# include_vars: "{{ item }}" +# with_first_found: +# - "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yaml" +# - "{{ ansible_distribution }}.yaml" +# - "{{ ansible_os_family }}.yaml" + +- name: run os specific tasks + include: "{{ item }}" + with_first_found: + - "{{ ansible_os_family }}.yaml" + +- name: manage package + package: + name: "{{ telegraf_package_name }}" + state: "{{ telegraf_package_state }}" + +- name: configure + template: + src: telegraf.conf.j2 + dest: /etc/telegraf/telegraf.conf + owner: root + group: root + mode: 0644 + notify: reload telegraf + +- name: create systemd override directory + file: + path: /etc/systemd/system/telegraf.service.d + owner: root + group: root + mode: 0755 + state: directory + +- name: patch systemd unit + copy: + dest: /etc/systemd/system/telegraf.service.d/override.conf + owner: root + group: root + mode: 0644 + content: | + [Service] + CapabilityBoundingSet=CAP_NET_RAW CAP_NET_ADMIN + AmbientCapabilities=CAP_NET_RAW CAP_NET_ADMIN + notify: telegraf daemon-reload + +- name: manage service + service: + name: "{{ telegraf_service_name }}" + state: "{{ telegraf_service_state }}" + enabled: "{{ telegraf_service_enabled }}" diff --git a/roles/telegraf/templates/telegraf.conf.j2 b/roles/telegraf/templates/telegraf.conf.j2 new file mode 100644 index 0000000..bc80ac0 --- /dev/null +++ b/roles/telegraf/templates/telegraf.conf.j2 @@ -0,0 +1,96 @@ +# {{ ansible_managed }} + +{% macro toml_scalar(v, seq=False) %} +{% if v is string %} + "{{ v | quote }}"{% if seq %},{% endif %} +{% elif v is sameas True or v is sameas False %} + {{ v | bool | lower }}{% if seq %},{% endif %} +{% elif v is sameas None %} + ""{% if seq %},{% endif %} +{% elif v is number %} + {{ v }}{% if seq %},{% endif %} +{% endif %} +{% endmacro %} + +{% if telegraf_config_global_tags %} +[global_tags] +{% for k, v in telegraf_config_global_tags.items() | default({}) %} +{% if v is string %} + {{ k }} = "{{ v | quote }}" +{% elif v is sameas True or v is sameas False %} + {{ k }} = {{ v | bool | lower }} +{% elif v is sameas None %} + {{ k }} = "" +{% else %} + {{ k }} = {{ v }} +{% endif %} +{% endfor %} +{% endif %} + + +{% if telegraf_config_agent %} +[agent] +{% for k, v in telegraf_config_agent.items() | default({}) %} +{% if v is string %} + {{ k }} = "{{ v | quote }}" +{% elif v is sameas True or v is sameas False %} + {{ k }} = {{ v | bool | lower }} +{% elif v is sameas None %} + {{ k }} = "" +{% else %} + {{ k }} = {{ v }} +{% endif %} +{% endfor %} +{% endif %} + + +{% if telegraf_config_outputs %} +{% for name, output in telegraf_config_outputs.items() | default({}) %} +[[outputs.{{ name }}]] +{% for k, v in output.items() | default({}) %} +{% if v is string %} + {{ k }} = "{{ v | quote }}" +{% elif v is sameas True or v is sameas False %} + {{ k }} = {{ v | bool | lower }} +{% elif v is sameas None %} + {{ k }} = "" +{% elif v is number %} + {{ k }} = {{ v }} +{% elif v is sequence %} + {{ k }} = [ +{% for i in v | default([]) %} + {{ toml_scalar(i, seq=True) }} +{% endfor %} + ] +{% endif %} +{% endfor %} +{% endfor %} +{% endif %} + + +{% if telegraf_config_inputs %} +{% for name, items in telegraf_config_inputs.items() | default({}) %} +{% for item in items %} +[[inputs.{{ name }}]] +{% for k, v in item.items() | default({}) %} +{% if v is string %} + {{ k }} = "{{ v | quote }}" +{% elif v is sameas True or v is sameas False %} + {{ k }} = {{ v | bool | lower }} +{% elif v is sameas None %} + {{ k }} = "" +{% elif v is number %} + {{ k }} = {{ v }} +{% elif v is sequence %} + {{ k }} = [ +{% for i in v | default([]) %} + {{ toml_scalar(i, seq=True) }} +{% endfor %} + ] +{% endif %} +{% endfor %} +{% else %} +[[inputs.{{ name }}]] +{% endfor %} +{% endfor %} +{% endif %}