From 3e4ff178867cf2562dce6e43fddc58f1de44ec01 Mon Sep 17 00:00:00 2001 From: Oleg Klyuchkin Date: Thu, 22 Feb 2024 16:43:51 +0300 Subject: [PATCH] feat: Add mongodb_exporter (#293) * feat: Add mongodb_exporter --------- Signed-off-by: anviar --- roles/mongodb_exporter/README.md | 44 +++++++ roles/mongodb_exporter/defaults/main.yml | 36 ++++++ roles/mongodb_exporter/handlers/main.yml | 10 ++ .../mongodb_exporter/meta/argument_specs.yml | 120 ++++++++++++++++++ roles/mongodb_exporter/meta/main.yml | 31 +++++ .../molecule/alternative/molecule.yml | 17 +++ .../molecule/alternative/prepare.yml | 76 +++++++++++ .../alternative/tests/test_alternative.py | 46 +++++++ .../molecule/default/molecule.yml | 6 + .../molecule/default/tests/test_default.py | 64 ++++++++++ .../molecule/latest/molecule.yml | 6 + .../molecule/latest/tests/test_latest.py | 43 +++++++ roles/mongodb_exporter/tasks/configure.yml | 36 ++++++ roles/mongodb_exporter/tasks/install.yml | 69 ++++++++++ roles/mongodb_exporter/tasks/main.yml | 63 +++++++++ roles/mongodb_exporter/tasks/preflight.yml | 110 ++++++++++++++++ roles/mongodb_exporter/tasks/selinux.yml | 23 ++++ .../templates/mongodb_exporter.service.j2 | 82 ++++++++++++ .../templates/web_config.yaml.j2 | 18 +++ roles/mongodb_exporter/test-requirements.txt | 1 + roles/mongodb_exporter/vars/main.yml | 11 ++ .../runme.sh | 4 + .../runme.sh | 4 + .../molecule-mongodb_exporter-latest/runme.sh | 4 + 24 files changed, 924 insertions(+) create mode 100644 roles/mongodb_exporter/README.md create mode 100644 roles/mongodb_exporter/defaults/main.yml create mode 100644 roles/mongodb_exporter/handlers/main.yml create mode 100644 roles/mongodb_exporter/meta/argument_specs.yml create mode 100644 roles/mongodb_exporter/meta/main.yml create mode 100644 roles/mongodb_exporter/molecule/alternative/molecule.yml create mode 100644 roles/mongodb_exporter/molecule/alternative/prepare.yml create mode 100644 roles/mongodb_exporter/molecule/alternative/tests/test_alternative.py create mode 100644 roles/mongodb_exporter/molecule/default/molecule.yml create mode 100644 roles/mongodb_exporter/molecule/default/tests/test_default.py create mode 100644 roles/mongodb_exporter/molecule/latest/molecule.yml create mode 100644 roles/mongodb_exporter/molecule/latest/tests/test_latest.py create mode 100644 roles/mongodb_exporter/tasks/configure.yml create mode 100644 roles/mongodb_exporter/tasks/install.yml create mode 100644 roles/mongodb_exporter/tasks/main.yml create mode 100644 roles/mongodb_exporter/tasks/preflight.yml create mode 100644 roles/mongodb_exporter/tasks/selinux.yml create mode 100644 roles/mongodb_exporter/templates/mongodb_exporter.service.j2 create mode 100644 roles/mongodb_exporter/templates/web_config.yaml.j2 create mode 100644 roles/mongodb_exporter/test-requirements.txt create mode 100644 roles/mongodb_exporter/vars/main.yml create mode 100755 tests/integration/targets/molecule-mongodb_exporter-alternative/runme.sh create mode 100755 tests/integration/targets/molecule-mongodb_exporter-default/runme.sh create mode 100755 tests/integration/targets/molecule-mongodb_exporter-latest/runme.sh diff --git a/roles/mongodb_exporter/README.md b/roles/mongodb_exporter/README.md new file mode 100644 index 000000000..84750239a --- /dev/null +++ b/roles/mongodb_exporter/README.md @@ -0,0 +1,44 @@ +

graph logo

+ +# Ansible Role: mongodb_exporter + +## Description + +Deploy prometheus [mongodb_exporter](https://github.com/percona/mongodb_exporter/) using ansible. + +## Requirements + +- Ansible >= 2.9 (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) file as well as in [meta/argument_specs.yml](meta/argument_specs.yml). +Please refer to the [collection docs](https://prometheus-community.github.io/ansible/branch/main/mongodb_exporter_role.html) for description and default values of the variables. + +## Example + +### Demo site + +We provide demo site for full monitoring solution based on prometheus and grafana. Repository with code and links to running instances is [available on github](https://github.com/prometheus/demo-site) and 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 to for your system. Running your tests is as simple as executing `molecule test`. + +## Continuous Intergation + +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 a quite 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/mongodb_exporter/defaults/main.yml b/roles/mongodb_exporter/defaults/main.yml new file mode 100644 index 000000000..ddb7b0893 --- /dev/null +++ b/roles/mongodb_exporter/defaults/main.yml @@ -0,0 +1,36 @@ +--- +mongodb_exporter_version: 0.40.0 +mongodb_exporter_binary_local_dir: "" +mongodb_exporter_binary_url: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/\ + mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz" +mongodb_exporter_checksums_url: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/\ + mongodb_exporter_{{ mongodb_exporter_version }}_checksums.txt" +mongodb_exporter_skip_install: false + +mongodb_exporter_web_listen_address: "0.0.0.0:9216" +mongodb_exporter_web_telemetry_path: "/metrics" + +mongodb_exporter_uri: "mongodb://127.0.0.1:27017/admin?ssl=false" +mongodb_exporter_collectors: [] +mongodb_exporter_collstats_colls: [] +mongodb_exporter_indexstats_colls: [] +mongodb_exporter_collstats_limit: 0 +mongodb_exporter_timeout_offset: 1 +mongodb_exporter_profile_time_ts: 30 + +mongodb_exporter_compatible_mode: false +mongodb_exporter_discovering_mode: false +mongodb_exporter_direct_connect: false +mongodb_exporter_global_conn_pool: false +mongodb_exporter_metrics_overridedescendingindex: false + +mongodb_exporter_tls_server_config: {} +mongodb_exporter_http_server_config: {} +mongodb_exporter_basic_auth_users: {} + +mongodb_exporter_log_level: "error" + +mongodb_exporter_binary_install_dir: "/usr/local/bin" +mongodb_exporter_system_group: "{{ mongodb_exporter_system_user }}" +mongodb_exporter_system_user: "mongodb-exp" +mongodb_exporter_config_dir: "/etc/mongodb_exporter" diff --git a/roles/mongodb_exporter/handlers/main.yml b/roles/mongodb_exporter/handlers/main.yml new file mode 100644 index 000000000..79b96814f --- /dev/null +++ b/roles/mongodb_exporter/handlers/main.yml @@ -0,0 +1,10 @@ +--- +- name: Restart mongodb_exporter + listen: "restart mongodb_exporter" + become: true + ansible.builtin.systemd: + daemon_reload: true + name: mongodb_exporter + state: restarted + when: + - not ansible_check_mode diff --git a/roles/mongodb_exporter/meta/argument_specs.yml b/roles/mongodb_exporter/meta/argument_specs.yml new file mode 100644 index 000000000..780fe0f5e --- /dev/null +++ b/roles/mongodb_exporter/meta/argument_specs.yml @@ -0,0 +1,120 @@ +--- +# yamllint disable rule:line-length +argument_specs: + main: + short_description: "Prometheus mongodb_exporter" + description: + - "Deploy prometheus L(mongodb exporter,https://github.com/percona/mongodb_exporter) using ansible" + author: + - "Prometheus Community" + options: + mongodb_exporter_version: + description: "mongodb_exporter package version. Also accepts latest as parameter." + default: "0.40.0" + mongodb_exporter_skip_install: + description: "mongodb_exporter installation tasks gets skipped when set to true." + type: bool + default: false + mongodb_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(mongodb_exporter) binary is stored on the host where ansible is run." + - "This overrides the I(mongodb_exporter_version) parameter" + mongodb_exporter_binary_url: + description: "URL of the mongodb_exporter binaries .tar.gz file" + default: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz" + mongodb_exporter_checksums_url: + description: "URL of the mongodb_exporter checksums file" + default: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/mongodb_exporter_{{ mongodb_exporter_version }}_checksums.txt" + mongodb_exporter_web_listen_address: + description: "Address on which mongodb exporter will listen" + default: "0.0.0.0:9216" + mongodb_exporter_web_telemetry_path: + description: "Path under which to expose metrics" + default: "/metrics" + mongodb_exporter_config_dir: + description: "The path where exporter configuration is stored" + default: "/etc/mongodb_exporter" + mongodb_exporter_tls_server_config: + description: + - "Configuration for TLS authentication." + - "Keys and values are the same as in L(mongodb_exporter docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)." + type: "dict" + mongodb_exporter_http_server_config: + description: + - "Config for HTTP/2 support." + - "Keys and values are the same as in L(mongodb_exporter docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)." + type: "dict" + mongodb_exporter_basic_auth_users: + description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt." + type: "dict" + mongodb_exporter_log_level: + description: "Only log messages with the given severity or above." + default: "error" + choices: + - "debug" + - "info" + - "warn" + - "error" + - "fatal" + mongodb_exporter_uri: + description: "MongoDB connection URI" + required: true + type: "str" + mongodb_exporter_collectors: + description: + - "List collectors from L(documantation, https://github.com/percona/mongodb_exporter/blob/main/REFERENCE.md)." + - "You can specify 'all' to enable all collectors" + mongodb_exporter_collstats_colls: + description: "List of databases.collections to get $collStats" + type: "list" + mongodb_exporter_indexstats_colls: + description: "List of databases.collections to get $indexStats" + type: "list" + mongodb_exporter_collstats_limit: + description: "Disable collstats, dbstats, topmetrics and indexstats collector if there are more than collections. 0=No limit" + type: "int" + default: 0 + mongodb_exporter_timeout_offset: + description: "Offset to subtract from the timeout in seconds" + type: "int" + default: 1 + mongodb_exporter_profile_time_ts: + description: "Set time for scrape slow queries" + type: "int" + default: 30 + mongodb_exporter_compatible_mode: + description: "Enable old mongodb-exporter compatible metrics" + type: bool + default: false + mongodb_exporter_discovering_mode: + description: "Enable autodiscover collections" + type: bool + default: false + mongodb_exporter_direct_connect: + description: "Whether or not a direct connect should be made. Direct connections are not valid if multiple hosts are specified or an SRV URI is used" + type: bool + default: false + mongodb_exporter_global_conn_pool: + description: "Use global connection pool instead of creating new pool for each http request" + type: bool + default: false + mongodb_exporter_metrics_overridedescendingindex: + description: "Enable descending index name override to replace -1 with _DESC" + type: bool + default: false + mongodb_exporter_binary_install_dir: + description: + - "I(Advanced)" + - "Directory to install mongodb_exporter binary" + default: "/usr/local/bin" + mongodb_exporter_system_group: + description: + - "I(Advanced)" + - "System group for mongodb_exporter" + default: "mongodb-exp" + mongodb_exporter_system_user: + description: + - "I(Advanced)" + - "mongodb_exporter user" + default: "mongodb-exp" diff --git a/roles/mongodb_exporter/meta/main.yml b/roles/mongodb_exporter/meta/main.yml new file mode 100644 index 000000000..25b2fe0f6 --- /dev/null +++ b/roles/mongodb_exporter/meta/main.yml @@ -0,0 +1,31 @@ +--- +galaxy_info: + author: "Prometheus Community" + description: "Prometheus mongodb_exporter" + license: "Apache" + min_ansible_version: "2.9" + platforms: + - name: "Ubuntu" + versions: + - "focal" + - "jammy" + - name: "Debian" + versions: + - "bullseye" + - "buster" + - name: "EL" + versions: + - "7" + - "8" + - "9" + - name: "Fedora" + versions: + - "37" + - '38' + galaxy_tags: + - "monitoring" + - "prometheus" + - "exporter" + - "metrics" + - "system" + - "mongodb" diff --git a/roles/mongodb_exporter/molecule/alternative/molecule.yml b/roles/mongodb_exporter/molecule/alternative/molecule.yml new file mode 100644 index 000000000..c9807730a --- /dev/null +++ b/roles/mongodb_exporter/molecule/alternative/molecule.yml @@ -0,0 +1,17 @@ +--- +provisioner: + inventory: + group_vars: + all: + mongodb_exporter_binary_local_dir: "/tmp/mongodb_exporter-linux-amd64_local" + mongodb_exporter_web_listen_address: "127.0.1.1:9216" + + mongodb_exporter_tls_server_config: + cert_file: /etc/mongodb_exporter/tls.cert + key_file: /etc/mongodb_exporter/tls.key + mongodb_exporter_http_server_config: + http2: true + mongodb_exporter_basic_auth_users: + randomuser: examplepassword + go_arch: amd64 + mongodb_exporter_version: 0.40.0 diff --git a/roles/mongodb_exporter/molecule/alternative/prepare.yml b/roles/mongodb_exporter/molecule/alternative/prepare.yml new file mode 100644 index 000000000..5f0c0b5f1 --- /dev/null +++ b/roles/mongodb_exporter/molecule/alternative/prepare.yml @@ -0,0 +1,76 @@ +--- +- name: Run local preparation + hosts: localhost + gather_facts: false + tasks: + - name: Download mongodb_exporter binary to local folder + become: false + ansible.builtin.get_url: + url: "https://github.com/percona/mongodb_exporter/releases/download/v{{ mongodb_exporter_version }}/\ + mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp/mongodb_exporter-{{ mongodb_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 mongodb_exporter binary + become: false + ansible.builtin.unarchive: + src: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp" + creates: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}/mongodb_exporter" + check_mode: false + + - name: Link to mongodb_exporter binaries directory + become: false + ansible.builtin.file: + src: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}" + dest: "{{ mongodb_exporter_binary_local_dir }}" + 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 + +- name: Run target preparation + hosts: all + any_errors_fatal: true + tasks: + - name: Create mongodb_exporter cert dir + ansible.builtin.file: + path: "{{ mongodb_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: "{{ mongodb_exporter_tls_server_config.cert_file }}" + - src: "/tmp/tls.key" + dest: "{{ mongodb_exporter_tls_server_config.key_file }}" diff --git a/roles/mongodb_exporter/molecule/alternative/tests/test_alternative.py b/roles/mongodb_exporter/molecule/alternative/tests/test_alternative.py new file mode 100644 index 000000000..a102ea94f --- /dev/null +++ b/roles/mongodb_exporter/molecule/alternative/tests/test_alternative.py @@ -0,0 +1,46 @@ +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/mongodb_exporter" + ] + for dir in dirs: + d = host.file(dir) + assert d.is_directory + assert d.exists + + +def test_service(host): + s = host.service("mongodb_exporter") + try: + assert s.is_running + except AssertionError: + # Capture service logs + journal_output = host.run('journalctl -u mongodb_exporter --since "1 hour ago"') + print("\n==== journalctl -u mongodb_exporter Output ====\n") + print(journal_output) + print("\n============================================\n") + raise # Re-raise the original assertion error + + +def test_protecthome_property(host): + s = host.service("mongodb_exporter") + p = s.systemd_properties + assert p.get("ProtectHome") == "yes" + + +def test_socket(host): + sockets = [ + "tcp://127.0.1.1:9216" + ] + for socket in sockets: + s = host.socket(socket) + assert s.is_listening diff --git a/roles/mongodb_exporter/molecule/default/molecule.yml b/roles/mongodb_exporter/molecule/default/molecule.yml new file mode 100644 index 000000000..2e05157fd --- /dev/null +++ b/roles/mongodb_exporter/molecule/default/molecule.yml @@ -0,0 +1,6 @@ +--- +provisioner: + inventory: + group_vars: + all: + mongodb_exporter_web_listen_address: "127.0.0.1:9216" diff --git a/roles/mongodb_exporter/molecule/default/tests/test_default.py b/roles/mongodb_exporter/molecule/default/tests/test_default.py new file mode 100644 index 000000000..53e3858d0 --- /dev/null +++ b/roles/mongodb_exporter/molecule/default/tests/test_default.py @@ -0,0 +1,64 @@ +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/mongodb_exporter" + ] + for dir in dirs: + d = host.file(dir) + assert d.is_directory + assert d.exists + + +def test_files(host): + files = [ + "/etc/systemd/system/mongodb_exporter.service", + "/usr/local/bin/mongodb_exporter" + ] + for file in files: + f = host.file(file) + assert f.exists + assert f.is_file + + +def test_user(host): + assert host.group("mongodb-exp").exists + assert "mongodb-exp" in host.user("mongodb-exp").groups + assert host.user("mongodb-exp").shell == "/usr/sbin/nologin" + assert host.user("mongodb-exp").home == "/" + + +def test_service(host): + s = host.service("mongodb_exporter") + try: + assert s.is_running + except AssertionError: + # Capture service logs + journal_output = host.run('journalctl -u mongodb_exporter --since "1 hour ago"') + print("\n==== journalctl -u mongodb_exporter Output ====\n") + print(journal_output) + print("\n============================================\n") + raise # Re-raise the original assertion error + + +def test_protecthome_property(host): + s = host.service("mongodb_exporter") + p = s.systemd_properties + assert p.get("ProtectHome") == "yes" + + +def test_socket(host): + sockets = [ + "tcp://127.0.0.1:9216" + ] + for socket in sockets: + s = host.socket(socket) + assert s.is_listening diff --git a/roles/mongodb_exporter/molecule/latest/molecule.yml b/roles/mongodb_exporter/molecule/latest/molecule.yml new file mode 100644 index 000000000..76b784e7f --- /dev/null +++ b/roles/mongodb_exporter/molecule/latest/molecule.yml @@ -0,0 +1,6 @@ +--- +provisioner: + inventory: + group_vars: + all: + mongodb_exporter_version: latest diff --git a/roles/mongodb_exporter/molecule/latest/tests/test_latest.py b/roles/mongodb_exporter/molecule/latest/tests/test_latest.py new file mode 100644 index 000000000..7f6a621c2 --- /dev/null +++ b/roles/mongodb_exporter/molecule/latest/tests/test_latest.py @@ -0,0 +1,43 @@ +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/mongodb_exporter.service", + "/usr/local/bin/mongodb_exporter" +]) +def test_files(host, files): + f = host.file(files) + assert f.exists + assert f.is_file + + +def test_service(host): + s = host.service("mongodb_exporter") + try: + assert s.is_running + except AssertionError: + # Capture service logs + journal_output = host.run('journalctl -u mongodb_exporter --since "1 hour ago"') + print("\n==== journalctl -u mongodb_exporter Output ====\n") + print(journal_output) + print("\n============================================\n") + raise # Re-raise the original assertion error + + +def test_protecthome_property(host): + s = host.service("mongodb_exporter") + p = s.systemd_properties + assert p.get("ProtectHome") == "yes" + + +def test_socket(host): + s = host.socket("tcp://0.0.0.0:9216") + assert s.is_listening diff --git a/roles/mongodb_exporter/tasks/configure.yml b/roles/mongodb_exporter/tasks/configure.yml new file mode 100644 index 000000000..e83997710 --- /dev/null +++ b/roles/mongodb_exporter/tasks/configure.yml @@ -0,0 +1,36 @@ +--- +- name: Copy the mongodb_exporter systemd service file + ansible.builtin.template: + src: mongodb_exporter.service.j2 + dest: /etc/systemd/system/mongodb_exporter.service + owner: root + group: root + mode: 0644 + notify: restart mongodb_exporter + +- name: Create mongodb_exporter config directory + ansible.builtin.file: + path: "/etc/mongodb_exporter" + state: directory + owner: root + group: root + mode: u+rwX,g+rwX,o=rX + +- name: Copy the mongodb_exporter web config file + ansible.builtin.template: + src: web_config.yaml.j2 + dest: /etc/mongodb_exporter/web_config.yaml + owner: root + group: root + mode: 0644 + notify: restart mongodb_exporter + +- name: Allow mongodb_exporter port in SELinux on RedHat OS family + community.general.seport: + ports: "{{ mongodb_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/mongodb_exporter/tasks/install.yml b/roles/mongodb_exporter/tasks/install.yml new file mode 100644 index 000000000..92b447911 --- /dev/null +++ b/roles/mongodb_exporter/tasks/install.yml @@ -0,0 +1,69 @@ +--- +- name: Create the mongodb_exporter group + ansible.builtin.group: + name: "{{ mongodb_exporter_system_group }}" + state: present + system: true + when: mongodb_exporter_system_group not in ["root", 'mongodb', 'nogroup'] + +- name: Create the mongodb_exporter user + ansible.builtin.user: + name: "{{ mongodb_exporter_system_user }}" + groups: "{{ mongodb_exporter_system_group }}" + append: true + shell: /usr/sbin/nologin + system: true + create_home: false + home: / + when: mongodb_exporter_system_user not in ["root", 'mongodb', 'nobody'] + +- name: Get binary + when: + - mongodb_exporter_binary_local_dir | length == 0 + - not mongodb_exporter_skip_install + block: + + - name: Download mongodb_exporter binary to local folder + become: false + ansible.builtin.get_url: + url: "{{ mongodb_exporter_binary_url }}" + dest: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz" + checksum: "sha256:{{ __mongodb_exporter_checksum }}" + mode: '0644' + register: _download_binary + until: _download_binary is succeeded + retries: 5 + delay: 2 + delegate_to: localhost + check_mode: false + + - name: Unpack mongodb_exporter binary + become: false + ansible.builtin.unarchive: + src: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp" + creates: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}/mongodb_exporter" + delegate_to: localhost + check_mode: false + + - name: Propagate mongodb_exporter binaries + ansible.builtin.copy: + src: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}/mongodb_exporter" + dest: "{{ mongodb_exporter_binary_install_dir }}/mongodb_exporter" + mode: 0755 + owner: root + group: root + notify: restart mongodb_exporter + when: not ansible_check_mode + +- name: Propagate locally distributed mongodb_exporter binary + ansible.builtin.copy: + src: "{{ mongodb_exporter_binary_local_dir }}/mongodb_exporter" + dest: "{{ mongodb_exporter_binary_install_dir }}/mongodb_exporter" + mode: 0755 + owner: root + group: root + when: + - mongodb_exporter_binary_local_dir | length > 0 + - not mongodb_exporter_skip_install + notify: restart mongodb_exporter diff --git a/roles/mongodb_exporter/tasks/main.yml b/roles/mongodb_exporter/tasks/main.yml new file mode 100644 index 000000000..b49b7c9f6 --- /dev/null +++ b/roles/mongodb_exporter/tasks/main.yml @@ -0,0 +1,63 @@ +--- +- name: Preflight + ansible.builtin.include_tasks: + file: preflight.yml + apply: + tags: + - mongodb_exporter_install + - mongodb_exporter_configure + - mongodb_exporter_run + tags: + - mongodb_exporter_install + - mongodb_exporter_configure + - mongodb_exporter_run + +- name: Install + ansible.builtin.include_tasks: + file: install.yml + apply: + become: true + tags: + - mongodb_exporter_install + when: + ( not __mongodb_exporter_is_installed.stat.exists ) or + ( (__mongodb_exporter_current_version_output.stderr_lines | length > 0) + and (__mongodb_exporter_current_version_output.stderr_lines[1].split(" ")[1] != 'v' + mongodb_exporter_version) ) or + ( (__mongodb_exporter_current_version_output.stdout_lines | length > 0) + and (__mongodb_exporter_current_version_output.stdout_lines[1].split(" ")[1] != 'v' + mongodb_exporter_version) ) or + ( mongodb_exporter_binary_local_dir | length > 0 ) + tags: + - mongodb_exporter_install + +- name: SELinux + ansible.builtin.include_tasks: + file: selinux.yml + apply: + become: true + tags: + - mongodb_exporter_configure + when: ansible_selinux.status == "enabled" + tags: + - mongodb_exporter_configure + +- name: Configure + ansible.builtin.include_tasks: + file: configure.yml + apply: + become: true + tags: + - mongodb_exporter_configure + tags: + - mongodb_exporter_configure + +- name: Ensure mongodb_exporter is enabled on boot + become: true + ansible.builtin.systemd: + daemon_reload: true + name: mongodb_exporter + enabled: true + state: started + when: + - not ansible_check_mode + tags: + - mongodb_exporter_run diff --git a/roles/mongodb_exporter/tasks/preflight.yml b/roles/mongodb_exporter/tasks/preflight.yml new file mode 100644 index 000000000..d5e28d0b3 --- /dev/null +++ b/roles/mongodb_exporter/tasks/preflight.yml @@ -0,0 +1,110 @@ +--- +- 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 + become: true + 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: Assert that used version supports listen address type + ansible.builtin.assert: + that: + - >- + mongodb_exporter_web_listen_address is string + +- name: Naive assertion of proper listen address + ansible.builtin.assert: + that: + - >- + [mongodb_exporter_web_listen_address] | + flatten | + reject('match', '.+:\\d+$') | + list | + length == 0 + +- name: Assert that TLS config is correct + when: mongodb_exporter_tls_server_config | length > 0 + block: + - name: Assert that TLS key and cert path are set + ansible.builtin.assert: + that: + - "mongodb_exporter_tls_server_config.cert_file is defined" + - "mongodb_exporter_tls_server_config.key_file is defined" + + - name: Check existence of TLS cert file + ansible.builtin.stat: + path: "{{ mongodb_exporter_tls_server_config.cert_file }}" + register: __mongodb_exporter_cert_file + + - name: Check existence of TLS key file + ansible.builtin.stat: + path: "{{ mongodb_exporter_tls_server_config.key_file }}" + register: __mongodb_exporter_key_file + + - name: Assert that TLS key and cert are present + ansible.builtin.assert: + that: + - "__mongodb_exporter_cert_file.stat.exists" + - "__mongodb_exporter_key_file.stat.exists" + +- name: Check if mongodb_exporter is installed + ansible.builtin.stat: + path: "{{ mongodb_exporter_binary_install_dir }}/mongodb_exporter" + register: __mongodb_exporter_is_installed + check_mode: false + tags: + - mongodb_exporter_install + +- name: Gather currently installed mongodb_exporter version (if any) + ansible.builtin.command: "{{ mongodb_exporter_binary_install_dir }}/mongodb_exporter --version" + changed_when: false + register: __mongodb_exporter_current_version_output + check_mode: false + when: __mongodb_exporter_is_installed.stat.exists + tags: + - mongodb_exporter_install + +- name: Discover latest version + ansible.builtin.set_fact: + mongodb_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _mongodb_exporter_repo }}/releases/latest', headers=_github_api_headers, + split_lines=False) | from_json).get('tag_name') | replace('v', '') }}" + run_once: true + until: mongodb_exporter_version is version('0.0.0', '>=') + retries: 10 + when: + - mongodb_exporter_version == "latest" + - mongodb_exporter_binary_local_dir | length == 0 + - not mongodb_exporter_skip_install + +- name: Get mongodb_exporter binary checksum + when: + - mongodb_exporter_binary_local_dir | length == 0 + - not mongodb_exporter_skip_install + block: + - name: Get checksum list from github + ansible.builtin.set_fact: + __mongodb_exporter_checksums: "{{ lookup('url', mongodb_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}" + run_once: true + until: __mongodb_exporter_checksums is search('linux-' + go_arch + '.tar.gz') + retries: 3 + + - name: "Get checksum for {{ go_arch }}" + ansible.builtin.set_fact: + __mongodb_exporter_checksum: "{{ item.split(' ')[0] }}" + with_items: "{{ __mongodb_exporter_checksums }}" + when: + - "item.endswith('mongodb_exporter-' + mongodb_exporter_version + '.linux-' + go_arch + '.tar.gz')" diff --git a/roles/mongodb_exporter/tasks/selinux.yml b/roles/mongodb_exporter/tasks/selinux.yml new file mode 100644 index 000000000..754cbd0a2 --- /dev/null +++ b/roles/mongodb_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/mongodb_exporter/templates/mongodb_exporter.service.j2 b/roles/mongodb_exporter/templates/mongodb_exporter.service.j2 new file mode 100644 index 000000000..c241f7fd1 --- /dev/null +++ b/roles/mongodb_exporter/templates/mongodb_exporter.service.j2 @@ -0,0 +1,82 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=Prometheus MongoDB Exporter +Documentation=https://github.com/percona/mongodb_exporter +After=network.target + +[Service] +Type=simple + +User={{ mongodb_exporter_system_user }} +Group={{ mongodb_exporter_system_group }} + +ExecStart={{ mongodb_exporter_binary_install_dir }}/mongodb_exporter \ +{% if mongodb_exporter_collectors is iterable %} +{% for collector in mongodb_exporter_collectors -%} + --collector.{{ collector }} \ +{% endfor %} +{% elif mongodb_exporter_collectors is string and mongodb_exporter_collectors == "all" %} + --collect-all \ +{% endif -%} +{% if mongodb_exporter_collstats_colls | length > 0 -%} + --mongodb.collstats-colls={{ mongodb_exporter_collstats_colls | join(',') }} \ +{% endif -%} +{% if mongodb_exporter_indexstats_colls | length > 0 -%} + --mongodb.indexstats-colls={{ mongodb_exporter_indexstats_colls | join(',') }} \ +{% endif -%} +{% if mongodb_exporter_collstats_limit > 0 -%} + --collector.collstats-limit={{ mongodb_exporter_collstats_limit }} \ +{% endif -%} +{% if mongodb_exporter_tls_server_config | length > 0 or mongodb_exporter_http_server_config | length > 0 or mongodb_exporter_basic_auth_users | length > 0 -%} + --web.config={{ mongodb_exporter_config_dir }}/web_config.yaml \ +{% endif -%} +{% if mongodb_exporter_compatible_mode -%} + --compatible-mode \ +{% endif -%} +{% if mongodb_exporter_discovering_mode -%} + --discovering-mode \ +{% endif -%} +{% if mongodb_exporter_direct_connect -%} + --mongodb.direct-connect \ +{% endif -%} +{% if mongodb_exporter_global_conn_pool -%} + --mongodb.global-conn-pool \ +{% endif -%} +{% if mongodb_exporter_metrics_overridedescendingindex -%} + --metrics.overridedescendingindex \ +{% endif -%} +{% if mongodb_exporter_profile_time_ts != 30 -%} + --collector.profile-time-ts={{ mongodb_exporter_profile_time_ts }} \ +{% endif -%} +{% if mongodb_exporter_log_level != "error" -%} + --log.level="{{ mongodb_exporter_log_level }}" \ +{% endif -%} +{% if mongodb_exporter_timeout_offset != 1 -%} + --web.timeout-offset= {{ mongodb_exporter_timeout_offset }} \ +{% endif -%} +{% if mongodb_exporter_web_telemetry_path != "/metrics" -%} + --web.telemetry-path={{ mongodb_exporter_web_telemetry_path }} \ +{% endif %} + --web.listen-address={{ mongodb_exporter_web_listen_address }} \ + --mongodb.uri={{ mongodb_exporter_uri }} + +SyslogIdentifier=mongodb_exporter +Restart=always +RestartSec=1 +StartLimitInterval=0 + +ProtectHome=yes +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/mongodb_exporter/templates/web_config.yaml.j2 b/roles/mongodb_exporter/templates/web_config.yaml.j2 new file mode 100644 index 000000000..54453f535 --- /dev/null +++ b/roles/mongodb_exporter/templates/web_config.yaml.j2 @@ -0,0 +1,18 @@ +--- +{{ ansible_managed | comment }} +{% if mongodb_exporter_tls_server_config | length > 0 %} +tls_server_config: +{{ mongodb_exporter_tls_server_config | to_nice_yaml | indent(2, true) }} +{% endif %} + +{% if mongodb_exporter_http_server_config | length > 0 %} +http_server_config: +{{ mongodb_exporter_http_server_config | to_nice_yaml | indent(2, true) }} +{% endif %} + +{% if mongodb_exporter_basic_auth_users | length > 0 %} +basic_auth_users: +{% for k, v in mongodb_exporter_basic_auth_users.items() %} + {{ k }}: {{ v | string | password_hash('bcrypt', ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' | shuffle(seed=inventory_hostname) | join)[:22], rounds=9) }} +{% endfor %} +{% endif %} diff --git a/roles/mongodb_exporter/test-requirements.txt b/roles/mongodb_exporter/test-requirements.txt new file mode 100644 index 000000000..7f0b6e759 --- /dev/null +++ b/roles/mongodb_exporter/test-requirements.txt @@ -0,0 +1 @@ +bcrypt diff --git a/roles/mongodb_exporter/vars/main.yml b/roles/mongodb_exporter/vars/main.yml new file mode 100644 index 000000000..cb201f87d --- /dev/null +++ b/roles/mongodb_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) }}" +_mongodb_exporter_repo: "percona/mongodb_exporter" +_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}" diff --git a/tests/integration/targets/molecule-mongodb_exporter-alternative/runme.sh b/tests/integration/targets/molecule-mongodb_exporter-alternative/runme.sh new file mode 100755 index 000000000..d094c3e1b --- /dev/null +++ b/tests/integration/targets/molecule-mongodb_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-mongodb_exporter-default/runme.sh b/tests/integration/targets/molecule-mongodb_exporter-default/runme.sh new file mode 100755 index 000000000..d094c3e1b --- /dev/null +++ b/tests/integration/targets/molecule-mongodb_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-mongodb_exporter-latest/runme.sh b/tests/integration/targets/molecule-mongodb_exporter-latest/runme.sh new file mode 100755 index 000000000..d094c3e1b --- /dev/null +++ b/tests/integration/targets/molecule-mongodb_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"