From c2ce8b42873bc487736e74b99f34c40895debc64 Mon Sep 17 00:00:00 2001 From: Arthur Koziel Date: Fri, 26 Feb 2021 22:14:52 +0800 Subject: [PATCH] add memcached_exporter (#1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add memcached_exporter * remove user/group and add consul service * fix consul tags * unpack bin from tarball Signed-off-by: Jakub SokoĊ‚owski --- README.md | 14 ++++++++- defaults/main.yml | 10 ++++++ handlers/main.yml | 6 ++++ tasks/main.yml | 3 ++ tasks/memcached_exporter.yml | 42 +++++++++++++++++++++++++ templates/memcached_exporter.service.j2 | 12 +++++++ 6 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 handlers/main.yml create mode 100644 tasks/memcached_exporter.yml create mode 100644 templates/memcached_exporter.service.j2 diff --git a/README.md b/README.md index 23a5f99..430a88c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ # Description -This role installs and configures [memcached](https://memcached.org/) service. +This role installs and configures [memcached](https://memcached.org/) and [memcached\_exporter](https://github.com/prometheus/memcached_exporter/) services. + +The exporter is available on port `9150` and the prometheus metrics can be fetched via the `/metrics` http endpoint: + +``` +curl 0.0.0.0:9150/metrics +``` # Configuration @@ -11,3 +17,9 @@ memcached_port: 11211 memcached_size_mb: 64 memcached_maxconn: 1024 ``` + +The installation of `memcached_exporter` is optional. It is installed by default but can be disabled by setting: + +```yaml +memcached_exporter_enabled: false +``` diff --git a/defaults/main.yml b/defaults/main.yml index 6b416db..33253f5 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -15,3 +15,13 @@ memcached_cfg_file: '/etc/memcached.conf' memcached_consul_tags_default: ['memcached', 'cache'] memcached_consul_tags_extra: [] memcached_consul_tags: '{{ memcached_consul_tags_default + memcached_consul_tags_extra }}' + +memcached_exporter_enabled: true +memcached_exporter_port: 9150 +memcached_exporter_version: '0.8.0' +memcached_exporter_release_name: 'memcached_exporter-{{ memcached_exporter_version }}.linux-amd64' +memcached_exporter_url: 'https://github.com/prometheus/memcached_exporter/releases/download/v{{ memcached_exporter_version }}/{{ memcached_exporter_release_name }}.tar.gz' +memcached_exporter_checksum: '0a666f41d733ebdc20cf2dc66fbb0c2899afc237002db67b7a31ff7f9f7dde3f' + +memcached_exporter_consul_service: 'memcached_exporter' +memcached_exporter_consul_tags: '{{ memcached_consul_tags + ["exporter", "metrics"] }}' diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..6679223 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,6 @@ +--- +- name: restart memcached_exporter + systemd: + daemon_reload: true + name: memcached_exporter + state: restarted diff --git a/tasks/main.yml b/tasks/main.yml index f3395d6..928adf0 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -18,6 +18,9 @@ state: restarted when: memcached_config.changed +- include_tasks: memcached_exporter.yml + when: memcached_exporter_enabled + - name: Create Consul service definition include_role: name=consul-service vars: diff --git a/tasks/memcached_exporter.yml b/tasks/memcached_exporter.yml new file mode 100644 index 0000000..b974977 --- /dev/null +++ b/tasks/memcached_exporter.yml @@ -0,0 +1,42 @@ +- name: Download memcached_exporter tarball + get_url: + url: '{{ memcached_exporter_url }}' + dest: '/tmp/{{ memcached_exporter_release_name }}.tar.gz' + checksum: 'sha256:{{ memcached_exporter_checksum }}' + mode: 0644 + +- name: Unpack binary from tarball + unarchive: + src: '/tmp/{{ memcached_exporter_release_name }}.tar.gz' + dest: '/usr/local/bin' + creates: '/usr/local/bin/memcached_exporter' + remote_src: true + extra_ops: + - '--wildcards' + - '--strip=1' + - '*/memcached_exporter' + notify: restart memcached_exporter + +- name: Copy the memcached_exporter systemd service file + template: + src: 'memcached_exporter.service.j2' + dest: '/lib/systemd/system/memcached_exporter.service' + owner: root + group: root + mode: 0644 + notify: restart memcached_exporter + +- name: Create Consul service definition + include_role: name=consul-service + vars: + consul_config_name: '{{ memcached_exporter_consul_service }}' + consul_services: + - name: '{{ memcached_exporter_consul_service }}' + tags: '{{ memcached_exporter_consul_tags }}' + port: '{{ memcached_exporter_port }}' + address: '{{ memcached_addr }}' + checks: + - id: '{{ memcached_exporter_consul_service }}-health' + name: '{{ memcached_exporter_consul_service }}' + type: 'tcp' + tcp: '{{ memcached_addr }}:{{ memcached_exporter_port }}' diff --git a/templates/memcached_exporter.service.j2 b/templates/memcached_exporter.service.j2 new file mode 100644 index 0000000..d2e1ca9 --- /dev/null +++ b/templates/memcached_exporter.service.j2 @@ -0,0 +1,12 @@ +[Unit] +Description="Memcached Exporter for Prometheus" +Documentation=https://github.com/prometheus/memcached_exporter +Requires=network-online.target +After=network-online.target + +[Service] +ExecStart=/usr/local/bin/memcached_exporter --memcached.address={{ memcached_addr }}:{{ memcached_port }} +Restart=on-failure + +[Install] +WantedBy=multi-user.target