diff --git a/roles/mysqld_exporter/README.md b/roles/mysqld_exporter/README.md new file mode 100644 index 00000000..5730b3a1 --- /dev/null +++ b/roles/mysqld_exporter/README.md @@ -0,0 +1,99 @@ +
+ +# Ansible Role: mysqld exporter + + +Deploy prometheus [mysql exporter](https://github.com/prometheus/mysqld_exporter) using ansible. + +## Requirements + +- Ansible >= 2.7 (It might work on previous versions, but we cannot guarantee it) +- gnu-tar on Mac deployer host (`brew install gnu-tar`) +- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`) + +## Role Variables + +All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) and are listed in the table below. + +| Name | Default Value | Description | +| -------------- | ------------- | -----------------------------------| +| `mysqld_exporter_version` | 0.14.0 | mysqld exporter package version. Also accepts latest as parameter. | +| `mysqld_exporter_binary_local_dir` | "" | Enables the use of local packages instead of those distributed on github. The parameter may be set to a directory where the `mysqld_exporter` binary is stored on the host where ansible is run. This overrides the `mysqld_exporter_version` parameter | +| `mysqld_exporter_binary_url` | `https://github.com/prometheus/mysqld_exporter/releases/download/v{{ mysqld_exporter_version }}/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz` | URL of the mysqld\_exporter binaries .tar.gz file | +| `mysqld_exporter_checksums_url` | `https://github.com/prometheus/mysqld_exporter/releases/download/v{{ mysqld_exporter_version }}/sha256sums.txt` | URL of the mysqld\_exporter checksums file | +| `mysqld_exporter_web_listen_address` | "0.0.0.0:9104" | Address on which mysqld\_exporter will listen | +| `mysqld_exporter_web_telemetry_path` | "/metrics" | Path under which to expose metrics | +| `mysqld_exporter_enabled_collectors` | `[]` | List of dicts defining additionally enabled collectors and their configuration. It adds collectors to [those enabled by default](https://github.com/prometheus/mysqld_exporter#collector-flags). | +| `mysqld_exporter_disabled_collectors` | [] | List of disabled collectors. By default mysqld_exporter disables collectors listed [here](https://github.com/prometheus/mysqld_exporter#collector-flags). | +| `mysqld_exporter_tls_server_config` | {} | Configuration for TLS authentication. Keys and values are the same as in [mysqld_exporter docs](https://github.com/prometheus/mysqld_exporter/blob/master/https/README.md#sample-config). | +| `mysqld_exporter_http_server_config` | {} | Config for HTTP/2 support. Keys and values are the same as in [mysqld_exporter docs](https://github.com/prometheus/mysqld_exporter/blob/master/https/README.md#sample-config). | +| `mysqld_exporter_basic_auth_users` | {} | Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt. | + +## Example + +### Playbook + +Use it in a playbook as follows: +```yaml +- hosts: all + collections: + - prometheus.prometheus + roles: + - prometheus.prometheus.mysqld_exporter +``` + +### TLS config + +Before running mysqld_exporter role, the user needs to provision their own certificate and key. +```yaml +- hosts: all + pre_tasks: + - name: Create mysqld_exporter cert dir + ansible.builtin.file: + path: "/etc/mysqld_exporter" + state: directory + owner: root + group: root + + - name: Create cert and key + community.crypto.x509_certificate: + path: /etc/mysqld_exporter/tls.cert + csr_path: /etc/mysqld_exporter/tls.csr + privatekey_path: /etc/mysqld_exporter/tls.key + provider: selfsigned + collections: + - prometheus.prometheus + roles: + - prometheus.prometheus.mysqld_exporter + vars: + mysqld_exporter_tls_server_config: + cert_file: /etc/mysqld_exporter/tls.cert + key_file: /etc/mysqld_exporter/tls.key + mysqld_exporter_basic_auth_users: + randomuser: examplepassword +``` + + +### Demo site + +We provide an example site that demonstrates a full monitoring solution based on prometheus and grafana. The repository with code and links to running instances is [available on github](https://github.com/prometheus/demo-site) and the site is hosted on [DigitalOcean](https://digitalocean.com). + +## Local Testing + +The preferred way of locally testing the role is to use Docker and [molecule](https://github.com/ansible-community/molecule) (v3.x). You will have to install Docker on your system. See "Get started" for a Docker package suitable for your system. Running your tests is as simple as executing `molecule test`. + +## Continuous Integration + +Combining molecule and circle CI allows us to test how new PRs will behave when used with multiple ansible versions and multiple operating systems. This also allows use to create test scenarios for different role configurations. As a result we have quite a large test matrix which can take more time than local testing, so please be patient. + +## Contributing + +See [contributor guideline](CONTRIBUTING.md). + +## Troubleshooting + +See [troubleshooting](TROUBLESHOOTING.md). + +## License + +This project is licensed under MIT License. See [LICENSE](/LICENSE) for more details. diff --git a/roles/mysqld_exporter/TROUBLESHOOTING.md b/roles/mysqld_exporter/TROUBLESHOOTING.md new file mode 100644 index 00000000..0bece12a --- /dev/null +++ b/roles/mysqld_exporter/TROUBLESHOOTING.md @@ -0,0 +1,20 @@ +# Troubleshooting + +## Bad requests (HTTP 400) + +This role downloads checksums from the Github project to verify the integrity of artifacts installed on your servers. When downloading the checksums, a "bad request" error might occur. + +This happens in environments which (knowningly or unknowling) use the [netrc mechanism](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) to auto-login into servers. + +Unless netrc is needed by your playbook and ansible roles, please unset the var like so: + +``` +$ NETRC= ansible-playbook ... +``` + +Or: + +``` +$ export NETRC= +$ ansible-playbook ... +``` diff --git a/roles/mysqld_exporter/defaults/main.yml b/roles/mysqld_exporter/defaults/main.yml new file mode 100644 index 00000000..e44b0253 --- /dev/null +++ b/roles/mysqld_exporter/defaults/main.yml @@ -0,0 +1,35 @@ +--- +mysqld_exporter_version: 0.14.0 +mysqld_exporter_binary_local_dir: "" +mysqld_exporter_binary_url: "https://github.com/{{ _mysqld_exporter_repo }}/releases/download/v{{ mysqld_exporter_version }}/\ + mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz" +mysqld_exporter_checksums_url: "https://github.com/{{ _mysqld_exporter_repo }}/releases/download/v{{ mysqld_exporter_version }}/sha256sums.txt" + +mysqld_exporter_web_listen_address: "0.0.0.0:9104" +mysqld_exporter_web_telemetry_path: "/metrics" + +mysqld_exporter_username: exporter +mysqld_exporter_password: secret + +mysqld_exporter_config_file: "mysqld_exporter.cnf" +# mysqld_exporter_host: localhost +# mysqld_exporter_port: 3306 +mysqld_exporter_socket: "/run/mysqld/mysqld.sock" + +mysqld_exporter_tls_server_config: {} + +mysqld_exporter_http_server_config: {} + +mysqld_exporter_basic_auth_users: {} + +mysqld_exporter_enabled_collectors: [] +# - engine_innodb_status +# - perf_schema.eventsstatements + +mysqld_exporter_disabled_collectors: [] + +# Internal variables. +mysqld_exporter_binary_install_dir: "/usr/local/bin" +mysqld_exporter_config_dir: "/etc/mysqld_exporter" +mysqld_exporter_system_group: "mysqld-exp" +mysqld_exporter_system_user: "{{ mysqld_exporter_system_group }}" diff --git a/roles/mysqld_exporter/handlers/main.yml b/roles/mysqld_exporter/handlers/main.yml new file mode 100644 index 00000000..33078931 --- /dev/null +++ b/roles/mysqld_exporter/handlers/main.yml @@ -0,0 +1,10 @@ +--- +- name: Restart mysqld_exporter + listen: "restart mysqld_exporter" + become: true + ansible.builtin.systemd: + daemon_reload: true + name: mysqld_exporter + state: restarted + when: + - not ansible_check_mode diff --git a/roles/mysqld_exporter/meta/argument_specs.yml b/roles/mysqld_exporter/meta/argument_specs.yml new file mode 100644 index 00000000..38180dbe --- /dev/null +++ b/roles/mysqld_exporter/meta/argument_specs.yml @@ -0,0 +1,80 @@ +--- +# yamllint disable rule:line-length +argument_specs: + main: + short_description: "Prometheus MySQLd Exporter" + description: + - "Deploy prometheus L(mysqld_exporter,https://github.com/prometheus/mysqld_exporter) using ansible" + author: + - "Prometheus Community" + options: + mysqld_exporter_version: + description: "MySQLd exporter package version. Also accepts latest as parameter." + default: "1.1.2" + mysqld_exporter_binary_local_dir: + description: + - "Enables the use of local packages instead of those distributed on github." + - "The parameter may be set to a directory where the C(mysqld_exporter) binary is stored on the host where ansible is run." + - "This overrides the I(mysqld_exporter_version) parameter" + mysqld_exporter_binary_url: + description: "URL of the mysqld_exporter binaries .tar.gz file" + default: "https://github.com/prometheus/{{ _mysqld_exporter_repo }}/download/v{{ mysqld_exporter_version }}/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz" + mysqld_exporter_checksums_url: + description: "URL of the mysqld_exporter checksums file" + default: "https://github.com/{{ _mysqld_exporter_repo }}/releases/download/v{{ mysqld_exporter_version }}/sha256sums.txt" + mysqld_exporter_web_listen_address: + description: "Address on which mysqld_exporter will listen" + default: "0.0.0.0:9104" + mysqld_exporter_web_telemetry_path: + description: "Path under which to expose metrics" + default: "/metrics" + mysqld_exporter_config_file: + description: "The filename of the exporter mysql config file" + default: "mysqld_exporter.cnf" + mysqld_exporter_host: + description: "The target MySQL host" + mysqld_exporter_port: + description: "The target MySQL port" + mysqld_exporter_socket: + description: "The target MySQL unix socket" + default: "/run/mysqld/mysqld.sock" + mysqld_exporter_enabled_collectors: + description: + - "List of dicts defining additionally enabled collectors and their configuration." + - "It adds collectors to L(those enabled by default,https://github.com/prometheus/mysqld_exporter#enabled-by-default)." + type: "list" + default: [] + mysqld_exporter_disabled_collectors: + description: + - "List of disabled collectors." + - "By default mysqld_exporter disables collectors listed L(here,https://github.com/prometheus/mysqld_exporter#disabled-by-default)." + type: "list" + elements: "str" + mysqld_exporter_tls_server_config: + description: + - "Configuration for TLS authentication." + - "Keys and values are the same as in L(mysqld_exporter docs,https://github.com/prometheus/mysqld_exporter/blob/master/https/README.md#sample-config)." + type: "dict" + mysqld_exporter_http_server_config: + description: + - "Config for HTTP/2 support." + - "Keys and values are the same as in L(mysqld_exporter docs,https://github.com/prometheus/mysqld_exporter/blob/master/https/README.md#sample-config)." + type: "dict" + mysqld_exporter_basic_auth_users: + description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt." + type: "dict" + mysqld_exporter_binary_install_dir: + description: + - "I(Advanced)" + - "Directory to install mysqld_exporter binary" + default: "/usr/local/bin" + mysqld_exporter_system_group: + description: + - "I(Advanced)" + - "System group for MySQLd Exporter" + default: "mysqld-exp" + mysqld_exporter_system_user: + description: + - "I(Advanced)" + - "MySQLd Exporter user" + default: "mysqld-exp" diff --git a/roles/mysqld_exporter/meta/main.yml b/roles/mysqld_exporter/meta/main.yml new file mode 100644 index 00000000..690811e6 --- /dev/null +++ b/roles/mysqld_exporter/meta/main.yml @@ -0,0 +1,34 @@ +--- +galaxy_info: + author: Prometheus Community + description: Prometheus MySQLd Exporter + license: Apache + company: none + min_ansible_version: "2.7" + platforms: + - name: Ubuntu + versions: + - bionic + - xenial + - name: Debian + versions: + - stretch + - buster + - name: EL + versions: + - '7' + - '8' + - name: Fedora + versions: + - '30' + - '31' + galaxy_tags: + - monitoring + - prometheus + - exporter + - metrics + - system + - mysql + - mariadb + +dependencies: [] diff --git a/roles/mysqld_exporter/molecule/alternative/converge.yml b/roles/mysqld_exporter/molecule/alternative/converge.yml new file mode 100644 index 00000000..54ee7651 --- /dev/null +++ b/roles/mysqld_exporter/molecule/alternative/converge.yml @@ -0,0 +1,40 @@ +--- +- name: Run role + hosts: all + any_errors_fatal: true + roles: + - prometheus.prometheus.mysqld_exporter + pre_tasks: + - name: Create mysqld_exporter cert dir + ansible.builtin.file: + path: "{{ mysqld_exporter_tls_server_config.cert_file | dirname }}" + state: directory + owner: root + group: root + mode: u+rwX,g+rwX,o=rX + + - name: Copy cert and key + ansible.builtin.copy: + src: "{{ item.src }}" + dest: "{{ item.dest }}" + mode: "{{ item.mode | default('0644') }}" + loop: + - src: "/tmp/tls.cert" + dest: "{{ mysqld_exporter_tls_server_config.cert_file }}" + - src: "/tmp/tls.key" + dest: "{{ mysqld_exporter_tls_server_config.key_file }}" + vars: + mysqld_exporter_binary_local_dir: "/tmp/mysqld_exporter-linux-amd64" + mysqld_exporter_web_listen_address: "127.0.0.1:8080" + mysqld_exporter_enabled_collectors: + - slave_hosts + mysqld_exporter_disabled_collectors: + - global_variables + + mysqld_exporter_tls_server_config: + cert_file: /etc/mysqld_exporter/tls.cert + key_file: /etc/mysqld_exporter/tls.key + mysqld_exporter_http_server_config: + http2: true + mysqld_exporter_basic_auth_users: + randomuser: examplepassword diff --git a/roles/mysqld_exporter/molecule/alternative/molecule.yml b/roles/mysqld_exporter/molecule/alternative/molecule.yml new file mode 100644 index 00000000..ed97d539 --- /dev/null +++ b/roles/mysqld_exporter/molecule/alternative/molecule.yml @@ -0,0 +1 @@ +--- diff --git a/roles/mysqld_exporter/molecule/alternative/prepare.yml b/roles/mysqld_exporter/molecule/alternative/prepare.yml new file mode 100644 index 00000000..ba8eb305 --- /dev/null +++ b/roles/mysqld_exporter/molecule/alternative/prepare.yml @@ -0,0 +1,56 @@ +--- +- name: Prepare + hosts: localhost + gather_facts: false + vars: + go_arch: amd64 + mysqld_exporter_version: 0.13.0 + tasks: + - name: Download mysqld_exporter binary to local folder + become: false + ansible.builtin.get_url: + url: "https://github.com/prometheus/mysqld_exporter/releases/download/v{{ mysqld_exporter_version }}/\ + mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz" + mode: 0644 + register: _download_binary + until: _download_binary is succeeded + retries: 5 + delay: 2 + check_mode: false + + - name: Unpack mysqld_exporter binary + become: false + ansible.builtin.unarchive: + src: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp" + creates: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}/mysqld_exporter" + check_mode: false + + - name: Link to mysqld_exporter binaries directory + become: false + ansible.builtin.file: + src: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-amd64" + dest: "/tmp/mysqld_exporter-linux-amd64" + state: link + check_mode: false + + - name: Install pyOpenSSL for certificate generation + ansible.builtin.pip: + name: "pyOpenSSL" + + - name: Create private key + community.crypto.openssl_privatekey: + path: "/tmp/tls.key" + + - name: Create CSR + community.crypto.openssl_csr: + path: "/tmp/tls.csr" + privatekey_path: "/tmp/tls.key" + + - name: Create certificate + community.crypto.x509_certificate: + path: "/tmp/tls.cert" + csr_path: "/tmp/tls.csr" + privatekey_path: "/tmp/tls.key" + provider: selfsigned diff --git a/roles/mysqld_exporter/molecule/alternative/tests/test_alternative.py b/roles/mysqld_exporter/molecule/alternative/tests/test_alternative.py new file mode 100644 index 00000000..1c1a96af --- /dev/null +++ b/roles/mysqld_exporter/molecule/alternative/tests/test_alternative.py @@ -0,0 +1,23 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_service(host): + s = host.service("mysqld_exporter") +# assert s.is_enabled + assert s.is_running + + +def test_socket(host): + sockets = [ + "tcp://127.0.0.1:8080" + ] + for socket in sockets: + s = host.socket(socket) + assert s.is_listening diff --git a/roles/mysqld_exporter/molecule/default/converge.yml b/roles/mysqld_exporter/molecule/default/converge.yml new file mode 100644 index 00000000..fce9b74b --- /dev/null +++ b/roles/mysqld_exporter/molecule/default/converge.yml @@ -0,0 +1,8 @@ +--- +- name: Converge + hosts: all + any_errors_fatal: true + roles: + - prometheus.prometheus.mysqld_exporter + vars: + mysqld_exporter_web_listen_address: "127.0.0.1:9104" diff --git a/roles/mysqld_exporter/molecule/default/molecule.yml b/roles/mysqld_exporter/molecule/default/molecule.yml new file mode 100644 index 00000000..ed97d539 --- /dev/null +++ b/roles/mysqld_exporter/molecule/default/molecule.yml @@ -0,0 +1 @@ +--- diff --git a/roles/mysqld_exporter/molecule/default/tests/test_default.py b/roles/mysqld_exporter/molecule/default/tests/test_default.py new file mode 100644 index 00000000..e2ad38da --- /dev/null +++ b/roles/mysqld_exporter/molecule/default/tests/test_default.py @@ -0,0 +1,66 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_directories(host): + dirs = [ + "/etc/mysqld_exporter" + ] + for dir in dirs: + d = host.file(dir) + assert d.is_directory + assert d.exists + + +def test_files(host): + files = [ + "/etc/systemd/system/mysqld_exporter.service", + "/usr/local/bin/mysqld_exporter" + ] + for file in files: + f = host.file(file) + assert f.exists + assert f.is_file + + +def test_permissions_didnt_change(host): + dirs = [ + "/etc", + "/root", + "/usr", + "/var" + ] + for file in dirs: + f = host.file(file) + assert f.exists + assert f.is_directory + assert f.user == "root" + assert f.group == "root" + + +def test_user(host): + assert host.group("mysqld-exp").exists + assert "mysqld-exp" in host.user("mysqld-exp").groups + assert host.user("mysqld-exp").shell == "/usr/sbin/nologin" + assert host.user("mysqld-exp").home == "/" + + +def test_service(host): + s = host.service("mysqld_exporter") +# assert s.is_enabled + assert s.is_running + + +def test_socket(host): + sockets = [ + "tcp://127.0.0.1:9104" + ] + for socket in sockets: + s = host.socket(socket) + assert s.is_listening diff --git a/roles/mysqld_exporter/molecule/latest/converge.yml b/roles/mysqld_exporter/molecule/latest/converge.yml new file mode 100644 index 00000000..bdeb42e7 --- /dev/null +++ b/roles/mysqld_exporter/molecule/latest/converge.yml @@ -0,0 +1,8 @@ +--- +- name: Run role + hosts: all + any_errors_fatal: true + roles: + - prometheus.prometheus.mysqld_exporter + vars: + mysqld_exporter_version: latest diff --git a/roles/mysqld_exporter/molecule/latest/molecule.yml b/roles/mysqld_exporter/molecule/latest/molecule.yml new file mode 100644 index 00000000..ed97d539 --- /dev/null +++ b/roles/mysqld_exporter/molecule/latest/molecule.yml @@ -0,0 +1 @@ +--- diff --git a/roles/mysqld_exporter/molecule/latest/tests/test_latest.py b/roles/mysqld_exporter/molecule/latest/tests/test_latest.py new file mode 100644 index 00000000..2fa859e8 --- /dev/null +++ b/roles/mysqld_exporter/molecule/latest/tests/test_latest.py @@ -0,0 +1,30 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import testinfra.utils.ansible_runner +import pytest + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +@pytest.mark.parametrize("files", [ + "/etc/systemd/system/mysqld_exporter.service", + "/usr/local/bin/mysqld_exporter" +]) +def test_files(host, files): + f = host.file(files) + assert f.exists + assert f.is_file + + +def test_service(host): + s = host.service("mysqld_exporter") + # assert s.is_enabled + assert s.is_running + + +def test_socket(host): + s = host.socket("tcp://0.0.0.0:9104") + assert s.is_listening diff --git a/roles/mysqld_exporter/tasks/configure.yml b/roles/mysqld_exporter/tasks/configure.yml new file mode 100644 index 00000000..db375e1e --- /dev/null +++ b/roles/mysqld_exporter/tasks/configure.yml @@ -0,0 +1,51 @@ +--- +- name: Copy the mysqld_exporter systemd service file + ansible.builtin.template: + src: mysqld_exporter.service.j2 + dest: /etc/systemd/system/mysqld_exporter.service + owner: root + group: root + mode: 0644 + notify: restart mysqld_exporter + +- name: Create mysqld_exporter config directory + ansible.builtin.file: + path: "{{ mysqld_exporter_config_dir }}" + state: directory + owner: root + group: root + mode: u+rwX,g+rwX,o=rX + +- name: Copy the mysqld_exporter config file + ansible.builtin.template: + src: my_cnf.j2 + dest: "{{ mysqld_exporter_config_dir }}/{{ mysqld_exporter_config_file }}" + owner: root + group: root + mode: 0644 + notify: restart mysqld_exporter + +- name: Configure mysqld_exporter web config + when: + ( mysqld_exporter_tls_server_config | length > 0 ) or + ( mysqld_exporter_http_server_config | length > 0 ) or + ( mysqld_exporter_basic_auth_users | length > 0 ) + block: + - name: Copy the mysqld_exporter web config file + ansible.builtin.template: + src: web_config.yaml.j2 + dest: "{{ mysqld_exporter_config_dir }}/web_config.yaml" + owner: root + group: root + mode: 0644 + notify: restart mysqld_exporter + +- name: Allow mysqld_exporter port in SELinux on RedHat OS family + community.general.seport: + ports: "{{ mysqld_exporter_web_listen_address.split(':')[-1] }}" + proto: tcp + setype: http_port_t + state: present + when: + - ansible_version.full is version_compare('2.4', '>=') + - ansible_selinux.status == "enabled" diff --git a/roles/mysqld_exporter/tasks/install.yml b/roles/mysqld_exporter/tasks/install.yml new file mode 100644 index 00000000..c8f3e7f2 --- /dev/null +++ b/roles/mysqld_exporter/tasks/install.yml @@ -0,0 +1,65 @@ +--- +- name: Create the mysqld_exporter group + ansible.builtin.group: + name: "{{ mysqld_exporter_system_group }}" + state: present + system: true + when: mysqld_exporter_system_group != "root" + +- name: Create the mysqld_exporter user + ansible.builtin.user: + name: "{{ mysqld_exporter_system_user }}" + groups: "{{ mysqld_exporter_system_group }}" + append: true + shell: /usr/sbin/nologin + system: true + create_home: false + home: / + when: mysqld_exporter_system_user != "root" + +- name: Discover latest version + when: mysqld_exporter_binary_local_dir | length == 0 + block: + + - name: Download mysqld_exporter binary to local folder + become: false + ansible.builtin.get_url: + url: "{{ mysqld_exporter_binary_url }}" + dest: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz" + checksum: "sha256:{{ __mysqld_exporter_checksum }}" + mode: '0644' + register: _download_binary + until: _download_binary is succeeded + retries: 5 + delay: 2 + delegate_to: localhost + check_mode: false + + - name: Unpack mysqld_exporter binary + become: false + ansible.builtin.unarchive: + src: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp" + creates: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}/mysqld_exporter" + delegate_to: localhost + check_mode: false + + - name: Propagate mysqld_exporter binaries + ansible.builtin.copy: + src: "/tmp/mysqld_exporter-{{ mysqld_exporter_version }}.linux-{{ go_arch }}/mysqld_exporter" + dest: "{{ mysqld_exporter_binary_install_dir }}/mysqld_exporter" + mode: 0755 + owner: root + group: root + notify: restart mysqld_exporter + when: not ansible_check_mode + +- name: Propagate locally distributed mysqld_exporter binary + ansible.builtin.copy: + src: "{{ mysqld_exporter_binary_local_dir }}/mysqld_exporter" + dest: "{{ mysqld_exporter_binary_install_dir }}/mysqld_exporter" + mode: 0755 + owner: root + group: root + when: mysqld_exporter_binary_local_dir | length > 0 + notify: restart mysqld_exporter diff --git a/roles/mysqld_exporter/tasks/main.yml b/roles/mysqld_exporter/tasks/main.yml new file mode 100644 index 00000000..12e73164 --- /dev/null +++ b/roles/mysqld_exporter/tasks/main.yml @@ -0,0 +1,51 @@ +--- +- name: Preflight + ansible.builtin.include_tasks: preflight.yml + tags: + - mysqld_exporter_install + - mysqld_exporter_configure + - mysqld_exporter_run + +- name: Install + ansible.builtin.include_tasks: + file: install.yml + apply: + become: true + when: + ( not __mysqld_exporter_is_installed.stat.exists ) or + ( (__mysqld_exporter_current_version_output.stderr_lines | length > 0) + and (__mysqld_exporter_current_version_output.stderr_lines[0].split(" ")[2] != mysqld_exporter_version) ) or + ( (__mysqld_exporter_current_version_output.stdout_lines | length > 0) + and (__mysqld_exporter_current_version_output.stdout_lines[0].split(" ")[2] != mysqld_exporter_version) ) or + ( mysqld_exporter_binary_local_dir | length > 0 ) + tags: + - mysqld_exporter_install + +- name: SELinux + ansible.builtin.include_tasks: + file: selinux.yml + apply: + become: true + when: ansible_selinux.status == "enabled" + tags: + - mysqld_exporter_configure + +- name: Configure + ansible.builtin.include_tasks: + file: configure.yml + apply: + become: true + tags: + - mysqld_exporter_configure + +- name: Ensure MySQLd Exporter is enabled on boot + become: true + ansible.builtin.systemd: + daemon_reload: true + name: mysqld_exporter + enabled: true + state: started + when: + - not ansible_check_mode + tags: + - mysqld_exporter_run diff --git a/roles/mysqld_exporter/tasks/preflight.yml b/roles/mysqld_exporter/tasks/preflight.yml new file mode 100644 index 00000000..877d0162 --- /dev/null +++ b/roles/mysqld_exporter/tasks/preflight.yml @@ -0,0 +1,101 @@ +--- +- name: Assert usage of systemd as an init system + ansible.builtin.assert: + that: ansible_service_mgr == 'systemd' + msg: "This role only works with systemd" + +- name: Install package fact dependencies + ansible.builtin.package: + name: "{{ _pkg_fact_req }}" + state: present + when: (_pkg_fact_req) + vars: + _pkg_fact_req: "{% if (ansible_pkg_mgr == 'apt') %}\ + {{ ('python-apt' if ansible_python_version is version('3', '<') else 'python3-apt') }} + {% else %}\ + {% endif %}" + +- name: Gather package facts + ansible.builtin.package_facts: + when: "not 'packages' in ansible_facts" + +- name: Naive assertion of proper listen address + ansible.builtin.assert: + that: + - "':' in mysqld_exporter_web_listen_address" + +- name: Assert collectors are not both disabled and enabled at the same time + ansible.builtin.assert: + that: + - "item not in mysqld_exporter_enabled_collectors" + with_items: "{{ mysqld_exporter_disabled_collectors }}" + +- name: Assert that TLS config is correct + when: mysqld_exporter_tls_server_config | length > 0 + block: + - name: Assert that TLS key and cert path are set + ansible.builtin.assert: + that: + - "mysqld_exporter_tls_server_config.cert_file is defined" + - "mysqld_exporter_tls_server_config.key_file is defined" + + - name: Check existence of TLS cert file + ansible.builtin.stat: + path: "{{ mysqld_exporter_tls_server_config.cert_file }}" + register: __mysqld_exporter_cert_file + + - name: Check existence of TLS key file + ansible.builtin.stat: + path: "{{ mysqld_exporter_tls_server_config.key_file }}" + register: __mysqld_exporter_key_file + + - name: Assert that TLS key and cert are present + ansible.builtin.assert: + that: + - "{{ __mysqld_exporter_cert_file.stat.exists }}" + - "{{ __mysqld_exporter_key_file.stat.exists }}" + +- name: Check if mysqld_exporter is installed + ansible.builtin.stat: + path: "{{ mysqld_exporter_binary_install_dir }}/mysqld_exporter" + register: __mysqld_exporter_is_installed + check_mode: false + tags: + - mysqld_exporter_install + +- name: Gather currently installed mysqld_exporter version (if any) + ansible.builtin.command: "{{ mysqld_exporter_binary_install_dir }}/mysqld_exporter --version" + changed_when: false + register: __mysqld_exporter_current_version_output + check_mode: false + when: __mysqld_exporter_is_installed.stat.exists + tags: + - mysqld_exporter_install + +- name: Discover latest version + ansible.builtin.set_fact: + mysqld_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/prometheus/mysqld_exporter/releases/latest', split_lines=False) | + from_json).get('tag_name') | replace('v', '') }}" + run_once: true + until: mysqld_exporter_version is version('0.0.0', '>=') + retries: 10 + when: + - mysqld_exporter_version == "latest" + - mysqld_exporter_binary_local_dir | length == 0 + +- name: Get mysqld_exporter binary checksum + when: mysqld_exporter_binary_local_dir | length == 0 + block: + - name: Get checksum list from github + ansible.builtin.set_fact: + __mysqld_exporter_checksums: "{{ lookup('url', mysqld_exporter_checksums_url, wantlist=True) | list }}" + run_once: true + until: __mysqld_exporter_checksums is search('linux-' + go_arch + '.tar.gz') + retries: 10 + + - name: "Get checksum for {{ go_arch }}" + ansible.builtin.set_fact: + __mysqld_exporter_checksum: "{{ item.split(' ')[0] }}" + with_items: "{{ __mysqld_exporter_checksums }}" + when: + - "('linux-' + go_arch + '.tar.gz') in item" diff --git a/roles/mysqld_exporter/tasks/selinux.yml b/roles/mysqld_exporter/tasks/selinux.yml new file mode 100644 index 00000000..754cbd0a --- /dev/null +++ b/roles/mysqld_exporter/tasks/selinux.yml @@ -0,0 +1,23 @@ +--- +- name: Install selinux python packages [RedHat] + ansible.builtin.package: + name: "{{ ['libselinux-python', 'policycoreutils-python'] + if ansible_python_version is version('3', '<') else + ['python3-libselinux', 'python3-policycoreutils'] }}" + state: present + register: _install_selinux_packages + until: _install_selinux_packages is success + retries: 5 + delay: 2 + when: ansible_os_family | lower == "redhat" + +- name: Install selinux python packages [clearlinux] + ansible.builtin.package: + name: sysadmin-basic + state: present + register: _install_selinux_packages + until: _install_selinux_packages is success + retries: 5 + delay: 2 + when: + - ansible_distribution | lower == "clearlinux" diff --git a/roles/mysqld_exporter/templates/my_cnf.j2 b/roles/mysqld_exporter/templates/my_cnf.j2 new file mode 100644 index 00000000..f1c61820 --- /dev/null +++ b/roles/mysqld_exporter/templates/my_cnf.j2 @@ -0,0 +1,11 @@ +{{ ansible_managed | comment }} +[client] +user={{ mysqld_exporter_username }} +password={{ mysqld_exporter_password }} +{% if mysqld_exporter_host is defined %} +host=localhost +port=3306 +{% endif %} +{% if mysqld_exporter_socket is defined %} +socket={{ mysqld_exporter_socket }} +{% endif %} diff --git a/roles/mysqld_exporter/templates/mysqld_exporter.service.j2 b/roles/mysqld_exporter/templates/mysqld_exporter.service.j2 new file mode 100644 index 00000000..2bc97d45 --- /dev/null +++ b/roles/mysqld_exporter/templates/mysqld_exporter.service.j2 @@ -0,0 +1,55 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=Prometheus MySQLd Exporter +After=network-online.target + +[Service] +Type=simple +User={{ mysqld_exporter_system_user }} +Group={{ mysqld_exporter_system_group }} +ExecStart={{ mysqld_exporter_binary_install_dir }}/mysqld_exporter \ +{% for collector in mysqld_exporter_enabled_collectors -%} +{% if not collector is mapping %} + --collect.{{ collector }} \ +{% else -%} +{% set name, options = (collector.items()|list)[0] -%} + --collect.{{ name }} \ +{% for k,v in options|dictsort %} + --collect.{{ name }}.{{ k }}={{ v | quote }} \ +{% endfor -%} +{% endif -%} +{% endfor -%} +{% for collector in mysqld_exporter_disabled_collectors %} + --no-collect.{{ collector }} \ +{% endfor %} +{% if mysqld_exporter_tls_server_config | length > 0 or mysqld_exporter_http_server_config | length > 0 or mysqld_exporter_basic_auth_users | length > 0 %} + --web.config.file={{ mysqld_exporter_config_dir }}/web_config.yaml \ +{% endif %} + --web.listen-address={{ mysqld_exporter_web_listen_address }} \ + --web.telemetry-path={{ mysqld_exporter_web_telemetry_path }} \ + --config.my-cnf={{ mysqld_exporter_config_dir }}/{{ mysqld_exporter_config_file }} + +SyslogIdentifier=mysqld_exporter +Restart=always +RestartSec=1 +StartLimitInterval=0 + +{% set protect_home = 'yes' %} +{% for m in ansible_mounts if m.mount.startswith('/home') %} +{% set protect_home = 'read-only' %} +{% endfor %} +ProtectHome={{ protect_home }} +NoNewPrivileges=yes + +{% if (ansible_facts.packages.systemd | first).version is version('232', '>=') %} +ProtectSystem=strict +ProtectControlGroups=true +ProtectKernelModules=true +ProtectKernelTunables=yes +{% else %} +ProtectSystem=full +{% endif %} + +[Install] +WantedBy=multi-user.target diff --git a/roles/mysqld_exporter/templates/web_config.yaml.j2 b/roles/mysqld_exporter/templates/web_config.yaml.j2 new file mode 100644 index 00000000..6be01161 --- /dev/null +++ b/roles/mysqld_exporter/templates/web_config.yaml.j2 @@ -0,0 +1,18 @@ +--- +{{ ansible_managed | comment }} +{% if mysqld_exporter_tls_server_config | length > 0 %} +tls_server_config: +{{ mysqld_exporter_tls_server_config | to_nice_yaml | indent(2, true) }} +{% endif %} + +{% if mysqld_exporter_http_server_config | length > 0 %} +http_server_config: +{{ mysqld_exporter_http_server_config | to_nice_yaml | indent(2, true) }} +{% endif %} + +{% if mysqld_exporter_basic_auth_users | length > 0 %} +basic_auth_users: +{% for k, v in mysqld_exporter_basic_auth_users.items() %} + {{ k }}: {{ v | password_hash('bcrypt', ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' | shuffle(seed=inventory_hostname) | join)[:22], rounds=9) }} +{% endfor %} +{% endif %} diff --git a/roles/mysqld_exporter/test-requirements.txt b/roles/mysqld_exporter/test-requirements.txt new file mode 100644 index 00000000..7f0b6e75 --- /dev/null +++ b/roles/mysqld_exporter/test-requirements.txt @@ -0,0 +1 @@ +bcrypt diff --git a/roles/mysqld_exporter/vars/main.yml b/roles/mysqld_exporter/vars/main.yml new file mode 100644 index 00000000..ae7aed9f --- /dev/null +++ b/roles/mysqld_exporter/vars/main.yml @@ -0,0 +1,11 @@ +--- +go_arch_map: + i386: '386' + x86_64: 'amd64' + aarch64: 'arm64' + armv7l: 'armv7' + armv6l: 'armv6' + +go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}" + +_mysqld_exporter_repo: "prometheus/mysqld_exporter" diff --git a/tests/integration/targets/molecule-mysqld_exporter-alternative/runme.sh b/tests/integration/targets/molecule-mysqld_exporter-alternative/runme.sh new file mode 100755 index 00000000..d094c3e1 --- /dev/null +++ b/tests/integration/targets/molecule-mysqld_exporter-alternative/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh" diff --git a/tests/integration/targets/molecule-mysqld_exporter-default/runme.sh b/tests/integration/targets/molecule-mysqld_exporter-default/runme.sh new file mode 100755 index 00000000..d094c3e1 --- /dev/null +++ b/tests/integration/targets/molecule-mysqld_exporter-default/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh" diff --git a/tests/integration/targets/molecule-mysqld_exporter-latest/runme.sh b/tests/integration/targets/molecule-mysqld_exporter-latest/runme.sh new file mode 100755 index 00000000..d094c3e1 --- /dev/null +++ b/tests/integration/targets/molecule-mysqld_exporter-latest/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh"