diff --git a/roles/minecraft/defaults/main.yml b/roles/minecraft/defaults/main.yml index 1d622be..8409e28 100644 --- a/roles/minecraft/defaults/main.yml +++ b/roles/minecraft/defaults/main.yml @@ -23,6 +23,7 @@ minecraft_install_location: "/srv/minecraft" minecraft_data_location: "{{ minecraft_install_location }}/data" minecraft_rcon_location: "{{ minecraft_install_location }}/rcon" minecraft_backup_location: "{{ minecraft_install_location }}/worlds" +minecraft_telegraf_location: "{{ minecraft_install_location }}/telegraf" # renovate: depName=docker.io/itzg/minecraft-server minecraft_image_version: "latest" @@ -30,6 +31,18 @@ minecraft_image_version: "latest" # Container versions minecraft_image_tag: "{{ minecraft_image_version }}" +# renovate: depName=docker.io/library/telegraf +minecraft_telegraf_version: "1.20.4" + +# Changing this image may also require changing the UID / GID below, +# to set the correct permissions +minecraft_telegraf_image_version: "{{ minecraft_telegraf_version }}-alpine" + +# Telegraf config user & group id +# This is used for the config folder that is mounted to the container +minecraft_telegraf_config_uid: 100 +minecraft_telegraf_config_gid: 101 + # The minecraft server port that should be exposed minecraft_server_port: 25565 @@ -61,3 +74,16 @@ minecraft_additional_ports: [] minecraft_ipv6: enabled: false subnet: "fd9e:21a7:a92c:2325::/64" + +# Minecraft telegraf configuration, allows to configure a monitoring setup for the minecraft server +minecraft_telegraf: + enabled: false + influxdb_database: "telegraf" + # Your influxDB hosts + influxdb_endpoints: + - "influxdb.example.com" + influxdb_username: telegraf + influxdb_password: "" + influxdb_retention_policy: "minecraft" + influxdb_retention_policy_tag: "" + plugins: [] diff --git a/roles/minecraft/handlers/main.yml b/roles/minecraft/handlers/main.yml new file mode 100644 index 0000000..1e91da8 --- /dev/null +++ b/roles/minecraft/handlers/main.yml @@ -0,0 +1,25 @@ +--- +# Handlers for the minecraft role + +# Infrastructure +# Ansible instructions to deploy the infrastructure +# Copyright (C) 2021 Saibotk +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +- name: Restart telegraf + docker_compose: + services: "telegraf" + project_src: "{{ minecraft_install_location }}" + restarted: true + become: true diff --git a/roles/minecraft/tasks/main.yml b/roles/minecraft/tasks/main.yml index 96d79ae..64fca92 100644 --- a/roles/minecraft/tasks/main.yml +++ b/roles/minecraft/tasks/main.yml @@ -27,6 +27,7 @@ - "{{ minecraft_data_location }}" - "{{ minecraft_backup_location }}" - "{{ minecraft_rcon_location }}" + - "{{ minecraft_telegraf_location }}" become: true - name: Create install directory @@ -55,6 +56,33 @@ tags: - minecraft +- name: Create telegraf config directory + file: + path: "{{ item }}" + mode: '0700' + owner: "{{ minecraft_telegraf_config_uid }}" + group: "{{ minecraft_telegraf_config_gid }}" + state: directory + setype: "container_file_t" + with_items: + - "{{ minecraft_telegraf_location }}" + become: true + +- name: Deploy telegraf.conf + template: + src: telegraf.conf + dest: "{{ minecraft_telegraf_location }}/telegraf.conf" + mode: '0600' + owner: "{{ minecraft_telegraf_config_uid }}" + group: "{{ minecraft_telegraf_config_gid }}" + notify: "Restart telegraf" + tags: + - telegraf + - minecraft + become: true + when: + - minecraft_telegraf.enabled + - name: Deploy docker-compose.yml template: src: docker-compose.yml diff --git a/roles/minecraft/templates/docker-compose.yml b/roles/minecraft/templates/docker-compose.yml index a811590..c6338d2 100644 --- a/roles/minecraft/templates/docker-compose.yml +++ b/roles/minecraft/templates/docker-compose.yml @@ -90,6 +90,19 @@ services: {% endif %} +{% if minecraft_telegraf.enabled %} + telegraf: + image: docker.io/library/telegraf:{{ minecraft_telegraf_image_version }} + restart: always + mem_limit: 256mb + memswap_limit: 384mb + read_only: true + networks: + minecraft-backend: + volumes: + - {{ minecraft_telegraf_location }}:/etc/telegraf/:ro +{% endif %} + networks: minecraft-backend: driver: bridge diff --git a/roles/minecraft/templates/telegraf.conf b/roles/minecraft/templates/telegraf.conf new file mode 100644 index 0000000..13c9d47 --- /dev/null +++ b/roles/minecraft/templates/telegraf.conf @@ -0,0 +1,55 @@ +{{ ansible_managed | comment }} +[global_tags] + +[agent] + interval = "10s" + round_interval = true + metric_batch_size = 1000 + metric_buffer_limit = 1000000 + collection_jitter = "5s" + flush_interval = "10s" + flush_jitter = "5s" + precision = "" + hostname = "{{ ansible_fqdn }}" + omit_hostname = false + +[[outputs.influxdb]] + urls = ["https://{{ minecraft_telegraf.influxdb_endpoints | join('","https://') }}"] + database = "{{ minecraft_telegraf.influxdb_database }}" + timeout = "5s" + retention_policy = "{{ minecraft_telegraf.influxdb_retention_policy }}" + retention_policy_tag = "{{ minecraft_telegraf.influxdb_retention_policy_tag }}" + username = "{{ minecraft_telegraf.influxdb_username }}" + password = "{{ minecraft_telegraf.influxdb_password }}" + +{% if minecraft_telegraf.plugins is defined and minecraft_telegraf.plugins is iterable %} +{% for item in minecraft_telegraf.plugins %} +[[inputs.{{ item.plugin }}]] +{% if item.interval is defined %} + interval = "{{ item.interval }}s" +{% endif %} +{% if item.config is defined and item.config is iterable %} +{% for items in item.config %} + {{ items }} +{% endfor %} +{% endif %} +{% if item.tags is defined and item.tags is iterable %} + [inputs.{{ item.plugin }}.tags] +{% for items in item.tags %} + {{ items }} +{% endfor %} +{% endif %} +{% if item.tagpass is defined and item.tagpass is iterable %} + [inputs.{{ item.plugin }}.tagpass] +{% for items in item.tagpass %} + {{ items }} +{% endfor %} +{% endif %} +{% if item.tagdrop is defined and item.tagdrop is iterable %} + [inputs.{{ item.plugin }}.tagdrop] +{% for items in item.tagdrop %} + {{ items }} +{% endfor %} +{% endif %} +{% endfor %} +{% endif %}