Skip to content

Commit

Permalink
feat: Add mongodb_exporter
Browse files Browse the repository at this point in the history
Signed-off-by: anviar <oleg.kluchkin+github@gmail.com>
  • Loading branch information
anviar committed Feb 16, 2024
1 parent f189e53 commit 5ea28fa
Show file tree
Hide file tree
Showing 24 changed files with 904 additions and 0 deletions.
44 changes: 44 additions & 0 deletions roles/mongodb_exporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<p><img src="https://www.circonus.com/wp-content/uploads/2015/03/sol-icon-itOps.png" alt="graph logo" title="graph" align="right" height="60" /></p>

# 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.
35 changes: 35 additions & 0 deletions roles/mongodb_exporter/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
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: "nogroup"
mongodb_exporter_system_user: "mongodb-exp"
10 changes: 10 additions & 0 deletions roles/mongodb_exporter/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -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
116 changes: 116 additions & 0 deletions roles/mongodb_exporter/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---
# 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"

Check failure on line 25 in roles/mongodb_exporter/meta/argument_specs.yml

View workflow job for this annotation

GitHub Actions / ansible-lint

yaml[trailing-spaces]

Trailing spaces
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_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
mongodb_exporter_collectors:
description:

Check failure on line 61 in roles/mongodb_exporter/meta/argument_specs.yml

View workflow job for this annotation

GitHub Actions / ansible-lint

yaml[trailing-spaces]

Trailing spaces
- "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 <n> 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: "nogroup"
mongodb_exporter_system_user:
description:
- "I(Advanced)"
- "mongodb_exporter user"
default: "mongodb-exp"
31 changes: 31 additions & 0 deletions roles/mongodb_exporter/meta/main.yml
Original file line number Diff line number Diff line change
@@ -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"
17 changes: 17 additions & 0 deletions roles/mongodb_exporter/molecule/alternative/molecule.yml
Original file line number Diff line number Diff line change
@@ -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
74 changes: 74 additions & 0 deletions roles/mongodb_exporter/molecule/alternative/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
- 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/{{ _mongodb_exporter_repo }}/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: Create local directory for binary
ansible.builtin.file:
path: "{{ mongodb_exporter_binary_local_dir }}"
state: directory
mode: '0755'

- name: Unpack mongodb_exporter binary
become: false
ansible.builtin.unarchive:
src: "/tmp/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
dest: "{{ mongodb_exporter_binary_local_dir }}"
creates: "{{ mongodb_exporter_binary_local_dir }}/mongodb_exporter"
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 }}"
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions roles/mongodb_exporter/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
mongodb_exporter_web_listen_address: "127.0.0.1:9216"

0 comments on commit 5ea28fa

Please sign in to comment.